C# 将 PictureBox 中的图像转换为位图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10651686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert the image in a PictureBox into a bitmap
提问by DjMalaikallan
I have used the following code to convert the image in a PictureBox into a Bitmap:
我使用以下代码将 PictureBox 中的图像转换为位图:
bmp = (Bitmap)pictureBox2.Image;
But I am getting the result as bmp = null. Can anyone tell me how I do this?
但我得到的结果为bmp = null. 谁能告诉我如何做到这一点?
采纳答案by Niranjan Singh
As per my understanding your have not assigned PictureBox's Image property, so that it is returning null on type cast.
PictureBox property automatically convert the Image format and if you see the tooltip on Image property, it will Show System.Drawing.Bitmap. Check your image property is correctly assigned.
根据我的理解,您尚未分配 PictureBox 的 Image 属性,因此它在类型转换时返回 null。
PictureBox 属性会自动转换 Image 格式,如果您在 Image 属性上看到工具提示,它将显示 System.Drawing.Bitmap。检查您的图像属性是否正确分配。
Check this, it is working at my side.
检查这个,它在我身边工作。
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");
}
/// Using BitMap Class
///使用位图类
Bitmap bmp = new Bitmap(pictureBox2.Image);
You can directly cast pictureBox2.Imageto Bitmap as you are doing and also using the Bitmap class to convert to Bitmap class object.
您可以直接pictureBox2.Image转换为 Bitmap,也可以使用 Bitmap 类转换为 Bitmap 类对象。
Ref:Bitmap Constructor (Image).
参考:位图构造函数(图像)。
You can find more options here with the Bitmap Class
您可以在此处使用位图类找到更多选项
回答by Tilak
Bitmap bitmap = new Bitmap(pictureBox2.Image)
回答by Renatas M.
I think you looking for this:
我想你在找这个:
Bitmap bmp = new Bitmap(pictureBox2.Image)

