|  | Home · All Classes · Main Classes · Annotated · Grouped Classes · Functions |

# QImage Class Reference

The QImage class provides a hardware-independent pixmap representation with direct access to the pixel data. More...

    #include <QImage>

Part of the QtGui module.

Inherits QPaintDevice.

- List of all members, including inherited members
- Qt 3 support members

### Public Types

- enum Endian { IgnoreEndian, BigEndian, LittleEndian }
- enum InvertMode { InvertRgb, InvertRgba }

### Public Functions

- QImage ()
- QImage ( int w, int h, int depth, int numColors = 0, Endian bitOrder = IgnoreEndian )
- QImage ( const QSize & size, int depth, int numColors = 0, Endian bitOrder = IgnoreEndian )
- QImage ( const char * const[] xpm )
- QImage ( const QString & fileName, const char * format = 0 )
- QImage ( const char * fileName, const char * format = 0 )
- QImage ( uchar * data, int w, int h, int depth, const QRgb * colortable, int numColors, Endian bitOrder )
- QImage ( uchar * data, int w, int h, int depth, int bpl, const QRgb * colortable, int numColors, Endian bitOrder )
- QImage ( const QImage & image )
- ~QImage ()
- bool allGray () const
- Endian bitOrder () const
- uchar * bits ()
- const uchar * bits () const
- int bytesPerLine () const
- QRgb color ( int i ) const
- QRgb * colorTable ()
- const QRgb * colorTable () const
- QImage convertBitOrder ( Endian bitOrder ) const
- QImage convertDepth ( int depth, Qt::ImageConversionFlags flags ) const
- QImage convertDepth ( int depth ) const
- QImage convertDepthWithPalette ( int d, QRgb * palette, int palette_count, Qt::ImageConversionFlags flags = Qt::AutoColor ) const
- QImage copy () const
- QImage copy ( int x, int y, int w, int h, Qt::ImageConversionFlags flags = Qt::AutoColor ) const
- QImage copy ( const QRect & r, Qt::ImageConversionFlags flags = Qt::AutoColor ) const
- bool create ( int width, int height, int depth, int numColors = 0, Endian bitOrder = IgnoreEndian )
- bool create ( const QSize & size, int depth, int numColors = 0, Endian bitOrder = IgnoreEndian )
- QImage createAlphaMask ( Qt::ImageConversionFlags flags = Qt::AutoColor ) const
- QImage createHeuristicMask ( bool clipTight = true ) const
- int depth () const
- void detach ()
- int dotsPerMeterX () const
- int dotsPerMeterY () const
- void fill ( uint pixel )
- bool hasAlphaBuffer () const
- int height () const
- void invertPixels ( InvertMode mode = InvertRgb )
- bool isDetached () const
- bool isGrayscale () const
- bool isNull () const
- uchar ** jumpTable ()
- const uchar * const * jumpTable () const
- bool load ( const QString & fileName, const char * format = 0 )
- bool loadFromData ( const uchar * data, int len, const char * format = 0 )
- bool loadFromData ( const QByteArray & data, const char * format = 0 )
- QImage mirror () const
- QImage mirror ( bool horizontal, bool vertical ) const
- int numBytes () const
- int numColors () const
- QPoint offset () const
- QRgb pixel ( int x, int y ) const
- int pixelIndex ( int x, int y ) const
- QRect rect () const
- void reset ()
- bool save ( const QString & fileName, const char * format, int quality = -1 ) const
- bool save ( QIODevice * device, const char * format, int quality = -1 ) const
- QImage scale ( int w, int h, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation ) const
- QImage scale ( const QSize & size, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation ) const
- QImage scaleHeight ( int h ) const
- QImage scaleWidth ( int w ) const
- uchar * scanLine ( int i )
- const uchar * scanLine ( int i ) const
- int serialNumber () const
- void setAlphaBuffer ( bool enable )
- void setColor ( int i, QRgb c )
- void setDotsPerMeterX ( int x )
- void setDotsPerMeterY ( int y )
- void setNumColors ( int numColors )
- void setOffset ( const QPoint & p )
- void setPixel ( int x, int y, uint index_or_rgb )
- void setText ( const char * key, const char * lang, const QString & s )
- QSize size () const
- QImage swapRGB () const
- QString text ( const char * key, const char * lang = 0 ) const
- QString text ( const QImageTextKeyLang & kl ) const
- QStringList textKeys () const
- QStringList textLanguages () const
- QList<QImageTextKeyLang> textList () const
- QImage transform ( const QMatrix & matrix, Qt::TransformationMode mode = Qt::FastTransformation ) const
- bool valid ( int x, int y ) const
- int width () const
- operator QVariant () const
- bool operator!= ( const QImage & i ) const
- QImage & operator= ( const QImage & image )
- bool operator== ( const QImage & i ) const

