Converting a bitmap to a byte array 位图文件与Byte[]转换
来源:互联网 发布:ipad软件图标上有锁 编辑:程序博客网 时间:2024/06/05 18:07
发布一个位图文件与Byte流间转换的方法集。
方法1 从内存中直接获取位图的Byte[]
// import this
// using System.Runtime.InteropServices;
private unsafe byte[] BmpToBytes_Unsafe (Bitmap bmp)
...{
BitmapData bData = bmp.LockBits(new Rectangle (new Point(), bmp.Size),
ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
// number of bytes in the bitmap
int byteCount = bData.Stride * bmp.Height;
byte[] bmpBytes = new byte[byteCount];
// Copy the locked bytes from memory
Marshal.Copy (bData.Scan0, bmpBytes, 0, byteCount);
// don't forget to unlock the bitmap!!
bmp.UnlockBits (bData);
return bmpBytes;
}
// using System.Runtime.InteropServices;
private unsafe byte[] BmpToBytes_Unsafe (Bitmap bmp)
...{
BitmapData bData = bmp.LockBits(new Rectangle (new Point(), bmp.Size),
ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
// number of bytes in the bitmap
int byteCount = bData.Stride * bmp.Height;
byte[] bmpBytes = new byte[byteCount];
// Copy the locked bytes from memory
Marshal.Copy (bData.Scan0, bmpBytes, 0, byteCount);
// don't forget to unlock the bitmap!!
bmp.UnlockBits (bData);
return bmpBytes;
}
还原方法
private unsafe Bitmap BytesToBmp (byte[] bmpBytes, Size imageSize)
...{
Bitmap bmp = new Bitmap (imageSize.Width, imageSize.Height);
BitmapData bData = bmp.LockBits (new Rectangle (new Point(), bmp.Size),
ImageLockMode.WriteOnly,
PixelFormat.Format24bppRgb);
// Copy the bytes to the bitmap object
Marshal.Copy (bmpBytes, 0, bData.Scan0, bmpBytes.Length);
bmp.UnlockBits(bData);
return bmp;
}
...{
Bitmap bmp = new Bitmap (imageSize.Width, imageSize.Height);
BitmapData bData = bmp.LockBits (new Rectangle (new Point(), bmp.Size),
ImageLockMode.WriteOnly,
PixelFormat.Format24bppRgb);
// Copy the bytes to the bitmap object
Marshal.Copy (bmpBytes, 0, bData.Scan0, bmpBytes.Length);
bmp.UnlockBits(bData);
return bmp;
}
方法2 通过将位图文件写入内存流的方式获取byte[]
// Bitmap bytes have to be created via a direct memory copy of the bitmap
private byte[] BmpToBytes_MemStream (Bitmap bmp)
...{
MemoryStream ms = new MemoryStream();
// Save to memory using the Jpeg format
bmp.Save (ms, ImageFormat.Jpeg);
// read to end
byte[] bmpBytes = ms.GetBuffer();
bmp.Dispose();
ms.Close();
return bmpBytes;
}
//Bitmap bytes have to be created using Image.Save()
private Image BytesToImg (byte[] bmpBytes)
...{
MemoryStream ms = new MemoryStream(bmpBytes);
Image img = Image.FromStream(ms);
// Do NOT close the stream!
return img;
}
private byte[] BmpToBytes_MemStream (Bitmap bmp)
...{
MemoryStream ms = new MemoryStream();
// Save to memory using the Jpeg format
bmp.Save (ms, ImageFormat.Jpeg);
// read to end
byte[] bmpBytes = ms.GetBuffer();
bmp.Dispose();
ms.Close();
return bmpBytes;
}
//Bitmap bytes have to be created using Image.Save()
private Image BytesToImg (byte[] bmpBytes)
...{
MemoryStream ms = new MemoryStream(bmpBytes);
Image img = Image.FromStream(ms);
// Do NOT close the stream!
return img;
}
方法3 使用序列化的方式获得位图的byte[]
// import these
// using System.Runtime.Serialization;
// using System.Runtime.Serialization.Formatters.Binary;
private byte[] BmpToBytes_Serialization (Bitmap bmp)
...{
// stream to save the bitmap to
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize (ms, bmp);
// read to end
byte[] bmpBytes = ms.GetBuffer();
bmp.Dispose();
ms.Close();
return bmpBytes;
}
private Bitmap BytesToBmp_Serialized (byte[] bmpBytes)
...{
BinaryFormatter bf = new BinaryFormatter ();
// copy the bytes to the memory
MemoryStream ms = new MemoryStream (bmpBytes);
return (Bitmap)bf.Deserialize(ms);
}
// using System.Runtime.Serialization;
// using System.Runtime.Serialization.Formatters.Binary;
private byte[] BmpToBytes_Serialization (Bitmap bmp)
...{
// stream to save the bitmap to
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize (ms, bmp);
// read to end
byte[] bmpBytes = ms.GetBuffer();
bmp.Dispose();
ms.Close();
return bmpBytes;
}
private Bitmap BytesToBmp_Serialized (byte[] bmpBytes)
...{
BinaryFormatter bf = new BinaryFormatter ();
// copy the bytes to the memory
MemoryStream ms = new MemoryStream (bmpBytes);
return (Bitmap)bf.Deserialize(ms);
}
方法4 获取位图对象句柄、
private IntPtr getImageHandle (Image img)
...{
FieldInfo fi = typeof(Image).GetField("nativeImage", BindingFlags.NonPublic | BindingFlags.Instance);
if (fi == null)
return IntPtr.Zero;
return (IntPtr)fi.GetValue (img);
}
...{
FieldInfo fi = typeof(Image).GetField("nativeImage", BindingFlags.NonPublic | BindingFlags.Instance);
if (fi == null)
return IntPtr.Zero;
return (IntPtr)fi.GetValue (img);
}
获得句柄后,想怎么操作都可以喽
转自一个不明英文论坛
- Converting a bitmap to a byte array 位图文件与Byte[]转换
- Converting a bitmap to a region
- Convert a byte array to a Hex string
- convert a byte array to a hexadecimal string
- Bitmap与byte[]相互转换
- Bitmap, byte array, Drawable之间的转换
- How to: Load Unmanaged Resources into a Byte Array
- Convert byte array to char* in a clr wrapper
- How to display a byte array as hex values
- Reading a File into a Byte Array
- Bitmap与Drawable、byte[]、图片资源、图片文件的转换
- c# Bitmap byte[] Stream 文件相互转换
- c# Bitmap byte[] Stream 文件相互转换
- c# Bitmap byte[] Stream 文件相互转换
- c# Bitmap byte[] Stream 文件相互转换
- C# 转换函数 (Stream to Byte[],Byte to Image,Image to Byte[],Image缩放,Byte[] to Bitmap)
- convert an int into a byte array
- The max length of a byte array
- visio2003反向工程
- 基础才是最重要的
- 原创:进化论带来对人类的思考
- 英语学习者必看——英语学习的误区
- [转]qtopia移植:交叉编译qtopia-2.2.
- Converting a bitmap to a byte array 位图文件与Byte[]转换
- Java jdbc 链接数据库MYSQL问题
- Java的ClassLoader与Package机制
- 屏蔽鼠标右键、Ctrl+N、Shift+F10、Alt+F4、F11、F5刷新、退格键
- 实习项目
- 科长双色球对奖器0.1
- 观察者模式
- 遍历Request.ServerVariables
- asp.net 2.0 ajax中实现弹出窗口报警提示