Re: image resizing



On Fri, 2003-07-11 at 22:13, ritwik kumar wrote:
hey,
    how do i scale images to my desired size and then
display using GtkImage widget? othetwise on part of
the image shows when window is smaller than the image.
Please help.


ritwik

You must create/scale a pixbuf yourself:

GtkWidget *
get_image_from_file_at_size (const gchar *filename,
                             gint dest_width,
                             gint dest_height)
{
    GtkWidget *img;
    GdkPixbuf *raw, *display;
    GError *err = NULL;

    raw = gdk_pixbuf_new_from_file (filename, &err);
    /* The pixbuf could not be loaded */
    if (err != NULL)
    {
        g_warning ("Could not load file '%s': %s",
                   "/path/to/whatever.png", err->message);
        g_error_free (err);
        return NULL;
    }

    if (gdk_pixbuf_get_width (raw) > dest_width ||
        gdk_pixbuf_get_height (raw) > dest_height)
    {
        gdouble scale_x, scale_y;
        gdouble offset_x, offset_y;

        scale_x = gdk_pixbuf_get_width (raw) / (gdouble) dest_width;
        scale_y = gdk_pixbuf_get_height (raw) / (gdouble) dest_height;

        if (scale_y > scale_x)
            scale_y = scale_x;
        else if (scale_x > scale_y)
            scale_x = scale_y;

        offset_x = (scale_x * gdk_pixbuf_get_width (raw)) / 2.0;
        offset_y = (scale_y * gdk_pixbuf_get_height (raw)) / 2.0;

        display = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
                                  gdk_pixbuf_get_has_alpha (raw),
                                  8, dest_width, dest_height);

        gdk_pixbuf_scale (raw, display, 0, 0, dest_width, dest_height,
                          offset_x, offset_y, scale_x, scale_y,
                          GDK_INTERP_HYPER);

        g_object_unref (raw);
    }
    else
    {
        display = raw;
    }

    img = gtk_image_new_from_pixbuf (display);
    g_object_unref (display);

    return img;
}

Notes:
1.) This function will only scale an image down, not up.
2.) I'm not sure if this function will compile, not segv, and work
properly, but it looks like it should [i.e. I did no testing on this at
all, it's off the top of my head].
3.) The API docs for GdkPixbuf are at:
http://developer.gnome.org/doc/API/2.0/gdk-pixbuf/index.html

4.) Perhaps the GtkImage widget needs a "scaled" property -- to handle
automatically scaling the pixbuf to the widget size...? Anyone?
Developers? Bueller? :-)

-- 
Peace,

    Jim Cape
    http://ignore-your.tv

    "It is literally true that, like Christianity, Socialism
     has conquered the world by defeating itself."
        -- Alexander Berkman, ABC of Anarchism

Attachment: signature.asc
Description: This is a digitally signed message part



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]