GdkPixbuf 的图像控件(GTK+2.0学习笔记)

根据书中所介绍说明,GdkPixbuf在GTK+编程中还是很重要的,尤其是要做一个漂亮的程序界面,这部分知识必须要掌握。书中的程序示例代码,有这样一个代码段:

pix2 = gdk_pixbuf_new_from_inline(134400+24, pieces_inline,TRUE, NULL);
  frame = gtk_frame_new("内建的一幅图像");
  image = gtk_image_new_from_pixbuf(pix2);
其中蓝色的函数第一个参数,照抄教程的是不行的。上边的是我附件中的图像数据结构参数。此参数正确才能使图片正常显示。就是重点学习了解的地方。

在GTK+2.x的API手册中此函数说明如下:

GdkPixbuf*  gdk_pixbuf_new_from_inline  ( gint               data_length,
                                                                   const guint8  *data,
                                                                   gboolean      copy_pixels,
                                                                    GError          **error  );
Create a GdkPixbuf from a flat representation that is suitable for storing as inline data in a program. This is useful if you want to ship a program with images, but don't want to depend on any external files. 从程序内部存储的数据中的创建一个GdkPixbuf。如果你想程序中的图像不依赖于外部,这是很重要的。
GTK+ ships with a program called gdk-pixbuf-csource which allows for conversion of GdkPixbufs into such a inline representation. In almost all cases, you should pass the --raw flag to gdk-pixbuf-csource. A sample invocation would be: GTK+提供一个叫做gdk-pixbuf-csource 的程序,可以转化GdkPixbufs 程序成内联源代码格式。一般情况下应该使用-raw 参数,示例如下:
    gdk-pixbuf-csource --raw --name = myimage_inline myimage.png
For the typical case where the inline pixbuf is read-only static data, you don't need to copy the pixel data unless you intend to write to it, so you can pass FALSE for copy_pixels. (If you pass --rle to gdk-pixbuf-csource, a copy will be made even if copy_pixels is FALSE, so using this option is generally a bad idea.) 上述示例中内联pixbuf是静态只读数据,你不需复制像素数据内容,除非你打算对其重写,这样你将使copy_pixels无效。(如果你使用--rle参数,将使copy_pixels的结果无效,通常使用此参数选项不是好的主意)
If you create a pixbuf from const inline data compiled into your program, it's probably safe to ignore errors and disable length checks, since things will always succeed: 如果从你想编译到程序中的内联数据常量中创建一个pixbuf图像,它可以安全的忽略错误(图像长度),并废止长度检查,因为其总是会成功的:
pixbuf = gdk_pixbuf_new_from_inline (-1, myimage_inline, FALSE, NULL);
For non-const inline data, you could get out of memory. For untrusted inline data located at runtime, you could have corrupt inline data in addition. 对于非内嵌数据结构,你可以摆脱内存限制。内嵌不可信的数据在运行时,你可以使内置之外的数据作废。

data_length :
    Length in bytes of the data argument or -1 to disable length checks 
数据变量的长度(字节),或者“ -1 ” 不做数据长度检查。
data :
    Byte data containing a serialized GdkPixdata structure 
包含连续的GdkPixdata结构的数据
copy_pixels :
    Whether to copy the pixel data, or use direct pointers data for the resulting pixbuf
是使用拷贝的像素数据,或指针指向的数据,来生成图像pixbuf
error :
    GError return location, may be NULL to ignore errors
GError返回位置, 也可以使用NULL来忽略错误。
Returns :
返回:    A newly-created GdkPixbuf structure with a reference, count of 1, or NULL if an error occurred.
返回新建的GdkPixbuf结构参考,计数为1,或如果发生错误返回NULL。


将图像转换成C语言源代码格式(内容看教程)
    用Gtk+2.0自带的命令行工具gdk-pixbuf-csource来完成图像转换成C语言代码格式的数据,并保存到C语言的头文件中,关键是要理解下面的命令行:
gdk-pixbuf-csource --raw --name pieces_inline pieces.png > pieces.h
    将此命令将pieces.png图像转换成呢个C语言源代码格式并将数据结构命名为pieces_inline,结果输出到pieces.h文件中,打开pieces.h文件就可以看到这一数据结构了。还可以执行那个此命令带 --help选项来查看此命令的详细帮助。
生成头文件的内容还是要看的,这里书上的教程没有给详细说明。内容部分就不说了,我也没学到遇到呢,只说与此示例程序相关的,打开pieces.h文件后看的内容中如下部分:
/* GdkPixbuf RGB C-Source image dump */

#ifdef __SUNPRO_C
#pragma align 4 (pieces_inline)
#endif
#ifdef __GNUC__
static const guint8 pieces_inline[] __attribute__ ((__aligned__ (4))) = 
#else
static const guint8 pieces_inline[] = 
#endif
{ ""  
  /* Pixbuf magic (0x47646b50) */
  "GdkP"

/* length: header (24) + pixel_data (134400) */   /* 这里是程序代码用到图像大小*/
  "\0\2\15\30"
  /* pixdata_type (0x1010001) */
  "\1\1\0\1"
  /* rowstride (840) */
  "\0\0\3H"
  /* width (279) */ 
  "\0\0\1\27"
  /* height (160) */
  "\0\0\0\240"
  /* pixel_data: */

只要是个widget,它的widget->window就是一个drawing area,随便画吧~注意是在expose事件的回调函数中画~
Table of Contents I. API Reference Initialization and Versions - Library version numbers. The GdkPixbuf Structure - Information that describes an image. Reference Counting and Memory Mangement - Functions for reference counting and memory management on pixbufs. File Loading - Loading a pixbuf from a file. File saving - Saving a pixbuf to a file. Image Data in Memory - Creating a pixbuf from image data that is already in memory. Inline data - Functions for inlined pixbuf handling. Scaling - Scaling pixbufs and scaling and compositing pixbufs Rendering - Rendering a pixbuf to a GDK drawable. Drawables to Pixbufs - Getting parts of a GDK drawable's image data into a pixbuf. Utilities - Utility and miscellaneous convenience functions. Animations - Animated images. GdkPixbufLoader - Application-driven progressive image loading. Module Interface - Extending gdk-pixbuf gdk-pixbuf Xlib initialization - Initializing the gdk-pixbuf Xlib library. Xlib Rendering - Rendering a pixbuf to an X drawable. X Drawables to Pixbufs - Getting parts of an X drawable's image data into a pixbuf. XlibRGB - Rendering RGB buffers to X drawables. II. Tools Reference gdk-pixbuf-csource - C code generation utility for GdkPixbuf images gdk-pixbuf-query-loaders - GdkPixbuf loader registration utility Index Index of deprecated symbols Index of new symbols in 2.2 Index of new symbols in 2.4 Index of new symbols in 2.6 Index of new symbols in 2.8 A. Porting applications from Imlib to gdk-pixbuf Introduction Differences between Imlib and gdk-pixbuf Initialization Memory management The Rendering Process Converting Applications to gdk-pixbuf Image loading and creation Rendering Images Scaling Images
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值