如何把Bitmap转为Stream
发布网友
发布时间:2022-05-17 05:12
我来回答
共1个回答
热心网友
时间:2023-10-09 02:25
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数组,可以用上面的函数进行转换