从图片框中获取图像 [英] getting image from a picturebox
问题描述
如何获得我在图片框上绘制的图像?在我的应用程序中,我使用手绘图创建了一个表单.但是我无法将我绘制的图像保存为文件,有一条错误消息对象引用未设置为对象的实例".我正在使用代码
how can i get an image that i drawn on a picturebox? in my application i created a form with the freehand drawing.but i can't save the image that i drawn as a file,there have a error message that "object reference not set to an instance of an object".I am using the code
me.picturebox1.image.save(filename,sysytem.....jpeg)
谁知道请帮帮我...谢谢
any one knows please help me...thank you
推荐答案
我会假设您从不分配 PictureBox
的 Image
属性,而是直接执行绘图命令在从 PictureBox 控件检索的 Graphics
对象上.如果是这种情况,则在执行绘图时 Image
属性将保持为 Nothing
/null
.要解决此问题,请在初始化 PictureBox
控件时为 Image
属性分配一个空图像:
I will assume that you never assign the Image
property of your PictureBox
but instead perform drawing commands directly on a Graphics
object retrieved from the PictureBox control. If that is the case, the Image
property will remain Nothing
/null
when you perform the drawing. To get around this, assign an empty image to the Image
property when you initialize the PictureBox
control:
myPictureBox.Image = New Bitmap(myPictureBox.ClientSize.Width, _
myPictureBox.ClientSize.Height)
然后,当你想执行一些绘图时,使用 Image
中的 Graphics
对象而不是 PictureBox
:
Then, when you want to perform some drawing, use a Graphics
object from the Image
instead of the PictureBox
:
Using g As Graphics = Graphics.FromImage(myPictureBox.Image)
' do the drawing '
End Using
使用这种方法,您可以像在当前代码中一样保存图像,只需在 PictureBox.Image
对象上调用 Save
.
With this approach you can save the image as you do in your current code, by simply calling Save
on the PictureBox.Image
object.
这篇关于从图片框中获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!