问题描述
//关键代码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,没有时间给你测试,顶一下吧