您的位置:首页 > 编程语言 > C#

C# 图片转换成二进制,二进制转换成图片

2009-09-11 13:52 218 查看
Code
public static class BitmapHelper
{
public static Bitmap BytesToBitmap(byte[] Bytes)
{
MemoryStream stream = null;
try
{
stream = new MemoryStream(Bytes);
return new Bitmap((Image)new Bitmap(stream));
}
catch (ArgumentNullException ex)
{
throw ex;
}
catch (ArgumentException ex)
{
throw ex;
}
finally
{
stream.Close();
}
}

public static byte[] BitmapToBytes(Bitmap Bitmap)
{
MemoryStream ms = null;
try
{
ms = new MemoryStream();
Bitmap.Save(ms, Bitmap.RawFormat);
byte[] byteImage = new Byte[ms.Length];
byteImage = ms.ToArray();
return byteImage;
}
catch (ArgumentNullException ex)
{
throw ex;
}
finally
{
ms.Close();
}
}
}

另外,存进数据库可以直接用Image对象:
SqlCommand.Parameters.Add("@image",SqlDbType.Image).Value=myImage;(myImage为Image对象)
从数据库取出来,是byte数组,可以用上面的函数进行转换
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: