将位图转换为图像类型C#asp.net [英] Convert Bitmap to Image type C# asp.net

查看:79
本文介绍了将位图转换为图像类型C#asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Assalam-o-Alaikum,



我正在尝试将位图图像转换为图像数据类型....到目前为止我通过搜索

图片新=(图片)myBitmap;



但它无法正常工作....



无法从system.drawing.bitmap转换为system.web.ui.controls.Image



有什么建议吗?

解决方案

将图像转换为字节的第一种也是最简单的方法是使用System.Drawing命名空间下的ImageConverter类。 ConvertTo函数不是静态的,因此您需要创建该类的实例。该函数返回一个对象,因此需要将结果转换为正确的类型。





 < span class =code-keyword> public   static   byte  [] ImageToByte(Image img )
{
ImageConverter converter = new ImageConverter();
return byte [])converter.ConvertTo(img, typeof byte []));
}


Wale Kum,

你有没有答案??


< blockquote> System.Drawing.Bitmap 继承自 System.Drawing.Image ,而不是来自系统.Web.UI.Controls.Image



你不能简单地通过说明对话来转换为控件图像 位图图像

如果您打算为图像执行此操作(即 System.Drawing.Image

只是:

位图bm =  new 位图(  yourBitmap.jpg); 
图片img =(图片)bm;





所以你看到你想要做的事情是不可能的,你是什么可能尝试做的是在图像控件中显示位图...是你正在尝试的吗?如果是这样,尝试这样的事情:

 ImageSourceConverter c =  new  ImageSourceConverter(); 
this .picture.Source =(ImageSource)c.ConvertFrom(bmp);



< br $>




祝你好运,

Edo


Assalam-o-Alaikum,

I am trying to convert bitmap image to Image data type....what I have learned so far by searching that
Image new = (Image)myBitmap;

But its not working....

cannot convert from system.drawing.bitmap to system.web.ui.controls.Image

Any suggestion?

解决方案

The first and easiest way to convert an image to bytes is to use the ImageConverter class under the System.Drawing namespace. The ConvertTo function is not static, so you'll need to create an instance of the class. The function returns an object so the result needs to be converted to the proper type.


public static byte[] ImageToByte(Image img)
{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}


Wale Kum,
Did you got your answer.?


System.Drawing.Bitmap Inherits from System.Drawing.Image, not from System.Web.UI.Controls.Image

You cannot convert to the control Image simply by stating that the Bitmap is an Image.
If you intended to do this for an Image (that is a System.Drawing.Image)
that would simply be:

Bitmap bm = new Bitmap("yourBitmap.jpg");
Image img = (Image)bm;



So you see that what you are trying to do is impossible, what you could try to do is show the bitmap in the Image control ... was that what you were trying?, if so, try something like this:

ImageSourceConverter c = new ImageSourceConverter();
this.picture.Source = (ImageSource)c.ConvertFrom(bmp);





Good luck,
Edo


这篇关于将位图转换为图像类型C#asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