2011-08-09 42 views
8

我在圖片框中有一個圖像。我想將該圖像作爲位圖來獲取。C#如何從一個圖片框獲取位圖

我的一行代碼是:

Bitmap default_image = (Bitmap)pictureBox5.Image.Clone();

但是我得到的是:

default_image value=null;

誰能幫我。

回答

15
Bitmap default_image = new Bitmap(pictureBox5.Image); 

你永遠不會實例化一個Bitmap這就是爲什麼它是null

+0

如果他從Image.Clone()中獲得空值,那麼調用具有相同圖像屬性的構造函數也不會做太多。 – MGZero

+2

@MGZero:這是我的理解,它是空的,因爲你不能將一個圖像對象轉換爲位圖。 –

+0

位圖是從Image..sooo繼承的...是的,你是對的。 +1現在我確定:) – MGZero

0

這是因爲你沒有圖像,可能你有BackgroundImage。 你需要有圖片屬性填充您的照片。

0

如果使用imageLocation

pbSourceImage.ImageLocation = openFile.FileName; 

得到了圖像到PictureBox然後PictureBox.Image將是無效的。

相反,加載使用

pbSourceImage.Image = Image.FromFile(openFile.FileName); 

然後你就可以從圖像屬性克隆的圖片。