在图片框中安装图像而不预先保存图像?

时间:2021-02-22 00:24:35

I have a picture box. The user can draw different rectangles on it, assuming that the user draws rectangles which go beyond the size of the picturebox, can I uniformly resize the contents to fit the picturebox, even though the image is not saved yet?.

我有一个图片框。用户可以在其上绘制不同的矩形,假设用户绘制的矩形超出了图片框的大小,我可以统一调整内容大小以适应图片框,即使图像尚未保存吗?

I can only manage to resize content that has been loaded in from and already saved image file. The code that I am using to draw rectangles is as follows:

我只能设法调整已加载的内容和已保存的图像文件。我用来绘制矩形的代码如下:

gr.FillRectangle(Brushes.Black, 0, 0, 2, 75)

Rectangles do appear on picturebox as would be expected. Also I am unable to save using the following code:

矩形确实出现在图片框上,如预期的那样。此外,我无法使用以下代码保存:

PictureBox1.Image.Save("C:\test\myimage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

The error that I get is: Object reference not set to an instance of an object.

我得到的错误是:对象引用未设置为对象的实例。

2 个解决方案

#1


1  

I use System.IO.MemoryStream when I want to fill pictureboxes with images without saving to the hard drive.

当我想用图像填充图片框而不保存到硬盘驱动器时,我使用System.IO.MemoryStream。

PictureBox1.Image = New System.Drawing.Bitmap(New IO.MemoryStream(New System.Net.WebClient().DownloadData("http://upload.wikimedia.org/wikipedia/commons/e/ec/Banksy.on.the.thekla.arp.jpg")))

#2


0  

This is probably because your PictureBox has no "Image" object assigned to it.

这可能是因为您的PictureBox没有分配给它的“图像”对象。

You can find the solution here (first answer): getting image from a picturebox

你可以在这里找到解决方案(第一个答案):从图片框中获取图像

[Also, you might face issues later on with regards to saving to your root folder (C:). Try to use a folder on your local Desktop, for example, to avoid any access-permission errors.]

[另外,您可能会在以后遇到有关保存到根文件夹(C :)的问题。例如,尝试使用本地桌面上的文件夹,以避免任何访问权限错误。]

Edit: This is taken from the above link. It appears to me that you have not tried it.

编辑:这取自上面的链接。在我看来,你还没有尝试过。

myPictureBox.Image =  New Bitmap(myPictureBox.ClientSize.Width, _
                                 myPictureBox.ClientSize.Height) 

'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

#1


1  

I use System.IO.MemoryStream when I want to fill pictureboxes with images without saving to the hard drive.

当我想用图像填充图片框而不保存到硬盘驱动器时,我使用System.IO.MemoryStream。

PictureBox1.Image = New System.Drawing.Bitmap(New IO.MemoryStream(New System.Net.WebClient().DownloadData("http://upload.wikimedia.org/wikipedia/commons/e/ec/Banksy.on.the.thekla.arp.jpg")))

#2


0  

This is probably because your PictureBox has no "Image" object assigned to it.

这可能是因为您的PictureBox没有分配给它的“图像”对象。

You can find the solution here (first answer): getting image from a picturebox

你可以在这里找到解决方案(第一个答案):从图片框中获取图像

[Also, you might face issues later on with regards to saving to your root folder (C:). Try to use a folder on your local Desktop, for example, to avoid any access-permission errors.]

[另外,您可能会在以后遇到有关保存到根文件夹(C :)的问题。例如,尝试使用本地桌面上的文件夹,以避免任何访问权限错误。]

Edit: This is taken from the above link. It appears to me that you have not tried it.

编辑:这取自上面的链接。在我看来,你还没有尝试过。

myPictureBox.Image =  New Bitmap(myPictureBox.ClientSize.Width, _
                                 myPictureBox.ClientSize.Height) 

'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