惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
N
News and Events Feed by Topic
S
Schneier on Security
G
GRAHAM CLULEY
Scott Helme
Scott Helme
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
CERT Recently Published Vulnerability Notes
T
The Exploit Database - CXSecurity.com
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
P
Privacy & Cybersecurity Law Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 司徒正美
Blog — PlanetScale
Blog — PlanetScale
Project Zero
Project Zero
MyScale Blog
MyScale Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
The Last Watchdog
The Last Watchdog
Vercel News
Vercel News
The Cloudflare Blog
C
Check Point Blog
Help Net Security
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
NISL@THU
NISL@THU
S
Security @ Cisco Blogs
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
S
SegmentFault 最新的问题
MongoDB | Blog
MongoDB | Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
AWS News Blog
AWS News Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
PCI Perspectives
PCI Perspectives
S
Securelist
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Vulnerabilities – Threatpost
S
Secure Thoughts

博客园 - 潘胜国

Emgu学习之(一)----Emgu简介 .NET 调用c++库注意事项 网站预编译命令说明(aspnet_compiler) VS2005即时窗口 DataSet主副表关系应注意的问题 通用验证脚本 SQL SERVER小Tips AG_E_RUNTIME_METHOD : CreateFromXaml错误的及解决 调试web服务器无法启动 IE 7 BETA 3安装 什么是爱情【转载】 取Assembly中的资源 利用IIS压缩文件 C# 2.0泛型初试 Page常用方法 IFame的另一种表现 自动提交问题,不知道这算不算是MS的一个BUG Ajax实现两级下拉框连动的应用 for和foreach的执行效率的问题之新发现
[转载]Converting a bitmap to a byte array
潘胜国 · 2006-12-06 · via 博客园 - 潘胜国

this comes from my own l33t hax0r skillz
if you've got a better way, say so

method 1 just copies the bitmap bytes from memory
method 2 actually saves the image and writes any image headers and whatnot using a proper image format, and then converts the written data to a byte array
method 3 saves the objects data using serialization (very useless just serialize to file or something... included for the heck of it)

the l33test way in my opinion is using unsafe code:

Method 1: unsafe code, locking the bitmap and using the Marshal class

Code:

// 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; }

in most cases this works fine, because your bitmap doesn't have an alpha channel. But if it does, and you want to preserve it, then use Format32bppArgb in the fist line when locking the bitmap.
I wish I could think of a way of doing this without locking the bitmap (ie, you could use GCHandle.Alloc() and then call Marshal.Copy() using the created handle, but the problem is I wouldnt know the size of the bitmap without locking it's bits and the Marshal.Copy() function needs to know the size).

reversing this process is a bit ugly, as you need to know the dimensions of the bitmap.

Code:

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; }

ok that was if you wanted to have exactly the same bytes as in the memory There are two other ways I can think of. Note that these won't produce the exact copy of the bitmap bits from the memory


Method 2: using a memory stream

This just asks the .Save() method to save the bytes to a memory stream, then you can get the written bytes to the stream:

Code:

// 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; }

converting that back to a bitmap is easy.

Code:

//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; }

Method 3:using serialization
I can't see any reason why someone would use this method, it's very stupid including it just for reference

Code:

// 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); }

big note: if you use any of these methods, then you have to use both functions presented in the method together. For example you can't get the bitmap bytes using method 1, and then call method2's function BytesToImg()

also, a bit after I wrote that method 1, I realized that there was an API for doing this . It's maybe the best method but there is a turn down: you have to use reflection to retreive the bitmap handle in order to use it with the api function:

Method 4: using reflection and API
I just tell you the APIs to retreive the bitmap handle you can use this

Code:

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); }

the GetBitmapBits API seems to be the easy way out, but apparently it's just there for 16bit compatibility. You should use the GetDIBits instead. If you end up using that method, please post your code