URN Logo
UNIX Resources » Linux » China Linux Forum » Linux下的GUI软件开发 » 10 » 如何将gtk的drawing area中的内容保存图片?谢谢
announcement 声明: 本页内容为中国Linux论坛的内容镜像,文章的版权以及其他所有的相关权利属于中国Linux论坛和相应文章的作者,如果转载,请注明文章来源及相关版权信息。
Resources
China Linux Forum(finished)
Linux Forum(finished)
FreeBSD China(finished)
linuxforum.net
  业界新闻与评论
  自由软件杂谈
  IT 人生
  Linux软件快递
  翻译作坊
  Linux图书与评论
  GNU Emacs/XEmacs
  Linux 中文环境和中文化
  Linux桌面与办公软件
  Linux 多媒体与娱乐版
  自由之窗Mozilla
  笔记本电脑上的Linux
  Gentoo
  Debian 一族
  网络管理技术
  Linux 安装与入门
  WEB服务器和FTP服务器
  域名服务器和邮件服务器
  Linux防火墙和代理服务器应用
  文件及打印服务器
  技术培训与认证
  TI专版
  Linux内核技术
  Linux 嵌入技术
  Linux设备驱动程序
  Linux 集群技术
  LINUX平台数据库
  系统和网络安全
  CPU 与 编译器
  系统计算研究所专栏
  Linux下的GUI软件开发
  C/C++编程版
  PHP 技 术
  Java&jsp技术
  Shell编程技术
  Perl 编 程
  Python 编 程
  XML/Web Service 技术
  永远的Unix
  FreeBSD世界
   
如何将gtk的drawing area中的内容保存图片?谢谢
 
 
 
Subject: 如何将gtk的drawing area中的内容保存图片?谢谢
Author: syncrow    Posted: 2006-08-28 17:18    Length: 104 byte(s)
[Original] [Print] [Top]
如题,在绘图区画了图后,如何将绘图区中内容保存成图片文件,只要是linux下支持的图片格式都可以.
谢谢!
[Original] [Print] [Top]
Subject: Re: 如何将gtk的drawing area中的内容保存图片?谢谢
Author: gavinlx    Posted: 2006-08-28 17:34    Length: 167 byte(s)
[Original] [Print] [Top]
可以用GdkPixbuf,
先从 drawable 建立 gdkpixbuf, gdk_pixbuf_get_from_drawable();
然后利用 gdk_pixbuf_save() 函数将得到的pixbuf保存为你需要的图片格式.
[Original] [Print] [Top]
Subject: Re: 如何将gtk的drawing area中的内容保存图片?谢谢
Author: syncrow    Posted: 2006-08-28 21:58    Length: 5,715 byte(s)
[Original] [Print] [Top]
谢谢gavinlx, 我按照你的提示找了一些资料把程序弄好了,随便把我的调试版放上来,大家分享!
这个版本只是一个保存空白画图区为jpeg文件的简单程序,考虑不周到的地方,勿见笑!!!!
#include <gtk/gtk.h>

#define WIN_WIDTH 1000 //window's width
#define WIN_HEIGHT 700 //window's height
#define DRAW_WIN_WIDTH 1400 //drawing area width
#define DRAW_WIN_HEIGHT 2000 //drawing area height

static GdkPixmap *pixmap = NULL; //draw map
static GtkWidget *da; //draw area
static GdkPixbuf *pixbuf = NULL; //save drawing area to pixbuf
char filename[50] = "1.jpeg";


/* Create a new pixmap of the appropriate size to store our flow area */
static gboolean
flow_area_configure_event (GtkWidget * widget,
GdkEventConfigure * event, gpointer data)
{
// printf ("configure ");

if (pixmap)
g_object_unref (pixmap);
pixmap = gdk_pixmap_new (widget->window,
widget->allocation.width,
widget->allocation.height, -1);

/* Initialize the pixmap to white */
gdk_draw_rectangle (pixmap,
widget->style->white_gc,
TRUE,
0, 0,
widget->allocation.width, widget->allocation.height);

/* We've handled the configure event, no need for further processing. */
return TRUE;
}


/* Redraw the screen from the pixmap */
static gboolean
flow_area_expose_event (GtkWidget * widget,
GdkEventExpose * event, gpointer data)
{
/* We use the "foreground GC" for the widget since it already exists,
* but honestly any GC would work. The only thing to worry about
* is whether the GC has an inappropriate clip region set.
*/
gdk_draw_drawable (widget->window,
widget->style->fg_gc[GTK_WIDGET_STATE (widget)], pixmap,
/* Only copy the area that was exposed. */
event->area.x, event->area.y,
event->area.x, event->area.y,
event->area.width, event->area.height);

return FALSE;
}

void
init_drawing_window (void)
{
GtkWidget *frame;
GtkWidget *vbox;
GtkWidget *window;
GtkWidget *scrolled_window;

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Flow Drawing Area");

g_signal_connect (window, "delete_event", G_CALLBACK (gtk_main_quit), NULL);

gtk_container_set_border_width (GTK_CONTAINER (window), 8);
gtk_window_set_default_size (GTK_WINDOW (window), WIN_WIDTH, WIN_HEIGHT);

//scrolled window
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
GTK_POLICY_ALWAYS, GTK_POLICY_ALWAYS);
gtk_container_add (GTK_CONTAINER (window), scrolled_window);

vbox = gtk_vbox_new (FALSE, 8);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
//gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW
(scrolled_window), vbox);
frame = gtk_frame_new (NULL);
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
da = gtk_drawing_area_new ();

gtk_widget_set_size_request (da, DRAW_WIN_WIDTH, DRAW_WIN_HEIGHT);
gtk_container_add (GTK_CONTAINER (frame), da);

/* Signals used to handle backing pixmap */
g_signal_connect (da, "expose_event",
G_CALLBACK (flow_area_expose_event), NULL);
g_signal_connect (da, "configure_event",
G_CALLBACK (flow_area_configure_event), NULL);

gtk_widget_set_events (da, gtk_widget_get_events (da)
| GDK_LEAVE_NOTIFY_MASK
| GDK_BUTTON_PRESS_MASK
| GDK_POINTER_MOTION_MASK
| GDK_POINTER_MOTION_HINT_MASK);
gtk_widget_show_all (window);
}

int
main (int argc, char *argv[])
{
int i;

gtk_init (&argc, &argv);
init_drawing_window ();

pixbuf =
gdk_pixbuf_get_from_drawable (NULL, (GdkDrawable *) pixmap,
gdk_drawable_get_colormap (pixmap), 0,
0, 0, 0, DRAW_WIN_WIDTH, DRAW_WIN_HEIGHT);

if (gdk_pixbuf_save
(pixbuf, filename, "jpeg", NULL, "quality", "100", NULL))
printf ("File Saved... ");
else
printf ("File not Saved... Error ");
gtk_main ();
return 0;
}




[Original] [Print] [Top]
« Previous thread
GdkAtom具体怎么理解,不懂,请赐教!
Linux下的GUI软件开发
10
Next thread »
轻松话题,Gtk+下如何播放声音
     

Copyright © 2007~2009 UNIX Resources Network, All Rights Reserved.      About URN | Privacy & Legal | Help | Contact us
webmaster: webmaster@unixresources.net
This page created on 2009-09-07 16:42:26, cost 0.0196540355682 ms.