To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Article :: Building Dynamic Systems with Expressions in .NET
How Is XML Like An Interface?
Understanding Covariance and Contravariance
Print VS 2010 Keyboard Shortcut References in Letter (8.5x11in) and A4 (210×297mm) Sizes
Updated Productivity Power Tools



Go Back   VBForums > VBForums CodeBank > CodeBank - C#

Reply Post New Thread
 
Thread Tools Display Modes
Old Sep 6th, 2005, 02:22 AM   #1
MrPolite
l33t!
 
MrPolite's Avatar
 
Join Date: Sep 01
Posts: 4,428
MrPolite is a jewel in the rough (200+)MrPolite is a jewel in the rough (200+)MrPolite is a jewel in the rough (200+)
Converting a bitmap to a byte array

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




it is your sacred oblication to study these methods and write back ... critisize/suggest better ways
__________________
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!

Last edited by MrPolite; Sep 6th, 2005 at 06:40 PM.
MrPolite is offline   Reply With Quote
Old Sep 6th, 2005, 03:11 AM   #2
wossname
Dream Theater Fan
 
wossname's Avatar
 
Join Date: Aug 02
Location: #!/bin/bash
Posts: 5,639
wossname is a glorious beacon of light (400+)wossname is a glorious beacon of light (400+)wossname is a glorious beacon of light (400+)wossname is a glorious beacon of light (400+)wossname is a glorious beacon of light (400+)wossname is a glorious beacon of light (400+)
Re: Converting a bitmap to a byte array

I use Method 1 (with optimisations). All the others are a waste of time

If you use 32bppARGB (or 32bppPARGB like I do) then you can use Marshal.Copy(int[]...) instead which is marginally faster apparently (I've not tested the difference).

I personally dislike using 24bit bitmaps when I'm doing unsafe code, having the stride value different to the width of the image is ugly. Not to mention costly in performance terms. Since 32bpp is more CPU friendly it tends to run better in my experience. Were using all 32 bit color in D# because we need the transparency capability (see the latest screenies in the DoomSharp thread).

We'll be converting all our textures to int arrays soon so we can get a performance boost (if i ever manage to find the time to do this blitter).
__________________
RPNSolve version 1.1.3 (Free commandline math parser) Go on, it's for charity.
Registered Linux user number 0x7468A.
My Flickr


Of course you're free to go // Go and tell the world my story // Tell them about my brother // Tell them about me // The Count of Tuscany
wossname is offline   Reply With Quote
Old Sep 6th, 2005, 12:26 PM   #3
MrPolite
l33t!
 
MrPolite's Avatar
 
Join Date: Sep 01
Posts: 4,428
MrPolite is a jewel in the rough (200+)MrPolite is a jewel in the rough (200+)MrPolite is a jewel in the rough (200+)
Re: Converting a bitmap to a byte array

Quote:
Originally Posted by wossname
I use Method 1 (with optimisations). All the others are a waste of time

If you use 32bppARGB (or 32bppPARGB like I do) then you can use Marshal.Copy(int[]...) instead which is marginally faster apparently (I've not tested the difference).

I personally dislike using 24bit bitmaps when I'm doing unsafe code, having the stride value different to the width of the image is ugly. Not to mention costly in performance terms. Since 32bpp is more CPU friendly it tends to run better in my experience. Were using all 32 bit color in D# because we need the transparency capability (see the latest screenies in the DoomSharp thread).

We'll be converting all our textures to int arrays soon so we can get a performance boost (if i ever manage to find the time to do this blitter).
I see. I use 32bit in my own app but I thought 24 would be more appropriate for this example
__________________
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
MrPolite is offline   Reply With Quote
Old Sep 8th, 2006, 09:04 AM   #4
drepin
New Member
 
Join Date: Sep 06
Posts: 1
drepin is an unknown quantity at this point (<10)
Re: Converting a bitmap to a byte array

For .NET Framework 2.0 the following line of code (C#) seems to do the trick:

byte[] bytes = (byte[])TypeDescriptor.GetConverter(bmp).ConvertTo(bmp, typeof(byte[]));

Dmitriy
drepin is offline   Reply With Quote
Old Sep 9th, 2006, 01:03 AM   #5
MrPolite
l33t!
 
MrPolite's Avatar
 
Join Date: Sep 01
Posts: 4,428
MrPolite is a jewel in the rough (200+)MrPolite is a jewel in the rough (200+)MrPolite is a jewel in the rough (200+)
Re: Converting a bitmap to a byte array

interesting! thanks!
__________________
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
MrPolite is offline   Reply With Quote
Old Feb 27th, 2008, 01:31 PM   #6
mattes
New Member
 
Join Date: Feb 08
Posts: 1
mattes is an unknown quantity at this point (<10)
Re: Converting a bitmap to a byte array

I know this post is old, but I'm trying to get an answer to my unexplained problem...
I use your method 1 to manipulate a bitmap.
I create an array with the datas of the original bitmap, make some calculation on it, and save the result in another array, wich i save to a.bmp using the second part of your method.
The problem is if the ratio H/W of the final image is a decimal number (not real) the output image is perfect, if this ratio is real the final array has the same number of cells (x3) as the number of pixel of the final image but produced actually a shifted image, juste like instead of writing 3*W cells per line, it skips one and writes actually 3*W cells +1 (but one is skipped so it writes 3*W cells but 3*W cells shifted by one cell from the 3*W cells it should have written).
Example :

image width = 3 pixels (24Bpp)
array length for one line 9 cells
what it should write : [123456789]1
what it actually writes : 1[234567891]
and do so for each line, one cell is skipped per line
mattes is offline   Reply With Quote
Reply

Go Back   VBForums > VBForums CodeBank > CodeBank - C#


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 07:56 PM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.