2009-04-13 130 views
19

是否可以從存儲器(byte[]streamBitmap)中加載圖片而不將其保存到磁盤?如何從位圖中將圖像置於圖片框中

這是我使用打開byte[]陣列爲Bitmap代碼:

unsafe 
{ 
    fixed (byte* ptr = Misc.ConvertFromUInt32Array(image)) 
    { 
     Bitmap bmp = new Bitmap(200, 64, 800, PixelFormat.Format32bppRgb, new IntPtr(ptr)); 
     bmp.RotateFlip(RotateFlipType.Rotate180FlipX); 
     bmp.MakeTransparent(Color.Black); 
     bmp.Save("test.bmp"); 
    } 
} 

而不是使用Bmp.save()的,我可以把Bitmap在圖片框中我的形式?

回答

41

你試過嗎?

pictureBox.Image = bmp; 
+0

我只是做了它和它的工作很好,謝謝! – 2009-04-13 10:51:36

0

如果您正在使用C++編程語言的工作,這是可以做到這樣的:

void backGroundImage() 
{ 
    Image^ back = gcnew Bitmap("C:\\Users\\User\\Documents\\image.bmp"); 
    pictureBox1->BackGroundImage = back; 
}; 

然後就可以調用backGroundImage當你需要加載一個位圖。

3

我有一些類似於導致內存泄漏的接受答案的代碼。問題在於,當您將圖片框圖像設置爲位圖時,您仍然指向位圖,而不是創建副本。如果您需要多次設置圖像,則需要確保處理所有舊位圖。

這是給任何想要的人克隆位圖到圖像框。試試這個:

if (pictureBox.Image != null) pictureBox.Image.Dispose(); 
pictureBox.Image = myBitmap.Clone(
    new Rectangle(0, 0, myBitmap.Width, myBitmap.Height), 
    System.Drawing.Imaging.PixelFormat.DontCare); 
+1

謝謝。試圖使用位圖而不克隆它是在System.Drawing.Dll中引發異常。使用.Clone建議爲我解決了這個問題! – saurabhj 2016-03-29 06:54:00

-1
pictureBox.Image = Properties.Resources.image.bmp; 
+0

你能否提供一些關於如何工作的更多細節? – jotik 2017-07-31 00:19:27

相關問題