为什么保存的时候 picturebox.image为空?怎样保存picturebox里的图?

问题描述

//关键代码Form1.csusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;//陈海gs0721526for北航毕业论文2008-12-9//此程序实现在屏幕上用鼠标左键画图,按下画,松开停。点“转”按钮保存为文件在://Directory.GetCurrentDirectory()+@"one_char_image.bmp"//请大家测试namespaceDrawChar{publicpartialclassForm1:Form{privatePointstartPoint,endPoint;//画笔开始位置,结束位置intwidth=5,height=5;//圆点的宽,高privatePenp=newPen(Color.Red,4.0f);//建立画笔;privateSystem.Collections.ArrayListshapes=newSystem.Collections.ArrayList();//存各点位置publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){}//写一个手写字符,用户任意写privatevoidpictureBox1_Paint(objectsender,PaintEventArgse){Graphicsg=e.Graphics;foreach(Pointshapeinshapes)//shapes是一个Point列表,shape取一个画圆{g.DrawEllipse(p,shape.X,shape.Y,width,height);}//连圆点为线}//记住鼠标按下点坐标privatevoidpictureBox1_MouseDown(objectsender,MouseEventArgse){startPoint=newPoint(e.X,e.Y);//鼠标落下点shapes.Add(startPoint);//记住鼠标落下点pictureBox1.Refresh();//马上画出来}privatevoidpictureBox1_MouseMove(objectsender,MouseEventArgse){endPoint=newPoint(e.X,e.Y);if(e.Button==System.Windows.Forms.MouseButtons.Left)//只处理鼠标左键{shapes.Add(endPoint);//记住所有点startPoint=endPoint;}pictureBox1.Refresh();//这句很关键,没有这句你无法看见你的画的线}//privatevoidpictureBox1_MouseClick(objectsender,MouseEventArgse){startPoint=newPoint(e.X,e.Y);endPoint=newPoint(e.X+1,e.Y+1);pictureBox1.Refresh();}privatevoidpictureBox1_MouseUp(objectsender,MouseEventArgse){startPoint=newPoint(e.X,e.Y);endPoint=newPoint(e.X+1,e.Y+1);pictureBox1.Refresh();}//将picturebox1.image保存为文件privatevoidbuttonOK_Click(objectsender,EventArgse){Imageb=newBitmap(pictureBox1.Width,pictureBox1.Height);//声明此图对象为了保存为文件Graphicsg=Graphics.FromImage(b);//graphics和image关联foreach(Pointshapeinshapes)//同时将shapes保存为一个bitmap文件以便保存为文件{g.DrawEllipse(p,shape.X,shape.Y,width,height);//以圆代点}b.Save(Directory.GetCurrentDirectory()+@"one_char_image.bmp");//保存为文件,注意这里如果改为使用pixturebox1.image.save("fname");会失败的}}}//Form1.Designer.csnamespaceDrawChar{partialclassForm1{///<summary>///Requireddesignervariable.///</summary>privateSystem.ComponentModel.IContainercomponents=null;///<summary>///Cleanupanyresourcesbeingused.///</summary>///<paramname="disposing">trueifmanagedresourcesshouldbedisposed;otherwise,false.</param>protectedoverridevoidDispose(booldisposing){if(disposing&&(components!=null)){components.Dispose();}base.Dispose(disposing);}#regionWindowsFormDesignergeneratedcode///<summary>///RequiredmethodforDesignersupport-donotmodify///thecontentsofthismethodwiththecodeeditor.///</summary>privatevoidInitializeComponent(){System.ComponentModel.ComponentResourceManagerresources=newSystem.ComponentModel.ComponentResourceManager(typeof(Form1));this.pictureBox1=newSystem.Windows.Forms.PictureBox();this.buttonOK=newSystem.Windows.Forms.Button();((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();this.SuspendLayout();////pictureBox1//this.pictureBox1.BackColor=System.Drawing.Color.White;this.pictureBox1.InitialImage=((System.Drawing.Image)(resources.GetObject("pictureBox1.InitialImage")));this.pictureBox1.Location=newSystem.Drawing.Point(46,12);this.pictureBox1.Name="pictureBox1";this.pictureBox1.Size=newSystem.Drawing.Size(398,257);this.pictureBox1.TabIndex=0;this.pictureBox1.TabStop=false;this.pictureBox1.MouseMove+=newSystem.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);this.pictureBox1.MouseClick+=newSystem.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);this.pictureBox1.MouseDown+=newSystem.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);this.pictureBox1.Paint+=newSystem.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);this.pictureBox1.MouseUp+=newSystem.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);////buttonOK//this.buttonOK.Location=newSystem.Drawing.Point(466,110);this.buttonOK.Name="buttonOK";this.buttonOK.Size=newSystem.Drawing.Size(75,23);this.buttonOK.TabIndex=1;this.buttonOK.Text="转";this.buttonOK.UseVisualStyleBackColor=true;this.buttonOK.Click+=newSystem.EventHandler(this.buttonOK_Click);////Form1//this.AutoScaleDimensions=newSystem.Drawing.SizeF(6F,12F);this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;this.ClientSize=newSystem.Drawing.Size(759,556);this.Controls.Add(this.buttonOK);this.Controls.Add(this.pictureBox1);this.Name="Form1";this.Text="Form1";this.Load+=newSystem.EventHandler(this.Form1_Load);((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();this.ResumeLayout(false);}#endregionprivateSystem.Windows.Forms.PictureBoxpictureBox1;privateSystem.Windows.Forms.ButtonbuttonOK;}}//chenhai20010201@163.com,可以给你整个sln

