1

我有一个 C 程序,它基本上将图像读入 char 缓冲区,在将图像显示回屏幕之前对其像素执行一些操作。

到目前为止,图像显示正常;但我遇到了以下两个问题:

  1. 当我最小化 GTK 窗口并再次打开它时,图像就消失了,使 Drawing_Area 为空。
  2. 如何通过按下按钮重绘/更新 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();
}

提前感谢您的任何帮助/输入。

4

2 回答 2

0

gtk_widget_queue_draw将重新排列要重绘的小部件。

最小化和取消最小化不应导致空白窗口,闻起来像您未提供的代码中的错误。

于 2014-03-21T10:20:51.670 回答
0

展示我的作品

我在这里找到了解决方案:http: //blog.csdn.net/zhongguomin/article/details/7088575

经过两天的调试,我知道了方法。
注意:取消最小化会导致暴露事件。

在 pixmap 上绘制 (pix,line,arc,rect,pixbuf) ,然后 queue_draw (将发出expose_event),将gdk_draw_drawable(pixmap) 放在expose_event 中。

g_signal_connect (G_OBJECT (drawarea), "expose_event", G_CALLBACK (expose_event_callback), NULL);
gboolean expose_event_callback (GtkWidget * widget, GdkEventExpose * event, gpointer data)
{
  g_message ("expose_event");
  gdk_draw_drawable (widget->window, gc, pixmap, 0, 0, 0, 0, widget->allocation.width, widget->allocation.height);
  return TRUE;
}
......
gdk_draw_line (pixmap, gc, xa, ya, xb, yb);
gtk_widget_queue_draw (drawarea);
gdk_draw_rectangle (pixmap, gc, FALSE, x, y, w, h);
gtk_widget_queue_draw (drawarea);
gdk_draw_arc (pixmap, gc, FALSE, xb, ya, w, h, 0, 360 * 64);
gtk_widget_queue_draw (drawarea);
//image from file !!!
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (FCD));
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file (filename, &error);
gdk_draw_pixbuf (pixmap, gc, pixbuf, 0, 0, 0, 0, -1, -1, GDK_RGB_DITHER_NORMAL, 0, 0);
gtk_widget_queue_draw (drawarea);

但是我仍然有一个问题,当我尝试调整drawarea的大小时,像素图会崩溃。

然后我 del if(pixmap)g_object_unref(pixmap); ,坠机消失了,真是个大坑!

然后我制作一个新大小的新像素图,大小可以改变!

而且 allocation.width , allocation.height 在 queue_draw 之前不生效,不能使用pixmap = gdk_pixmap_new (GDK_DRAWABLE (drawarea->window), widget->allocation.width, widget->allocation.height, -1); 马上 !

int wpb = gdk_pixbuf_get_width (pixbuf);
int hpb = gdk_pixbuf_get_height (pixbuf);
g_message ("%d X %d", wpb, hpb);
gtk_widget_set_size_request(drawarea,wpb,hpb);    
pixmap = gdk_pixmap_new (GDK_DRAWABLE (drawarea->window),wpb,hpb, -1);
......
于 2016-03-07T02:43:52.023 回答