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 );
// 再省略兩萬行...
}
沒有留言:
張貼留言