May 6,2009

gtk 載入並顯示圖片

實際上是用 GDK+GtkDrawingArea 來畫,所以在下面的程式片斷,你會看到我宣告了 GtkDrawingArea 並且實作了 GtkDrawingArea 的 expose 事件。 GDK 支援的圖片格式很多,常見的 jpg、png、bmp 都沒問題。
static gboolean expose_event( GtkWidget* widget, GdkEventExpose* event, gpointer data )
{
	GError* error=NULL;
	int width=widget->allocation.width, height=widget->allocation.height;

	GdkPixbuf* buf=gdk_pixbuf_new_from_file_at_scale( "your_photo.jpg", &error );
	if( buf==NULL )
		g_print("load fail.\n" );
	else
	{
		bufWidth = gdk_pixbuf_get_width( buf );
		bufHeight = gdk_pixbuf_get_height( buf );
		gdk_draw_pixbuf( widget->window, NULL, buf, 0, 0, 0, 0, 
				(width>bufWidth?bufWidth:width), (height>bufHeight?bufHeight:height), 
				GDK_RGB_DITHER_NORMAL, 0, 0 );
		g_object_unref( buf );
	}
}

int main( int argc, char* argv[])
{
	GtkWidget* drawing_area=NULL;

	// 省略一萬行
	g_signal_connect( G_OBJECT(drawing_area), "expose_event", G_CALLBACK( expose_event ), NULL );

	// 再省略兩萬行...	
}


Posted by elleryq at 樂多Roodo! │19:14 │回應(0)引用(0)C/C++
樂多分類:網路/3C 共同主題:C/C++ 工具:編輯本文
標籤:linux,gtk,c
Ads by Roodo! 

引用URL

http://cgi.blog.roodo.com/trackback/8881399