解决方案

解决方案二:
搂主绘制的图其实是在屏幕设备上,并不在pictureBox1的Image上,所以你绘制的图是保存不下来的我在下面贴中有相关解释和做法,搂主可以参考:http://topic.csdn.net/u/20081207/18/f841d3d0-5094-47ef-aef9-54b8c83473bb.html
解决方案三:
噢,不好意思,没仔细看搂主的buttonOK_Click函数的代码,搂主的做法是正确的
解决方案四:
刚给搂主也测试了,我这里点击你说的按钮,是可以保存下来的
解决方案五:
mark,没有时间给你测试,顶一下吧

时间: 2024-09-13 16:57:54

为什么保存的时候 picturebox.image为空?怎样保存picturebox里的图?的相关文章

oracle-text里修改一个timestamp格式的时间,使他为空后保存报错,说格式不对

问题描述 text里修改一个timestamp格式的时间,使他为空后保存报错,说格式不对 严重: Servlet.service() for servlet [jsp] in context with path [/java23_news] threw exception [An exception occurred processing JSP page /SaveNews.jsp at line 35 32: news.setSummary(summary); 33: news.setAut

spamassassin自动生成规则的是保存在哪里,以什么样的数据格式保存的?

问题描述 spamassassin自动生成规则的是保存在哪里,以什么样的数据格式保存的? 大神们好,请问有谁研究过spamassassin,它自动生成规则的是保存在哪里,以什么样的数据格式保存的?求教! 解决方案 我悬赏我全部的50分,大神们出来冒冒泡!

文件保存问题,jsp端生成的文件想保存到客户端

问题描述 文件保存问题,jsp端生成的文件想保存到客户端 我在用户端的jsp上生成了一个文件,我想把这个文件保存到服务器端,不用上传的方法能不能实现? 我在jsp上用的是 <% response.addHeader("Content-disposition", "inline; filename=myword.doc"); response.setContentType("application/msword,charset=gbk");

此頁面為暫時保存一點信息,不好意思占用空間,祝大家中秋快樂

问题描述 InnosetupCreateInstallFreeGgz數據庫名安裝mso97rt.dll需要注冊msjet35.dllsystem32Wrkgadm.exe,comcat.dll,Dbcs3032.dllmssharedC:ProgramFilesMicrosoftOfficeMsain800.dll,Msacc8.olb,C:ProgramFilesCommonFilesMicrosoftShared~VbaMrt7enu.dll,VbA3CHS.dll,Mrt7Enu.dll~

c#保存文件时候的弹出选择要保存的文件夹带新建文件夹效果的类代码

添加引用system.design.dll //选择文件的保存路径   DirBrowser   olderBrowserDlg=new   DirBrowser();         if (folderBrowserDlg.ShowDialog()==DialogResult.OK)    {    txtFilePath.Text = folderBrowserDlg.SelectedPath;   } // using System.Windows.Forms.Design;using

vbs实现的保存剪贴板中的文本并编辑或保存_vbs

保存剪贴板中的文本.vbs 原理就是首先判断是否存在同名文件,存在就名字后面+1,然后将剪贴板的内容保存到自定义扩展名的文件里 复制代码 代码如下: set fso=createobject("scripting.filesystemobject") : name=1Dim nana=Inputbox("请输入拓展名:","拓展名","txt")while fso.fileexists(name&".&quo

我想开发一个软件,可以画很多框框和箭头,然后在框框里填图象和文字,然后可以保存起来,需要什么准备知识?界面要用哪些控件好?

问题描述 如题,请大侠们指教.~~~~~ 解决方案 解决方案二:......主要是绘图的控件.解决方案三:比如哪个?能说详细点吗?我是不是要重载控件?重载控件的过程应该怎么写?解决方案四:到底有没有重载控件的必要呢?重载控件的必要性该如何判断?解决方案五:你需要具备能自己写原生态控件的知识,而不是重载某某控件.重载控件一般都是在现有的控件功能在基本上能满足需要,而你去补充剩余部分.解决方案六:还有,绘图的控件哪个最适合我这个设计?.NET里的控件就好还是用OfficeWord的还是还有其它的呢?

织梦整合百度编辑器保存栏目内容为空了

问题描述 织梦整合百度编辑器保存栏目内容为空了 织梦整合了下百度编辑器,编辑器是好了,倒是保存栏目内容后却是空的,不知道哪位遇到过,谢谢!

wps演示新建和保存文件

  我们在学习公司演讲时常常会使用到幻灯片,今天小编为大家介绍一下如何新建及保存wps演示文档. 首先,打开wps演示,点击左上角上的"wps演示"按钮,在下拉菜单中选择"新建"--"新建空白文档"选项. 这时我们就创建好第一张幻灯片,如下图所示: 在左侧的侧边栏中,右击菜单中,选择"新幻灯片"选项,就可以插入一张新幻灯片.如下图所示: 点击左上角上的"wps演示"按钮,在下拉菜单中点击"保存&q