File Loading: GDK-PixBuf Reference Manual

## File Loading File Loading — Loading a pixbuf from a file. |

## Functions

GdkPixbuf * | gdk_pixbuf_new_from_file ()
GdkPixbuf * | gdk_pixbuf_new_from_file_at_size ()
GdkPixbuf * | gdk_pixbuf_new_from_file_at_scale ()
GdkPixbufFormat * | gdk_pixbuf_get_file_info ()
void | gdk_pixbuf_get_file_info_async ()
GdkPixbufFormat * | gdk_pixbuf_get_file_info_finish ()
GdkPixbuf * | gdk_pixbuf_new_from_resource ()
GdkPixbuf * | gdk_pixbuf_new_from_resource_at_scale ()
GdkPixbuf * | gdk_pixbuf_new_from_stream ()
void | gdk_pixbuf_new_from_stream_async ()
GdkPixbuf * | gdk_pixbuf_new_from_stream_finish ()
GdkPixbuf * | gdk_pixbuf_new_from_stream_at_scale ()
void | gdk_pixbuf_new_from_stream_at_scale_async ()

## Includes

    #include <gdk-pixbuf/gdk-pixbuf.h>

## Description

The GdkPixBuf library provides a simple mechanism for loading an image from a file in synchronous fashion. This means that the library takes control of the application while the file is being loaded; from the user's point of view, the application will block until the image is done loading.

This interface can be used by applications in which blocking is acceptable while an image is being loaded. It can also be used to load small images in general. Applications that need progressive loading can use the GdkPixbufLoader functionality instead.

## Functions

### gdk_pixbuf_new_from_file ()

    GdkPixbuf *
    gdk_pixbuf_new_from_file (const char *filename,
                              GError **error);

Creates a new pixbuf by loading an image from a file. The file format is detected automatically. If `NULL` is returned, then `error` will be set. Possible errors are in the GDK_PIXBUF_ERROR and G_FILE_ERROR domains.

#### Parameters

filename | Name of file to load, in the GLib file name encoding |
error | Return location for an error |

#### Returns

A newly-created pixbuf with a reference count of 1, or `NULL` if any of several error conditions occurred: the file could not be opened, there was no loader for the file's format, there was not enough memory to allocate the image buffer, or the image file contained invalid data.

### gdk_pixbuf_new_from_file_at_size ()

    GdkPixbuf *
    gdk_pixbuf_new_from_file_at_size (const char *filename,
                                      int width,
                                      int height,
                                      GError **error);

Creates a new pixbuf by loading an image from a file. The file format is detected automatically. If `NULL` is returned, then `error` will be set. Possible errors are in the GDK_PIXBUF_ERROR and G_FILE_ERROR domains.

The image will be scaled to fit in the requested size, preserving the image's aspect ratio. Note that the returned pixbuf may be smaller than `width` x `height` , if the aspect ratio requires it. To load and image at the requested size, regardless of aspect ratio, use `gdk_pixbuf_new_from_file_at_scale()`.

#### Parameters

filename | Name of file to load, in the GLib file name encoding |
width | The width the image should have or -1 to not constrain the width |
height | The height the image should have or -1 to not constrain the height |
error | Return location for an error |

#### Returns

A newly-created pixbuf with a reference count of 1, or `NULL` if any of several error conditions occurred: the file could not be opened, there was no loader for the file's format, there was not enough memory to allocate the image buffer, or the image file contained invalid data.

Since: 2.4

### gdk_pixbuf_new_from_file_at_scale ()

    GdkPixbuf *
    gdk_pixbuf_new_from_file_at_scale (const char *filename,
                                       int width,
                                       int height,
                                       gboolean preserve_aspect_ratio,
                                       GError **error);

Creates a new pixbuf by loading an image from a file. The file format is detected automatically. If `NULL` is returned, then `error` will be set. Possible errors are in the GDK_PIXBUF_ERROR and G_FILE_ERROR domains. The image will be scaled to fit in the requested size, optionally preserving the image's aspect ratio.

When preserving the aspect ratio, a `width` of -1 will cause the image to be scaled to the exact given height, and a `height` of -1 will cause the image to be scaled to the exact given width. When not preserving aspect ratio, a `width` or `height` of -1 means to not scale the image at all in that dimension. Negative values for `width` and `height` are allowed since 2.8.

#### Parameters

filename | Name of file to load, in the GLib file name encoding |
width | The width the image should have or -1 to not constrain the width |
height | The height the image should have or -1 to not constrain the height |
preserve_aspect_ratio | `TRUE` to preserve the image's aspect ratio |
error | Return location for an error |

