/// <summary>
/// Bitmap转成Byte[]
/// </summary>
/// <param name="bitmap"></param>
/// <returns></returns>
public static byte[] BitmapToByte(System.Drawing.Bitmap bitmap)
{
// 1.先将BitMap转成内存流
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.Seek(0, System.IO.SeekOrigin.Begin);
// 2.再将内存流转成byte[]并返回
byte[] bytes = new byte[ms.Length];
ms.Read(bytes, 0, bytes.Length);
ms.Dispose();
return bytes;
}
/// <summary>
/// byte[] 转 Bitmap
/// </summary>
/// <param name="ImageByte"></param>
/// <returns></returns>
public static System.Drawing.Bitmap ByteToBitmap(byte[] ImageByte)
{
System.Drawing.Bitmap bitmap = null;
try
{
using (System.IO.MemoryStream stream = new System.IO.MemoryStream(ImageByte))
{
bitmap = new System.Drawing.Bitmap((System.Drawing.Image)new System.Drawing.Bitmap(stream));
}
}
catch (Exception ex)
{
}
return bitmap;
}
/// <summary>
/// Bitmap 克隆
/// </summary>
/// <param name="src"></param>
/// <returns></returns>
public static System.Drawing.Bitmap BitmapClone(System.Drawing.Bitmap src)
{
// Create a Bitmap object from a file.
System.Drawing.Bitmap myBitmap = src;
// Clone a portion of the Bitmap object.
System.Drawing.RectangleF cloneRect = new System.Drawing.RectangleF(0, 0, myBitmap.Width, myBitmap.Height);
System.Drawing.Imaging.PixelFormat format =
myBitmap.PixelFormat;
System.Drawing.Bitmap cloneBitmap = myBitmap.Clone(cloneRect, format);
return cloneBitmap;
}
Bitmap 与 Byte[] 的相互转换; Bitmap的克隆
最新推荐文章于 2024-05-07 17:35:50 发布