2012-05-18 66 views
7

我用下面的代码转换图像在PictureBox成位图:转换图像在PictureBox成位图

bmp = (Bitmap)pictureBox2.Image; 

但我得到的结果为bmp = null。谁能告诉我我是怎么做到的?

+3

当图像属性为null这只能发生找到更多的选择。首先分配属性。 –

+0

如果图像不会为空,我想你会得到一个异常。如果图像不是直接位图,则不能将图像对象转换为位图。 – user287107

回答

5

按我的理解,你有没有指定的图片框的图片 属性,因此它返回的类型转换空。

PictureBox属性自动转换图像格式,如果你看到Image属性的工具提示,它会显示System.Drawing.Bitmap。检查您的图像 属性是否正确分配。

检查一下,它在我身边工作。

private void button1_Click(object sender, EventArgs e) 
     { 
      Bitmap bmp = (Bitmap)pictureBox1.Image; 
     } 

     private void TestForm12_Load(object sender, EventArgs e) 
     { 
      pictureBox1.Image = Image.FromFile("c:\\url.gif"); 
     } 

/// 使用位图类

Bitmap bmp = new Bitmap(pictureBox2.Image); 

您可以直接投给pictureBox2.Image位图作为你正在做的,也使用Bitmap类转换为位图类对象。

编号:Bitmap Constructor (Image)

你可以在这里与Bitmap Class

2

我认为你在寻找这样的:

Bitmap bmp = new Bitmap(pictureBox2.Image)