#### Returns

A newly-created pixbuf with a reference count of 1, or `NULL` if any of several error conditions occurred: the file could not be opened, there was no loader for the file's format, there was not enough memory to allocate the image buffer, or the image file contained invalid data.

Since: 2.6

### gdk_pixbuf_get_file_info ()

    GdkPixbufFormat *
    gdk_pixbuf_get_file_info (const gchar *filename,
                              gint *width,
                              gint *height);

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

#### Parameters

filename | The name of the file to identify. |
width | Return location for the width of the image, or `NULL`. | [optional][out]
height | Return location for the height of the image, or `NULL`. | [optional][out]

#### Returns

A GdkPixbufFormat describing the image format of the file or `NULL` if the image format wasn't recognized. The return value is owned by GdkPixbuf and should not be freed.

[nullable][transfer none]

Since: 2.4

### gdk_pixbuf_get_file_info_async ()

    void
    gdk_pixbuf_get_file_info_async (const gchar *filename,
                                    GCancellable *cancellable,
                                    GAsyncReadyCallback callback,
                                    gpointer user_data);

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

For more details see `gdk_pixbuf_get_file_info()`, which is the synchronous version of this function.

When the operation is finished, `callback` will be called in the main thread. You can then call `gdk_pixbuf_get_file_info_finish()` to get the result of the operation.

#### Parameters

filename | The name of the file to identify |
cancellable | optional GCancellable object, `NULL` to ignore. | [allow-none]
callback | a GAsyncReadyCallback to call when the the pixbuf is loaded |
user_data | the data to pass to the callback function |

Since: 2.32

### gdk_pixbuf_get_file_info_finish ()

    GdkPixbufFormat *
    gdk_pixbuf_get_file_info_finish (GAsyncResult *async_result,
                                     gint *width,
                                     gint *height,
                                     GError **error);

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

#### Parameters

async_result | a GAsyncResult |
width | Return location for the width of the image, or `NULL`. | [out]
height | Return location for the height of the image, or `NULL`. | [out]
error | a GError, or `NULL` |

#### Returns

A GdkPixbufFormat describing the image format of the file or `NULL` if the image format wasn't recognized. The return value is owned by GdkPixbuf and should not be freed.

[transfer none]

Since: 2.32

### gdk_pixbuf_new_from_resource ()

    GdkPixbuf *
    gdk_pixbuf_new_from_resource (const char *resource_path,
                                  GError **error);

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

The file format is detected automatically. If `NULL` is returned, then `error` will be set.

#### Parameters

resource_path | the path of the resource file |
error | Return location for an error |

#### Returns

A newly-created pixbuf, or `NULL` if any of several error conditions occurred: the file could not be opened, the image format is not supported, there was not enough memory to allocate the image buffer, the stream contained invalid data, or the operation was cancelled.

Since: 2.26

### gdk_pixbuf_new_from_resource_at_scale ()

    GdkPixbuf *
    gdk_pixbuf_new_from_resource_at_scale (const char *resource_path,
                                           int width,
                                           int height,
                                           gboolean preserve_aspect_ratio,
                                           GError **error);

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

The file format is detected automatically. If `NULL` is returned, then `error` will be set.

The image will be scaled to fit in the requested size, optionally preserving the image's aspect ratio. When preserving the aspect ratio, a `width` of -1 will cause the image to be scaled to the exact given height, and a `height` of -1 will cause the image to be scaled to the exact given width. When not preserving aspect ratio, a `width` or `height` of -1 means to not scale the image at all in that dimension.

The stream is not closed.

#### Parameters

resource_path | the path of the resource file |
width | The width the image should have or -1 to not constrain the width |
height | The height the image should have or -1 to not constrain the height |
preserve_aspect_ratio | `TRUE` to preserve the image's aspect ratio |
error | Return location for an error |

#### Returns

A newly-created pixbuf, or `NULL` if any of several error conditions occurred: the file could not be opened, the image format is not supported, there was not enough memory to allocate the image buffer, the stream contained invalid data, or the operation was cancelled.

Since: 2.26

### gdk_pixbuf_new_from_stream ()

    GdkPixbuf *
    gdk_pixbuf_new_from_stream (GInputStream *stream,
                                GCancellable *cancellable,
                                GError **error);

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

The file format is detected automatically. If `NULL` is returned, then `error` will be set. The `cancellable` can be used to abort the operation from another thread. If the operation was cancelled, the error `G_IO_ERROR_CANCELLED` will be returned. Other possible errors are in the GDK_PIXBUF_ERROR and `G_IO_ERROR` domains.

