将图片框中的图像转换为位图

本文关键字:图像 转换 位图 | 更新日期: 2023-09-27 18:34:31

我使用以下代码将PictureBox中的图像转换为位图:

bmp = (Bitmap)pictureBox2.Image;

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

将图片框中的图像转换为位图

据我了解,您尚未分配PictureBox的图像属性,以便它在类型强制转换时返回 null。

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

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

private void button1_Click(object sender, EventArgs e)
{
    var 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 类对象。

参考:位图构造函数(图像(。

您可以在此处找到更多位图类选项

Bitmap bitmap = new Bitmap(pictureBox2.Image)

http://msdn.microsoft.com/en-us/library/ts25csc8.aspx

我想你在寻找这个:

Bitmap bmp = new Bitmap(pictureBox2.Image)