- 11 public functions inherited from QPaintDevice

### Static Public Members

- QImage fromData ( const uchar * data, int size, const char * format = 0 )
- QImage fromData ( const QByteArray & data, const char * format = 0 )
- Endian systemBitOrder ()
- Endian systemByteOrder ()
- QMatrix trueMatrix ( const QMatrix & matrix, int w, int h )

### Protected Functions

- virtual int metric ( PaintDeviceMetric metric ) const

### Related Non-Members

- void bitBlt ( QImage * dst, int dx, int dy, const QImage * src, int sx = 0, int sy = 0, int sw = -1, int sh = -1, Qt::ImageConversionFlags flags = Qt::AutoColor )
- QDataStream & operator<< ( QDataStream & s, const QImage & image )
- QDataStream & operator>> ( QDataStream & s, QImage & image )

## Detailed Description

The QImage class provides a hardware-independent pixmap representation with direct access to the pixel data.

Qt provides two classes for handling image data: QImage and QPixmap. QImage is designed and optimized for I/O, and for direct pixel access/manipulation. QPixmap is designed and optimized for drawing. There are functions to convert between QImage and QPixmap: QPixmap::convertToImage() and QPixmap::convertFromImage().

An image has the parameters width, height and depth (bits per pixel, bpp), a color table, and the actual pixels. QImage supports 1-bpp, 8-bpp, and 32-bpp image data. 1-bpp and 8-bpp images use a color lookup table; the pixel value is a color table index.

32-bpp images contain RGB values encoded in 24 bits, and do not use a color table. The most significant byte is used for the alpha buffer.

An entry in the color table is an RGB triplet encoded as an `qRgb` value. Use the color() function to obtain an entry from the table, and the qRed(), qGreen(), and qBlue() functions (`qcolor.h`) to access the components. The qRgb() function is used to make an RGB triplet suitable for use with the setColor() function.

1-bpp (monochrome) images have a color table with a most two colors. There are two different formats: big endian (MSB first) or little endian (LSB first) bit order. To access a single bit you will must do some bit shifts:

        QImage image;
        // sets bit at (x, y) to 1
        if (image.bitOrder() == QImage::LittleEndian)
            image.scanLine(y)[x >> 3] |= 1 << (x & 7);
        else
            image.scanLine(y)[x >> 3] |= 1 << (7 - (x & 7));

If this looks complicated, it might be a good idea to convert the 1-bpp image to an 8-bpp image using convertDepth().

8-bpp images are much easier to work with than 1-bpp images because they have a single byte per pixel:

        QImage image;

        // set entry 19 in the color table to yellow
        image.setColor(19, qRgb(255, 255, 0));

        // set 8 bit pixel at (x,y) to value yellow (in color table)
        image.scanLine(y)[x] = 19;

32-bpp images ignore the color table; instead, each pixel contains the RGB triplet. 24 bits contain the RGB value; the most significant byte is reserved for the alpha buffer.

        QImage image;

        // sets 32 bit pixel at (x,y) to yellow.
        uint *ptr = reinterpret_cast<uint *>(image.scanLine(y)) + x;
        *ptr = qRgb(255, 255, 0);

On Qt/Embedded, scanlines are aligned to the pixel depth and may be padded to any degree, while on all other platforms, the scanlines are 32-bit aligned for all depths. The constructor taking a `uchar*` argument always expects 32-bit aligned data. On Qt/Embedded, an additional constructor allows the number of bytes-per-line to be specified.

Pixel colors are retrieved with pixel() and set with setPixel().

QImage supports a variety of functions that can be used to obtain information about the image. width(), height(), dotsPerMeterX(), and dotsPerMeterY() provide information about the image size and resolution. The depth(), numColors(), isGrayscale(), and colorTable() functions provide information about the color depth and available color components used to store the image data. The hasAlphaBuffer() function indicates whether the image has an associated alpha buffer.

It is possible to determine whether a color image can be safely converted to a grayscale image by using the allGray() function. The bitOrder(), bytesPerLine(), and numBytes() functions provide low-level information about the data stored in the image.

QImage also supports a number of functions for creating a new image that is a transformed version of the original. For example, copy(), convertBitOrder(), convertDepth(), createAlphaMask(), createHeuristicMask(), mirror(), scale(), swapRGB() and transform(). There are also functions for changing attributes of an image in-place, for example, setAlphaBuffer(), setColor(), setDotsPerMeterX() and setDotsPerMeterY() and setNumColors().