The stream is not closed.

#### Parameters

stream | a GInputStream to load the pixbuf from |
cancellable | optional GCancellable object, `NULL` to ignore. | [allow-none]
error | Return location for an error |

#### Returns

A newly-created pixbuf, or `NULL` if any of several error conditions occurred: the file could not be opened, the image format is not supported, there was not enough memory to allocate the image buffer, the stream contained invalid data, or the operation was cancelled.

Since: 2.14

### gdk_pixbuf_new_from_stream_async ()

    void
    gdk_pixbuf_new_from_stream_async (GInputStream *stream,
                                      GCancellable *cancellable,
                                      GAsyncReadyCallback callback,
                                      gpointer user_data);

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

For more details see `gdk_pixbuf_new_from_stream()`, which is the synchronous version of this function.

When the operation is finished, `callback` will be called in the main thread. You can then call `gdk_pixbuf_new_from_stream_finish()` to get the result of the operation.

#### Parameters

stream | a GInputStream from which to load the pixbuf |
cancellable | optional GCancellable object, `NULL` to ignore. | [allow-none]
callback | a GAsyncReadyCallback to call when the the pixbuf is loaded |
user_data | the data to pass to the callback function |

Since: 2.24

### gdk_pixbuf_new_from_stream_finish ()

    GdkPixbuf *
    gdk_pixbuf_new_from_stream_finish (GAsyncResult *async_result,
                                       GError **error);

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

#### Parameters

async_result | a GAsyncResult |
error | a GError, or `NULL` |

#### Returns

a GdkPixbuf or `NULL` on error. Free the returned object with `g_object_unref()`.

Since: 2.24

### gdk_pixbuf_new_from_stream_at_scale ()

    GdkPixbuf *
    gdk_pixbuf_new_from_stream_at_scale (GInputStream *stream,
                                         gint width,
                                         gint height,
                                         gboolean preserve_aspect_ratio,
                                         GCancellable *cancellable,
                                         GError **error);

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

The file format is detected automatically. If `NULL` is returned, then `error` will be set. The `cancellable` can be used to abort the operation from another thread. If the operation was cancelled, the error `G_IO_ERROR_CANCELLED` will be returned. Other possible errors are in the GDK_PIXBUF_ERROR and `G_IO_ERROR` domains.

The image will be scaled to fit in the requested size, optionally preserving the image's aspect ratio.

When preserving the aspect ratio, a `width` of -1 will cause the image to be scaled to the exact given height, and a `height` of -1 will cause the image to be scaled to the exact given width. If both `width` and `height` are given, this function will behave as if the smaller of the two values is passed as -1.

When not preserving aspect ratio, a `width` or `height` of -1 means to not scale the image at all in that dimension.

The stream is not closed.

#### Parameters

stream | a GInputStream to load the pixbuf from |
width | The width the image should have or -1 to not constrain the width |
height | The height the image should have or -1 to not constrain the height |
preserve_aspect_ratio | `TRUE` to preserve the image's aspect ratio |
cancellable | optional GCancellable object, `NULL` to ignore. | [allow-none]
error | Return location for an error |

#### Returns

A newly-created pixbuf, or `NULL` if any of several error conditions occurred: the file could not be opened, the image format is not supported, there was not enough memory to allocate the image buffer, the stream contained invalid data, or the operation was cancelled.

Since: 2.14

### gdk_pixbuf_new_from_stream_at_scale_async ()

    void
    gdk_pixbuf_new_from_stream_at_scale_async
                                   (GInputStream *stream,
                                    gint width,
                                    gint height,
                                    gboolean preserve_aspect_ratio,
                                    GCancellable *cancellable,
                                    GAsyncReadyCallback callback,
                                    gpointer user_data);

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

For more details see `gdk_pixbuf_new_from_stream_at_scale()`, which is the synchronous version of this function.

When the operation is finished, `callback` will be called in the main thread. You can then call `gdk_pixbuf_new_from_stream_finish()` to get the result of the operation.

#### Parameters

stream | a GInputStream from which to load the pixbuf |
width | the width the image should have or -1 to not constrain the width |
height | the height the image should have or -1 to not constrain the height |
preserve_aspect_ratio | `TRUE` to preserve the image's aspect ratio |
cancellable | optional GCancellable object, `NULL` to ignore. | [allow-none]
callback | a GAsyncReadyCallback to call when the the pixbuf is loaded |
user_data | the data to pass to the callback function |

Since: 2.24

## Types and Values

## See Also

GdkPixbufLoader.