简体   繁体   English

在不预先保存图像的情况下使图片适合图片框?

[英]Fit image in picturebox without saving image beforehand?

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?. 用户可以得出不同的rectangles就可以了,假设用户绘制rectangles ,其超越的大小picturebox ,我可以均匀调整内容以适应picturebox ,即使图像尚未保存?

I can only manage to resize content that has been loaded in from and already saved image file. 我只能设法resize已从中加载并已保存的图像文件的内容的resize The code that I am using to draw rectangles is as follows: 我用来绘制rectangles的代码如下:

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

Rectangles do appear on picturebox as would be expected. Rectangles确实会出现在图片picturebox 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. 我得到的错误是: Object reference not set to an instance of an object.

当我想用图像填充图片框而不保存到硬盘驱动器时,我使用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")))

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:). [[此外,在以后保存到根文件夹(C :)时,您可能会遇到问题。 Try to use a folder on your local Desktop, for example, to avoid any access-permission errors.] 例如,请尝试使用本地桌面上的文件夹,以避免任何访问权限错误。]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM