### Class

# GdkPixbuf Pixbuf

#### Description [src]

    final class GdkPixbuf.Pixbuf : GObject.Object
      implements Gio.Icon, Gio.LoadableIcon {
      /* No available fields */
    }

A pixel buffer.

`GdkPixbuf` contains information about an image’s pixel data, its color space, bits per sample, width and height, and the rowstride (the number of bytes between the start of one row and the start of the next).

## Creating new `GdkPixbuf`

The most basic way to create a pixbuf is to wrap an existing pixel buffer with a `GdkPixbuf` instance. You can use the `gdk_pixbuf_new_from_data()` function to do this.

Every time you create a new `GdkPixbuf` instance for some data, you will need to specify the destroy notification function that will be called when the data buffer needs to be freed; this will happen when a `GdkPixbuf` is finalized by the reference counting functions. If you have a chunk of static data compiled into your application, you can pass in `NULL` as the destroy notification function so that the data will not be freed.

The `gdk_pixbuf_new()` constructor function can be used as a convenience to create a pixbuf with an empty buffer; this is equivalent to allocating a data buffer using `malloc()` and then wrapping it with `gdk_pixbuf_new_from_data()`. The `gdk_pixbuf_new()` function will compute an optimal rowstride so that rendering can be performed with an efficient algorithm.

You can also copy an existing pixbuf with the `gdk_pixbuf_copy()` function. This is not the same as just acquiring a reference to the old pixbuf instance: the copy function will actually duplicate the pixel data in memory and create a new `GdkPixbuf` instance for it.

## Reference counting

`GdkPixbuf` structures are reference counted. This means that an application can share a single pixbuf among many parts of the code. When a piece of the program needs to use a pixbuf, it should acquire a reference to it by calling `g_object_ref()`; when it no longer needs the pixbuf, it should release the reference it acquired by calling `g_object_unref()`. The resources associated with a `GdkPixbuf` will be freed when its reference count drops to zero. Newly-created `GdkPixbuf` instances start with a reference count of one.

## Image Data

Image data in a pixbuf is stored in memory in an uncompressed, packed format. Rows in the image are stored top to bottom, and in each row pixels are stored from left to right.

There may be padding at the end of a row.

The “rowstride” value of a pixbuf, as returned by `gdk_pixbuf_get_rowstride()`, indicates the number of bytes between rows.

NOTE: If you are copying raw pixbuf data with `memcpy()` note that the last row in the pixbuf may not be as wide as the full rowstride, but rather just as wide as the pixel data needs to be; that is: it is unsafe to do `memcpy (dest, pixels, rowstride * height)` to copy a whole pixbuf. Use `gdk_pixbuf_copy()` instead, or compute the width in bytes of the last row as:

    last_row = width * ((n_channels * bits_per_sample + 7) / 8);

The same rule applies when iterating over each row of a `GdkPixbuf` pixels array.