Images can be loaded and saved in the supported formats. Images are saved to a file with save(). Images are loaded from a file with load() (or in the constructor) or from an array of data with loadFromData(). The lists of supported formats are available from QImageReader::supportedImageFormats() and QImageWriter::supportedImageFormat().

When loading an image, the file name can be either refer to an actual file on disk or to one of the application's embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application's executable.

Strings of text may be added to images using setText().

The QImage class uses implicit sharing, so you can pass QImage objects around by value.

New image formats can be added as plugins.

See also QImageReader, QImageWriter, QPixmap, QColor, and Shared Classes.

## Member Type Documentation

### enum QImage::Endian

This enum type is used to describe the endianness of the CPU and graphics hardware.

Constant | Value | Description
--- | --- | ---
`QImage::IgnoreEndian` | `2` | Endianness does not matter. Useful for some operations that are independent of endianness.
`QImage::BigEndian` | `0` | Network byte order, as on SPARC and Motorola CPUs.
`QImage::LittleEndian` | `1` | PC/Alpha byte order.

### enum QImage::InvertMode

This enum type is used to describe how pixel values should be inverted in the invertPixels() function.

Constant | Value | Description
--- | --- | ---
`QImage::InvertRgb` | `0` | Invert only the RGB values and leave the alpha channel unchanged.
`QImage::InvertRgba` | `1` | Invert all channels, including the alpha channel.

## Member Function Documentation

### QImage::QImage ()

Constructs a null image.

See also isNull().

### QImage::QImage ( int w, int h, int depth, int numColors = 0, Endian bitOrder = IgnoreEndian )

Constructs an image with w width, h height, depth bits per pixel, numColors colors and bit order bitOrder.

Using this constructor is the same as first constructing a null image and then calling the create() function.

See also create().

### QImage::QImage ( const QSize & size, int depth, int numColors = 0, Endian bitOrder = IgnoreEndian )

Constructs an image with size size pixels, depth depth bits, numColors and bitOrder endianness.

Using this constructor is the same as first constructing a null image and then calling the create() function.

See also create().

### QImage::QImage ( const char * const[] xpm )

Constructs an image from xpm, which must be a valid XPM image.

Errors are silently ignored.

