如何保存在PictureBox上创建的图形?在c#和VisualStudioWindows窗体中,我将图像加载到图片框(pictureBox2)中,然后将其裁剪并显示在另一个图片框(pictureBox3)中。现在我想将pictureBox3中的内容保存为图片文件。我怎样才能做到这一点?privatevoidcrop_bttn_Click(objectsender,EventArgse){Imagecrop=GetCopyImage("grayScale.jpg");pictureBox2.Image=裁剪;位图sourceBitmap=newBitmap(pictureBox2.Image,pictureBox2.Width,pictureBox2.Height);图形g=pictureBox3.CreateGraphics();g.DrawImage(sourceBitmap,newRectangle(0,0,pictureBox3.Width,pictureBox3.Height),rectCropArea,GraphicsUnit.Pixel);sourceBitmap.Dispose();永远不要使用control.CreateGraphics!使用Graphicsg=Graphics.FromImage(bmp)或使用e.Graphics参数在控件的Paint事件中绘制位图bmp。这是一个裁剪代码,它绘制一个新的位图并使用您的控件等,但改变了一些东西:privatevoidcrop_bttn_Click(objectsender,EventArgse){pictureBox2.Image=裁剪;位图targetBitmap=newBitmap(pictureBox3.ClientSize.Width,pictureBox3.ClientSize.Height);使用(BitmapsourceBitmap=newBitmap(pictureBox2.Image,pictureBox2.ClientSize.Width,pictureBox2.ClientSize.Height)){使用(Graphicsg=Graphics.FromImage(targetBitmap)){g.DrawImage(sourceBitmap,newRectangle(0,0,pictureBox3.ClientSize.Width,pictureBox3.ClientSize.Height),rectCropArea,GraphicsUnit.Pixel);}}if(pictureBox3.Image!=null)pictureBox3.Image.Dispose();pictureBox3.Image=targetBitmap;targetBitmap.Save(somename,someFormat);替代方案是...:位图targetBitmap=newBitmap(pictureBox3.ClientSize.Width,pictureBox3.ClientSize.Height);pictureBox3.DrawToBitmap(targetBitmap,pictureBox3.ClientRectangle);你不应该使用PictureBox.CreateGraph除非你知道你在做什么ics()方法,因为它会导致一些不太明显的问题。例如,在您的场景中,当您最小化或调整窗口大小时,pictureBox3的图像消失。更好的方法是绘制位图,您也可以保存它:varcroppedImage=newBitmap(pictureBox3.Width,pictureBox3.Height);varg=Graphics.FromImage(croppedImage);g.DrawImage(crop,newPoint(0,0),rectCropArea,GraphicsUnit.Pixel);g.处置();//现在你可以保存位图croppedImage.Save(...);pictureBox3.Image=croppedImage;顺便说一句,使用更明智的变量名称,特别是pictureBox1..3。以上就是C#学习教程:如何保存在PictureBox上创建的图形?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