The following code illustrates a simple `put_pixel()` function for RGB pixbufs with 8 bits per channel with an alpha channel.

    static void
    put_pixel (GdkPixbuf *pixbuf,
               int x,
           int y,
           guchar red,
           guchar green,
           guchar blue,
           guchar alpha)
    {
      int n_channels = gdk_pixbuf_get_n_channels (pixbuf);

      // Ensure that the pixbuf is valid
      g_assert (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
      g_assert (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
      g_assert (gdk_pixbuf_get_has_alpha (pixbuf));
      g_assert (n_channels == 4);

      int width = gdk_pixbuf_get_width (pixbuf);
      int height = gdk_pixbuf_get_height (pixbuf);

      // Ensure that the coordinates are in a valid range
      g_assert (x >= 0 && x < width);
      g_assert (y >= 0 && y < height);

      int rowstride = gdk_pixbuf_get_rowstride (pixbuf);

      // The pixel buffer in the GdkPixbuf instance
      guchar *pixels = gdk_pixbuf_get_pixels (pixbuf);

      // The pixel we wish to modify
      guchar *p = pixels + y * rowstride + x * n_channels;
      p[0] = red;
      p[1] = green;
      p[2] = blue;
      p[3] = alpha;
    }

## Loading images

The `GdkPixBuf` class provides a simple mechanism for loading an image from a file in synchronous and asynchronous fashion.

For GUI applications, it is recommended to use the asynchronous stream API to avoid blocking the control flow of the application.

Additionally, `GdkPixbuf` provides the `GdkPixbufLoader` API for progressive image loading.

## Saving images

The `GdkPixbuf` class provides methods for saving image data in a number of file formats. The formatted data can be written to a file or to a memory buffer. `GdkPixbuf` can also call a user-defined callback on the data, which allows to e.g. write the image to a socket or store it in a database.

#### Ancestors

- GObject

#### Implements

- GIcon
- GLoadableIcon

#### Constructors

###### gdk_pixbuf_new

Creates a new `GdkPixbuf` structure and allocates a buffer for it.

###### gdk_pixbuf_new_from_bytes

Creates a new `GdkPixbuf` out of in-memory readonly image data.

since: 2.32

###### gdk_pixbuf_new_from_data

Creates a new `GdkPixbuf` out of in-memory image data.

###### gdk_pixbuf_new_from_file

Creates a new pixbuf by loading an image from a file.

###### gdk_pixbuf_new_from_file_at_scale

Creates a new pixbuf by loading an image from a file.

since: 2.6

###### gdk_pixbuf_new_from_file_at_size

Creates a new pixbuf by loading an image from a file.

since: 2.4

###### gdk_pixbuf_new_from_inline

Creates a `GdkPixbuf` from a flat representation that is suitable for storing as inline data in a program.

deprecated: 2.32

###### gdk_pixbuf_new_from_resource

Creates a new pixbuf by loading an image from an resource.

since: 2.26

###### gdk_pixbuf_new_from_resource_at_scale

Creates a new pixbuf by loading an image from an resource.

since: 2.26

###### gdk_pixbuf_new_from_stream

Creates a new pixbuf by loading an image from an input stream.

since: 2.14

###### gdk_pixbuf_new_from_stream_at_scale

Creates a new pixbuf by loading an image from an input stream.

since: 2.14

###### gdk_pixbuf_new_from_stream_finish

Finishes an asynchronous pixbuf creation operation started with gdk_pixbuf_new_from_stream_async().

since: 2.24

###### gdk_pixbuf_new_from_xpm_data

Creates a new pixbuf by parsing XPM data in memory.

deprecated: 2.44

#### Functions

###### gdk_pixbuf_calculate_rowstride

Calculates the rowstride that an image created with those values would have.

since: 2.36.8

###### gdk_pixbuf_get_file_info

Parses an image file far enough to determine its format and size.

since: 2.4

###### gdk_pixbuf_get_file_info_async

Asynchronously parses an image file far enough to determine its format and size.

since: 2.32

###### gdk_pixbuf_get_file_info_finish

Finishes an asynchronous pixbuf parsing operation started with gdk_pixbuf_get_file_info_async().

since: 2.32

###### gdk_pixbuf_get_formats

Obtains the available information about the image formats supported by GdkPixbuf.

since: 2.2

###### gdk_pixbuf_init_modules

Initalizes the gdk-pixbuf loader modules referenced by the `loaders.cache` file present inside that directory.

since: 2.40

###### gdk_pixbuf_new_from_stream_async

Creates a new pixbuf by asynchronously loading an image from an input stream.

since: 2.24

###### gdk_pixbuf_new_from_stream_at_scale_async

Creates a new pixbuf by asynchronously loading an image from an input stream.

since: 2.24

###### gdk_pixbuf_save_to_stream_finish

Finishes an asynchronous pixbuf save operation started with gdk_pixbuf_save_to_stream_async().

since: 2.24

#### Instance methods

###### gdk_pixbuf_add_alpha

Takes an existing pixbuf and adds an alpha channel to it.

###### gdk_pixbuf_apply_embedded_orientation

Takes an existing pixbuf and checks for the presence of an associated “orientation” option.

since: 2.12

###### gdk_pixbuf_composite

Creates a transformation of the source image `src` by scaling by `scale_x` and `scale_y` then translating by `offset_x` and `offset_y`.

###### gdk_pixbuf_composite_color

Creates a transformation of the source image `src` by scaling by `scale_x` and `scale_y` then translating by `offset_x` and `offset_y`, then alpha blends the rectangle (`dest_x` ,`dest_y`, `dest_width`, `dest_height`) of the resulting image with a checkboard of the colors `color1` and `color2` and renders it onto the destination image.

###### gdk_pixbuf_composite_color_simple

Creates a new pixbuf by scaling `src` to `dest_width` x `dest_height` and alpha blending the result with a checkboard of colors `color1` and `color2`.

###### gdk_pixbuf_copy

Creates a new `GdkPixbuf` with a copy of the information in the specified `pixbuf`.

###### gdk_pixbuf_copy_area

Copies a rectangular area from `src_pixbuf` to `dest_pixbuf`.

###### gdk_pixbuf_copy_options

Copies the key/value pair options attached to a `GdkPixbuf` to another `GdkPixbuf`.

since: 2.36

###### gdk_pixbuf_fill

Clears a pixbuf to the given RGBA value, converting the RGBA value into the pixbuf’s pixel format.

###### gdk_pixbuf_flip

Flips a pixbuf horizontally or vertically and returns the result in a new pixbuf.

since: 2.6

###### gdk_pixbuf_get_bits_per_sample

Queries the number of bits per color sample in a pixbuf.

###### gdk_pixbuf_get_byte_length

Returns the length of the pixel data, in bytes.

since: 2.26

###### gdk_pixbuf_get_colorspace

Queries the color space of a pixbuf.

###### gdk_pixbuf_get_has_alpha

Queries whether a pixbuf has an alpha channel (opacity information).

###### gdk_pixbuf_get_height

Queries the height of a pixbuf.

###### gdk_pixbuf_get_n_channels

Queries the number of channels of a pixbuf.

###### gdk_pixbuf_get_option

Looks up `key` in the list of options that may have been attached to the `pixbuf` when it was loaded, or that may have been attached by another function using gdk_pixbuf_set_option().

###### gdk_pixbuf_get_options

Returns a `GHashTable` with a list of all the options that may have been attached to the `pixbuf` when it was loaded, or that may have been attached by another function using `gdk_pixbuf_set_option()`.

since: 2.32

###### gdk_pixbuf_get_pixels

Queries a pointer to the pixel data of a pixbuf.

###### gdk_pixbuf_get_pixels_with_length

Queries a pointer to the pixel data of a pixbuf.

since: 2.26

###### gdk_pixbuf_get_rowstride

Queries the rowstride of a pixbuf, which is the number of bytes between the start of a row and the start of the next row.

###### gdk_pixbuf_get_width

Queries the width of a pixbuf.

###### gdk_pixbuf_new_subpixbuf

Creates a new pixbuf which represents a sub-region of `src_pixbuf`.

###### gdk_pixbuf_read_pixel_bytes

Provides a `GBytes` buffer containing the raw pixel data; the data must not be modified.

since: 2.32

###### gdk_pixbuf_read_pixels

Provides a read-only pointer to the raw pixel data.

since: 2.32

###### gdk_pixbuf_ref

Adds a reference to a pixbuf.

deprecated: 2.0

###### gdk_pixbuf_remove_option

Removes the key/value pair option attached to a `GdkPixbuf`.

since: 2.36

###### gdk_pixbuf_rotate_simple

Rotates a pixbuf by a multiple of 90 degrees, and returns the result in a new pixbuf.

since: 2.6

###### gdk_pixbuf_saturate_and_pixelate

Modifies saturation and optionally pixelates `src`, placing the result in `dest`.

###### gdk_pixbuf_save

Saves pixbuf to a file in format `type`. By default, “jpeg”, “png”, “ico” and “bmp” are possible file formats to save in, but more formats may be installed. The list of all writable formats can be determined in the following way:.

###### gdk_pixbuf_save_to_buffer

Saves pixbuf to a new buffer in format `type`, which is currently “jpeg”, “png”, “tiff”, “ico” or “bmp”.

since: 2.4

###### gdk_pixbuf_save_to_bufferv

Vector version of `gdk_pixbuf_save_to_buffer()`.

since: 2.4

###### gdk_pixbuf_save_to_callback

Saves pixbuf in format `type` by feeding the produced data to a callback.

since: 2.4

###### gdk_pixbuf_save_to_callbackv

Vector version of `gdk_pixbuf_save_to_callback()`.

since: 2.4

###### gdk_pixbuf_save_to_stream

Saves `pixbuf` to an output stream.

since: 2.14

###### gdk_pixbuf_save_to_stream_async

Saves `pixbuf` to an output stream asynchronously.

since: 2.24

###### gdk_pixbuf_save_to_streamv

Saves `pixbuf` to an output stream.

since: 2.36

###### gdk_pixbuf_save_to_streamv_async

Saves `pixbuf` to an output stream asynchronously.

since: 2.36

###### gdk_pixbuf_savev

Vector version of `gdk_pixbuf_save()`.

###### gdk_pixbuf_scale

Creates a transformation of the source image `src` by scaling by `scale_x` and `scale_y` then translating by `offset_x` and `offset_y`, then renders the rectangle (`dest_x`, `dest_y`, `dest_width`, `dest_height`) of the resulting image onto the destination image replacing the previous contents.

###### gdk_pixbuf_scale_simple

Create a new pixbuf containing a copy of `src` scaled to `dest_width` x `dest_height`.

###### gdk_pixbuf_set_option

Attaches a key/value pair as an option to a `GdkPixbuf`.

since: 2.2

###### gdk_pixbuf_unref

Removes a reference from a pixbuf.

deprecated: 2.0

##### Methods inherited from GObject (43)

Please see GObject for a full list of methods.

##### Methods inherited from GIcon (4)

###### g_icon_equal

Checks if two icons are equal.

###### g_icon_hash

Gets a hash for an icon.

###### g_icon_serialize

Serializes a `GIcon` into a `GVariant`. An equivalent `GIcon` can be retrieved back by calling `g_icon_deserialize()` on the returned value. As serialization will avoid using raw icon data when possible, it only makes sense to transfer the `GVariant` between processes on the same machine, (as opposed to over the network), and within the same file system namespace.

###### g_icon_to_string

Generates a textual representation of `icon` that can be used for serialization such as when passing `icon` to a different process or saving it to persistent storage. Use `g_icon_new_for_string()` to get `icon` back from the returned string.

##### Methods inherited from GLoadableIcon (3)

###### g_loadable_icon_load

Loads a loadable icon. For the asynchronous version of this function, see g_loadable_icon_load_async().

###### g_loadable_icon_load_async

Loads an icon asynchronously. To finish this function, see g_loadable_icon_load_finish(). For the synchronous, blocking version of this function, see g_loadable_icon_load().

###### g_loadable_icon_load_finish

Finishes an asynchronous icon load started in g_loadable_icon_load_async().

#### Properties

###### GdkPixbuf.Pixbuf:bits-per-sample

The number of bits per sample.

###### GdkPixbuf.Pixbuf:colorspace

The color space of the pixbuf.

###### GdkPixbuf.Pixbuf:has-alpha

Whether the pixbuf has an alpha channel.

###### GdkPixbuf.Pixbuf:height

The number of rows of the pixbuf.

###### GdkPixbuf.Pixbuf:n-channels

The number of samples per pixel.

###### GdkPixbuf.Pixbuf:pixel-bytes

No description available.

###### GdkPixbuf.Pixbuf:pixels

A pointer to the pixel data of the pixbuf.

###### GdkPixbuf.Pixbuf:rowstride

The number of bytes between the start of a row and the start of the next row.

###### GdkPixbuf.Pixbuf:width

The number of columns of the pixbuf.

#### Signals

##### Signals inherited from GObject (1)

###### GObject::notify

The notify signal is emitted on an object when one of its properties has its value set through g_object_set_property(), g_object_set(), et al.