簡體   English   中英

在不預先保存圖像的情況下使圖片適合圖片框?

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

我有一個畫框。 用戶可以得出不同的rectangles就可以了,假設用戶繪制rectangles ,其超越的大小picturebox ,我可以均勻調整內容以適應picturebox ,即使圖像尚未保存?

我只能設法resize已從中加載並已保存的圖像文件的內容的resize 我用來繪制rectangles的代碼如下:

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

Rectangles確實會出現在圖片picturebox 我也無法使用以下代碼保存:

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

我得到的錯誤是: 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")))

這可能是因為您的PictureBox沒有分配“圖像”對象。

您可以在此處找到解決方案(第一個答案): 從圖片框獲取圖像

[[此外,在以后保存到根文件夾(C :)時,您可能會遇到問題。 例如,請嘗試使用本地桌面上的文件夾,以避免任何訪問權限錯誤。]

編輯:這是從上面的鏈接。 在我看來,您還沒有嘗試過。

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