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

来源:互联网 发布:什么评价会被淘宝删除 编辑:程序博客网 时间:2024/06/05 20:13
根据书中所介绍说明,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事件的回调函数中画~
原创粉丝点击