我有一个 C 程序,它基本上将图像读入 char 缓冲区,在将图像显示回屏幕之前对其像素执行一些操作。
到目前为止,图像显示正常;但我遇到了以下两个问题:
- 当我最小化 GTK 窗口并再次打开它时,图像就消失了,使 Drawing_Area 为空。
- 如何通过按下按钮重绘/更新 Drawing_Area 中的图像?
这是进行绘图的函数:
void Draw_Img(GtkWidget *widget, unsigned char **buffer,int x_offset,int y_offset,int x1, int y1,int x2, int y2)
{
int i,j;
int width,height;
width=x2-x1+1;
height=y2-y1+1;
for(j=0;j<width*height;j++)
tmp_buf[j]=0;
for(j=y1;j<=y2;j++)
for(i=x1;i<=x2;i++)
tmp_buf[j*width+i-x1]=buffer[j][i];
gdk_draw_gray_image(widget->window,
widget->style->fg_gc[GTK_STATE_NORMAL], x_offset, y_offset,
width, height, GDK_RGB_DITHER_NONE, tmp_buf, width);
}
主要功能:
int main(int argc,char **argv)
{
gtk_init (&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW (window), "RESET DEMO");
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (gtk_main_quit), NULL);
button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
g_signal_connect_swapped (button, "clicked", G_CALLBACK (display), drawing_area);
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_widget_show (vbox);
gtk_widget_set_size_request(drawing_area, Test_Size*3, Test_Size);
gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
gtk_widget_show(drawing_area);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
gtk_widget_set_can_default (button, TRUE);
gtk_widget_grab_default (button);
gtk_widget_show (button);
gtk_widget_show(window);
/*
reads image into buffer before calling the "display" function in which
calls the Draw_Img function
*/
myinit();
g_signal_connect (G_OBJECT (drawing_area), "expose_event",
G_CALLBACK (display), NULL);
gtk_main();
}
提前感谢您的任何帮助/输入。