将字节[]转换为picture.image [英] convert byte [] to picture.image

查看:140
本文介绍了将字节[]转换为picture.image的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将byte []转换为picterw.image以显示图片或将图像转换为picurebox

how to Convert byte[] to picterw.image for show pic or image to picurebox

推荐答案

byte[] buffer= byte Array here;
  MemoryStream ms = new MemoryStream(buffer);
  Bitmap bmp=new Bitmap(ms);



可以将此位图分配给图片框的image属性.确保字节也包括图像标题

使用后处置流.



This bitmap can be assign to the image property of the picture box. Make sure the bytes includes the image headers as well

dispose the streams after use.


尝试此
public static Bitmap BytesToBitmap(byte[] byteArray)
{
using (MemoryStream ms = new MemoryStream(byteArray))
{
Bitmap img = (Bitmap)Image.FromStream(ms);
return img;
}
}


从数据库中读取图像时,通常需要一种将它们放在第一位的方法,因此出于完整性考虑:
使用类似的方法从System.Drawing.Image
获取byte []
When you read images from a db, you usually need a way to put them there in the first place, so just for completeness:
Use something like this to get byte[] from System.Drawing.Image
public static byte[] ConvertImageToBytes(System.Drawing.Image imageToConvert,
ImageFormat formatOfImage) 
 { 
 byte[] result; 
 try 
 { 
  using (MemoryStream ms = new MemoryStream()) 
  { 
   imageToConvert.Save(ms,formatOfImage); 
   result = ms.ToArray(); 
  } 
 } 
 catch (Exception) { /* do something useful like logging the exception */ throw;} 
 return result; 
} 



要将byte []转换为System.Drawing.Image,可以使用类似以下内容的方法(如d @ nish,RaviRanjankr和Albin Abel所示):



To convert a byte[] to a System.Drawing.Image you can use something like (as shown by d@nish, RaviRanjankr and Albin Abel ):

public static System.Drawing.Image ConvertBytesToImage(byte[] buffer)
{
 System.Drawing.Image result; 

 using (MemoryStream ms = new MemoryStream(buffer,0,buffer.Length)) 
 { 
  ms.Write(myByteArray,0,myByteArray.Length); 
  result = Image.FromStream(ms,true); 
 }
 return result;
}



问候
Espen Harlinn



Regards
Espen Harlinn


这篇关于将字节[]转换为picture.image的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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