Note that it's possible to squeeze the XPM variable a little bit by using an unusual declaration:

        static const char * const start_xpm[] = {
            "16 15 8 1",
            "a c #cec6bd",
        ....

The extra `const` makes the entire definition read-only, which is slightly more efficient (e.g. when the code is in a shared library) and ROMable when the application is to be stored in ROM.

### QImage::QImage ( const QString & fileName, const char * format = 0 )

Constructs an image and tries to load the image from the file fileName.

If format is specified, the loader attempts to read the image using the specified format. If format is not specified (which is the default), the loader reads a few bytes from the header to guess the file format.

If the loading of the image failed, this object is a null image.

The QImageReader documentation lists the supported image formats and explains how to add extra formats.

The file name can be either refer to an actual file on disk or to one of the application's embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application's executable.

See also load(), isNull(), and QImageReader.

### QImage::QImage ( const char * fileName, const char * format = 0 )

Constructs an image and tries to load the image from the file fileName.

If format is specified, the loader attempts to read the image using the specified format. If format is not specified (which is the default), the loader reads a few bytes from the header to guess the file format.

If the loading of the image failed, this object is a null image.

The QImageReader documentation lists the supported image formats and explains how to add extra formats.

The file name can be either refer to an actual file on disk or to one of the application's embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application's executable.

You can disable this constructor by defining `QT_NO_CAST_FROM_ASCII` when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.

See also QString::fromAscii(), sa, load(), isNull(), and QImageReader.

### QImage::QImage ( uchar * data, int w, int h, int depth, const QRgb * colortable, int numColors, Endian bitOrder )

Constructs an image w pixels wide, h pixels high with a color depth of depth, that uses an existing memory buffer, data. The buffer must remain valid throughout the life of the QImage. The image does not delete the buffer at destruction.

If colortable is 0, a color table sufficient for numColors will be allocated (and destructed later).

Note that data must be 32-bit aligned.

The endianness is given in bitOrder.

### QImage::QImage ( uchar * data, int w, int h, int depth, int bpl, const QRgb * colortable, int numColors, Endian bitOrder )

Constructs an image that uses an existing memory buffer. The buffer must remain valid for the life of the QImage. The image does not delete the buffer at destruction. The buffer is passed as data. The image's width is w and its height is h. The color depth is depth. bpl specifies the number of bytes per line.

If colortable is 0, a color table sufficient for numColors will be allocated (and destructed later).

The endianness is specified by bitOrder.

Warning: This constructor is only available on Qt/Embedded.

### QImage::QImage ( const QImage & image )

Constructs a shallow copy of image.

### QImage::~QImage ()

Destroys the image and cleans up.

### bool QImage::allGray () const

Returns true if all the colors in the image are shades of gray (i.e. their red, green and blue components are equal); otherwise returns false.

This function is slow for large 16-bit (Qt/Embedded only) and 32-bit images.

See also isGrayscale().

### Endian QImage::bitOrder () const

Returns the bit order for the image.

If it is a 1-bpp image, this function returns either QImage::BigEndian or QImage::LittleEndian.

If it is not a 1-bpp image, this function returns QImage::IgnoreEndian.

See also depth().

### uchar * QImage::bits ()

Returns a pointer to the first pixel data. This is equivalent to scanLine(0).

See also numBytes(), scanLine(), and jumpTable().

### const uchar * QImage::bits () const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

### int QImage::bytesPerLine () const

Returns the number of bytes per image scanline. This is equivalent to numBytes()/height().

See also numBytes() and scanLine().

### QRgb QImage::color ( int i ) const

Returns the color in the color table at index i. The first color is at index 0.

A color value is an RGB triplet. Use the qRed(), qGreen() and qBlue() functions (defined in `qcolor.h`) to get the color value components.

See also setColor(), numColors(), and QColor.

### QRgb * QImage::colorTable ()

Returns a pointer to the color table.

See also numColors().

### const QRgb * QImage::colorTable () const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

### QImage QImage::convertBitOrder ( Endian bitOrder ) const

Converts the bit order of the image to bitOrder and returns the converted image. The original image is not changed.

Returns `*this` if the bitOrder is equal to the image bit order, or a null image if this image cannot be converted.

See also bitOrder(), systemBitOrder(), and isNull().

### QImage QImage::convertDepth ( int depth, Qt::ImageConversionFlags flags ) const

Converts the depth (bpp) of the image to depth and returns the converted image. The original image is not changed.

The depth argument must be 1, 8, 16 (Qt/Embedded only) or 32.

Returns `*this` if depth is equal to the image depth, or a null image if this image cannot be converted.

If the image needs to be modified to fit in a lower-resolution result (e.g. converting from 32-bit to 8-bit), use the flags to specify how you'd prefer this to happen.

See also Qt::ImageConversionFlags, depth(), and isNull().

### QImage QImage::convertDepth ( int depth ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

### QImage QImage::convertDepthWithPalette ( int d, QRgb * palette, int palette_count, Qt::ImageConversionFlags flags = Qt::AutoColor ) const

Returns an image with depth d, using the palette_count colors pointed to by palette. If d is 1 or 8, the returned image will have its color table ordered the same as palette.

If the image needs to be modified to fit in a lower-resolution result (e.g. converting from 32-bit to 8-bit), use the flags to specify how you'd prefer this to happen.

Note: currently no closest-color search is made. If colors are found that are not in the palette, the palette may not be used at all. This result should not be considered valid because it may change in future implementations.

Currently inefficient for non-32-bit images.

See also Qt::ImageConversionFlags.

### QImage QImage::copy () const

Returns a deep copy of the image.

See also detach().

### QImage QImage::copy ( int x, int y, int w, int h, Qt::ImageConversionFlags flags = Qt::AutoColor ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns a deep copy of a sub-area of the image.

The returned image is always w by h pixels in size, and is copied from position x, y in this image. In areas beyond this image pixels are filled with pixel 0.

If the image needs to be modified to fit in a lower-resolution result (e.g. converting from 32-bit to 8-bit), use the flags to specify how you'd prefer this to happen.

See also bitBlt() and Qt::ImageConversionFlags.

### QImage QImage::copy ( const QRect & r, Qt::ImageConversionFlags flags = Qt::AutoColor ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns a deep copy of a sub-area of the image.

The returned image always has the size of the rectangle r. In areas beyond this image pixels are filled with pixel 0.

If the image needs to be modified to fit in a lower-resolution result (e.g. converting from 32-bit to 8-bit), use the flags to specify how you'd prefer this to happen.

See also bitBlt() and Qt::ImageConversionFlags.

### bool QImage::create ( int width, int height, int depth, int numColors = 0, Endian bitOrder = IgnoreEndian )

Sets the image width, height, depth, its number of colors (in numColors), and bit order. Returns true if successful, or false if the parameters are incorrect or if memory cannot be allocated.

The width and height is limited to 32767. depth must be 1, 8, or 32. If depth is 1, bitOrder must be set to either QImage::LittleEndian or QImage::BigEndian. For other depths bitOrder must be QImage::IgnoreEndian.

This function allocates a color table and a buffer for the image data. The image data is not initialized.

The image buffer is allocated as a single block that consists of a table of scanline pointers (jumpTable()) and the image data (bits()).

jumpTable() scanLine() bits() bytesPerLine() numBytes()

See also fill(), width(), height(), depth(), numColors(), and bitOrder().

### bool QImage::create ( const QSize & size, int depth, int numColors = 0, Endian bitOrder = IgnoreEndian )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

The width and height are specified in the size argument.

### QImage QImage::createAlphaMask ( Qt::ImageConversionFlags flags = Qt::AutoColor ) const

Builds and returns a 1-bpp mask from the alpha buffer in this image. Returns a null image if alpha buffer mode is disabled.

See QPixmap::convertFromImage() for a description of the flags argument.

The returned image has little-endian bit order, which you can convert to big-endianness using convertBitOrder().

See also createHeuristicMask(), hasAlphaBuffer(), and setAlphaBuffer().

### QImage QImage::createHeuristicMask ( bool clipTight = true ) const

Creates and returns a 1-bpp heuristic mask for this image. It works by selecting a color from one of the corners, then chipping away pixels of that color starting at all the edges.

The four corners vote for which color is to be masked away. In case of a draw (this generally means that this function is not applicable to the image), the result is arbitrary.

The returned image has little-endian bit order, which you can convert to big-endianness using convertBitOrder().

If clipTight is true the mask is just large enough to cover the pixels; otherwise, the mask is larger than the data pixels.

This function disregards the alpha buffer.

See also createAlphaMask().

### int QImage::depth () const

Returns the depth of the image.

The image depth is the number of bits used to encode a single pixel, also called bits per pixel (bpp) or bit planes of an image.

The supported depths are 1, 8, 16 (Qt/Embedded only) and 32.

See also convertDepth().

### void QImage::detach ()

Detaches from shared image d and makes sure that this image is the only one referring to the data.

If multiple images share common data, this image makes a copy of the data and detaches itself from the sharing mechanism. Nothing is done if there is just a single reference.

See also copy().

### int QImage::dotsPerMeterX () const

Returns the number of pixels that fit horizontally in a physical meter. This and dotsPerMeterY() define the intended scale and aspect ratio of the image.

See also setDotsPerMeterX().

### int QImage::dotsPerMeterY () const

Returns the number of pixels that fit vertically in a physical meter. This and dotsPerMeterX() define the intended scale and aspect ratio of the image.

See also setDotsPerMeterY().

### void QImage::fill ( uint pixel )

Fills the entire image with the pixel value pixel.

If the depth of this image is 1, only the lowest bit is used. If you say fill(0), fill(2), etc., the image is filled with 0s. If you say fill(1), fill(3), etc., the image is filled with 1s. If the depth is 8, the lowest 8 bits are used.

If the depth is 32 and the image has no alpha buffer, the pixel value is written to each pixel in the image. If the image has an alpha buffer, only the 24 RGB bits are set and the upper 8 bits (alpha value) are left unchanged.

Note: QImage::pixel() returns the color of the pixel at the given coordinates; QColor::pixel() returns the pixel value of the underlying window system (essentially an index value), so normally you will want to use QImage::pixel() to use a color from an existing image or QColor::rgb() to use a specific color.

See also invertPixels(), depth(), hasAlphaBuffer(), and create().

### QImage QImage::fromData ( const uchar * data, int size, const char * format = 0 ) `[static]`

Constructs a QImage from the first size bytes of binary data in data. If the loading of the image failed, this object is a null image.

If format is specified, the loader attempts to read the image using the specified format. If format is not specified (which is the default), the loader reads a few bytes from the header to guess the file format.

The QImageReader documentation lists the supported image formats and explains how to add extra formats.

See also load(), save(), imageFormat(), QPixmap::loadFromData(), and QImageReader.

### QImage QImage::fromData ( const QByteArray & data, const char * format = 0 ) `[static]`

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Loads an image from the QByteArray data.

### bool QImage::hasAlphaBuffer () const

Returns true if alpha buffer mode is enabled; otherwise returns false.

See also setAlphaBuffer().

### int QImage::height () const

Returns the height of the image.

See also width(), size(), and rect().

### void QImage::invertPixels ( InvertMode mode = InvertRgb )

Inverts all pixel values in the image.

If the depth is 32: if mode is InvertRgba, the alpha bits are also inverted, otherwise they are left unchanged.

If the depth is not 32, the argument mode has no meaning. The default mode is InvertRgb, which leaves the alpha channel unchanged.

Note that inverting an 8-bit image means to replace all pixels using color index i with a pixel using color index 255 minus i. Similarly for a 1-bit image. The color table is not changed.

See also fill(), depth(), and hasAlphaBuffer().

### bool QImage::isDetached () const

Returns true if the image is detached; otherwise returns false.

See also detach().

### bool QImage::isGrayscale () const

For 16-bit (Qt/Embedded only) and 32-bit images, this function is equivalent to allGray().

For 8-bpp images, this function returns true if color(i) is QRgb(i,i,i) for all indexes of the color table; otherwise returns false.

See also allGray() and depth().

### bool QImage::isNull () const

Returns true if it is a null image; otherwise returns false.

A null image has all parameters set to zero and no allocated data.

### uchar ** QImage::jumpTable ()

Returns a pointer to the scanline pointer table.

This is the beginning of the data block for the image.

See also bits() and scanLine().

### const uchar * const * QImage::jumpTable () const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

### bool QImage::load ( const QString & fileName, const char * format = 0 )

Loads an image from the file fileName. Returns true if the image was successfully loaded; otherwise returns false.

If format is specified, the loader attempts to read the image using the specified format. If format is not specified (which is the default), the loader reads a few bytes from the header to guess the file format.

The QImageReader documentation lists the supported image formats and explains how to add extra formats.

The file name can be either refer to an actual file on disk or to one of the application's embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application's executable.

See also loadFromData(), save(), imageFormat(), QPixmap::load(), and QImageReader.

### bool QImage::loadFromData ( const uchar * data, int len, const char * format = 0 )

Loads an image from the first len bytes of binary data in data. Returns true if the image was successfully loaded; otherwise returns false.

If format is specified, the loader attempts to read the image using the specified format. If format is not specified (which is the default), the loader reads a few bytes from the header to guess the file format.

The QImageReader documentation lists the supported image formats and explains how to add extra formats.

See also load(), save(), imageFormat(), QPixmap::loadFromData(), and QImageReader.

### bool QImage::loadFromData ( const QByteArray & data, const char * format = 0 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Loads an image from the QByteArray data.

### int QImage::metric ( PaintDeviceMetric metric ) const `[virtual protected]`

### QImage QImage::mirror () const

Returns a QImage which is a vertically mirrored copy of this image. The original QImage is not changed.

### QImage QImage::mirror ( bool horizontal, bool vertical ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns a mirror of the image, mirrored in the horizontal and/or the vertical direction depending on whether horizontal and vertical are set to true or false. The original image is not changed.

See also scale().

### int QImage::numBytes () const

Returns the number of bytes occupied by the image data.

See also bytesPerLine() and bits().

### int QImage::numColors () const

Returns the size of the color table for the image.

Notice that numColors() returns 0 for 16-bpp (Qt/Embedded only) and 32-bpp images because these images do not use color tables, but instead encode pixel values as RGB triplets.

See also setNumColors() and colorTable().

### QPoint QImage::offset () const

Returns the number of pixels by which the image is intended to be offset by when positioning relative to other images.

### QRgb QImage::pixel ( int x, int y ) const

Returns the color of the pixel at the coordinates (x, y).

If (x, y) is not on the image, the results are undefined.

See also setPixel(), qRed(), qGreen(), qBlue(), and valid().

### int QImage::pixelIndex ( int x, int y ) const

Returns the pixel index at the given coordinates.

If (x, y) is not valid, or if the image is not a paletted image (depth() > 8), the results are undefined.

See also valid() and depth().

### QRect QImage::rect () const

Returns the enclosing rectangle (0, 0, width(), height()) of the image.

See also width(), height(), and size().

### void QImage::reset ()

Resets all image parameters and deallocates the image data.

### bool QImage::save ( const QString & fileName, const char * format, int quality = -1 ) const

Saves the image to the file fileName, using the image file format format and a quality factor of quality. quality must be in the range 0 to 100 or -1. Specify 0 to obtain small compressed files, 100 for large uncompressed files, and -1 (the default) to use the default settings.

Returns true if the image was successfully saved; otherwise returns false.

See also load(), loadFromData(), imageFormat(), QPixmap::save(), and QImageReader.

### bool QImage::save ( QIODevice * device, const char * format, int quality = -1 ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This function writes a QImage to the QIODevice, device. This can be used, for example, to save an image directly into a QByteArray:

        QImage image;
        QByteArray ba;
        QBuffer buffer(ba);
        buffer.open(QIODevice::WriteOnly);
        image.save(&buffer, "PNG"); // writes image into ba in PNG format

### QImage QImage::scale ( int w, int h, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation ) const

Returns a copy of the image scaled to a rectangle of width w and height h according to aspectRatioMode and transformMode.

- If aspectRatioMode is `Qt::IgnoreAspectRatio`, the image is scaled to (w, h).
- If aspectRatioMode is `Qt::KeepAspectRatio`, the image is scaled to a rectangle as large as possible inside (w, h), preserving the aspect ratio.
- If aspectRatioMode is `Qt::KeepAspectRatioByExpanding`, the image is scaled to a rectangle as small as possible outside (w, h), preserving the aspect ratio.

If either the width w or the height h is zero or negative, this function returns a null image.

See also scaleWidth(), scaleHeight(), and transform().

### QImage QImage::scale ( const QSize & size, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Scales the image to the given size, using the aspect ratio and transformation modes specified by aspectMode and transformMode.

### QImage QImage::scaleHeight ( int h ) const

Returns a scaled copy of the image. The returned image has a height of h pixels. This function automatically calculates the width of the image so that the ratio of the image is preserved.

If h is 0 or negative a null image is returned.

See also scale(), scaleWidth(), and transform().

### QImage QImage::scaleWidth ( int w ) const

Returns a scaled copy of the image. The returned image has a width of w pixels. This function automatically calculates the height of the image so that the ratio of the image is preserved.

If w is 0 or negative a null image is returned.

See also scale(), scaleHeight(), and transform().

### uchar * QImage::scanLine ( int i )

Returns a pointer to the pixel data at the scanline with index i. The first scanline is at index 0.

The scanline data is aligned on a 32-bit boundary.

Warning: If you are accessing 32-bpp image data, cast the returned pointer to `QRgb*` (QRgb has a 32-bit size) and use it to read/write the pixel value. You cannot use the `uchar*` pointer directly, because the pixel format depends on the byte order on the underlying platform. Hint: use qRed(), qGreen() and qBlue(), etc. (qcolor.h) to access the pixels.

Warning: If you are accessing 16-bpp image data, you must handle endianness yourself. (Qt/Embedded only)

See also bytesPerLine(), bits(), and jumpTable().

### const uchar * QImage::scanLine ( int i ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

### int QImage::serialNumber () const

Returns a number that uniquely identifies the contents of this QImage object. This means that multiple QImage objects can have the same serial number as long as they refer to the same contents.

A null image always have a serial number of 0.

An example of where this is useful is for caching QImages.

### void QImage::setAlphaBuffer ( bool enable )

Enables alpha buffer mode if enable is true, otherwise disables it. The default setting is disabled.

An 8-bpp image has 8-bit pixels. A pixel is an index into the color table, which contains 32-bit color values. In a 32-bpp image, the 32-bit pixels are the color values.

This 32-bit value is encoded as follows: The lower 24 bits are used for the red, green, and blue components. The upper 8 bits contain the alpha component.

The alpha component specifies the transparency of a pixel. 0 means completely transparent and 255 means opaque. The alpha component is ignored if you do not enable alpha buffer mode.

The alpha buffer is used to set a mask when a QImage is translated to a QPixmap.

See also hasAlphaBuffer() and createAlphaMask().

### void QImage::setColor ( int i, QRgb c )

Sets a color in the color table at index i to c.

A color value is an RGB triplet. Use the qRgb() function (defined in `qcolor.h`) to make RGB triplets.

See also color(), setNumColors(), and numColors().

### void QImage::setDotsPerMeterX ( int x )

Sets the value returned by dotsPerMeterX() to x.

### void QImage::setDotsPerMeterY ( int y )

Sets the value returned by dotsPerMeterY() to y.

### void QImage::setNumColors ( int numColors )

Resizes the color table to numColors colors.

If the color table is expanded all the extra colors will be set to black (RGB 0,0,0).

See also numColors(), color(), setColor(), and colorTable().

### void QImage::setOffset ( const QPoint & p )

Sets the value returned by offset() to p.

### void QImage::setPixel ( int x, int y, uint index_or_rgb )

Sets the pixel index or color at the coordinates (x, y) to index_or_rgb.

If (x, y) is not valid, the result is undefined.

If the image is a paletted image (depth() <= 8) and index_or_rgb >= numColors(), the result is undefined.

See also pixelIndex(), pixel(), qRgb(), qRgba(), and valid().

### void QImage::setText ( const char * key, const char * lang, const QString & s )

Records string s for the keyword key. The key should be a portable keyword recognizable by other software - some suggested values can be found in the PNG specification. s can be any text. lang should specify the language code (see RFC 1766) or 0.

### QSize QImage::size () const

Returns the size of the image, i.e. its width and height.

See also width(), height(), and rect().

### QImage QImage::swapRGB () const

Returns a QImage in which the values of the red and blue components of all pixels have been swapped, effectively converting an RGB image to a BGR image. The original QImage is not changed.

### Endian QImage::systemBitOrder () `[static]`

Determines the bit order of the display hardware. Returns QImage::LittleEndian (LSB first) or QImage::BigEndian (MSB first).

See also systemByteOrder().

### Endian QImage::systemByteOrder () `[static]`

Determines the host computer byte order. Returns QImage::LittleEndian (LSB first) or QImage::BigEndian (MSB first).

See also systemBitOrder().

### QString QImage::text ( const char * key, const char * lang = 0 ) const

Returns the string recorded for the keyword key in language lang, or in a default language if lang is 0.

### QString QImage::text ( const QImageTextKeyLang & kl ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns the string recorded for the keyword and language kl.

### QStringList QImage::textKeys () const

Returns the keywords for which some texts are recorded.

Note that if you want to iterate over the list, you should iterate over a copy, e.g.

        QStringList list = myImage.textKeys();
        QStringList::Iterator it = list.begin();
        while(it != list.end()) {
            myProcessing(*it);
            ++it;
        }

See also textList(), text(), setText(), and textLanguages().

### QStringList QImage::textLanguages () const

Returns the language identifiers for which some texts are recorded.

Note that if you want to iterate over the list, you should iterate over a copy, e.g.

        QStringList list = myImage.textLanguages();
        QStringList::Iterator it = list.begin();
        while(it != list.end()) {
            myProcessing(*it);
            ++it;
        }

See also textList(), text(), setText(), and textKeys().

### QList<QImageTextKeyLang> QImage::textList () const

Returns a list of QImageTextKeyLang objects that enumerate all the texts key/language pairs set by setText() for this image.

### QImage QImage::transform ( const QMatrix & matrix, Qt::TransformationMode mode = Qt::FastTransformation ) const

Returns a copy of the image that is transformed with the transformation matrix specified by matrix and using the transformation mode specified by mode.

The transformation matrix is internally adjusted to compensate for unwanted translation; i.e. the image produced is the smallest image that contains all the transformed points of the original image.

See also scale(), QPixmap::transform(), QPixmap::trueMatrix(), and QMatrix.

### QMatrix QImage::trueMatrix ( const QMatrix & matrix, int w, int h ) `[static]`

Returns the actual matrix used for transforming a image with w width and h height and matrix matrix.

When transforming a image with transform(), the transformation matrix is internally adjusted to compensate for unwanted translation, i.e. transform() returns the smallest image containing all transformed points of the original image.

This function returns the modified matrix, which maps points correctly from the original image into the new image.

See also transform() and QMatrix.

### bool QImage::valid ( int x, int y ) const

Returns true if (x, y) is a valid coordinate in the image; otherwise returns false.

See also width(), height(), and pixelIndex().

### int QImage::width () const

Returns the width of the image.

See also height(), size(), and rect().

### QImage::operator QVariant () const

Returns the image as a QVariant

### bool QImage::operator!= ( const QImage & i ) const

Returns true if this image and image i have different contents; otherwise returns false. The comparison can be slow, unless there is some obvious difference, such as different widths, in which case the function will return quickly.

See also operator=().

### QImage & QImage::operator= ( const QImage & image )

Assigns a shallow copy of image to this image and returns a reference to this image.

See also copy().

### bool QImage::operator== ( const QImage & i ) const

Returns true if this image and image i have the same contents; otherwise returns false. The comparison can be slow, unless there is some obvious difference, such as different widths, in which case the function will return quickly.

See also operator=().

## Related Non-Members

### void bitBlt ( QImage * dst, int dx, int dy, const QImage * src, int sx = 0, int sy = 0, int sw = -1, int sh = -1, Qt::ImageConversionFlags flags = Qt::AutoColor )

Copies a block of pixels from src to dst. The pixels copied from source (src) are converted according to flags if it is incompatible with the destination (dst).

sx, sy is the top-left pixel in src, dx, dy is the top-left position in dst and sw, sh is the size of the copied block.

The copying is clipped if areas outside src or dst are specified.

If sw is -1, it is adjusted to src->width(). Similarly, if sh is -1, it is adjusted to src->height().

Currently inefficient for non 32-bit images.

### QDataStream & operator<< ( QDataStream & s, const QImage & image )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Writes the image image to the stream s as a PNG image, or as a BMP image if the stream's version is 1.

Note that writing the stream to a file will not produce a valid image file.

Format of the QDataStream operators

See also QImage::save().

### QDataStream & operator>> ( QDataStream & s, QImage & image )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Reads an image from the stream s and stores it in image.

Format of the QDataStream operators

See also QImage::load().