473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Convert Bitmap to Byte[]

How can I convert a Bitmap to a byte array? I've searched all over and none
of the code samples I've managed to find work.

Actually, I just need to create a Byte array starting from an Image object.

--
El que persevera insiste
Sergio Florez M.
Medellín, Colombia
Nov 16 '05 #1
7 68540
> Actually, I just need to create a Byte array starting from an Image
object.

You can serialize your object using the 'BinaryFormatter' and read the
bytes from the 'Stream'.

Joannes
Nov 16 '05 #2
Image have more private fields than just the image data. If you use a
BinaryFormatter
you'll get a bunch of .NET serialization primitives in the stream and you won't
be able
to easily get at the data. This is a fairly decent attempt at quickly turning a
bitmap into
a binary stream where you can get access to it's data though.

The primary method is to use LockBits
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Joannes Vermorel" <fi*******@lastname.com> wrote in message
news:uw**************@TK2MSFTNGP09.phx.gbl...
Actually, I just need to create a Byte array starting from an Image

object.

You can serialize your object using the 'BinaryFormatter' and read the
bytes from the 'Stream'.

Joannes

Nov 16 '05 #3
Assuming image is an Image object:

MemoryStream stream = new MemoryStream();
image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
Byte[] bytes = stream.ToArray();

"Sergio Florez M." <se***********@hotmail.com> wrote in message
news:ek**************@TK2MSFTNGP11.phx.gbl...
How can I convert a Bitmap to a byte array? I've searched all over and none of the code samples I've managed to find work.

Actually, I just need to create a Byte array starting from an Image object.
--
El que persevera insiste
Sergio Florez M.
Medellín, Colombia

Nov 16 '05 #4

Heres some sample code taht you coudl use.. you will need a unsage code
block for this, and compile it with the unsafe option
//Lock the images in memory so we can access them directly
originalBitmapData = originalBitmap.LockBits(new
Rectangle(0, 0, originalBitmap.Width, originalBitmap.Height),
ImageLockMode.ReadOnly, originalBitmap.PixelFormat);
comparisonBitmapData = comparisonBitmap.LockBits(new
Rectangle(0, 0, comparisonBitmap.Width, comparisonBitmap.Height),
ImageLockMode.ReadOnly, comparisonBitmap.PixelFormat);
outputBitmapData = outputBitmap.LockBits(new Rectangle(0, 0,
outputBitmap.Width, outputBitmap.Height), ImageLockMode.WriteOnly,
outputBitmap.PixelFormat);

//Get pointers to the first byte in each image
byte* originalScanPtr = (byte*)originalBitmapData.Scan0;

"Sergio Florez M." <se***********@hotmail.com> wrote in message
news:ek**************@TK2MSFTNGP11.phx.gbl...
How can I convert a Bitmap to a byte array? I've searched all over and none of the code samples I've managed to find work.

Actually, I just need to create a Byte array starting from an Image object.
--
El que persevera insiste
Sergio Florez M.
Medellín, Colombia

Nov 16 '05 #5

Heres some sample code taht you coudl use.. you will need a unsage code
block for this, and compile it with the unsafe option
//Lock the images in memory so we can access them directly
originalBitmapData = originalBitmap.LockBits(new
Rectangle(0, 0, originalBitmap.Width, originalBitmap.Height),
ImageLockMode.ReadOnly, originalBitmap.PixelFormat);
comparisonBitmapData = comparisonBitmap.LockBits(new
Rectangle(0, 0, comparisonBitmap.Width, comparisonBitmap.Height),
ImageLockMode.ReadOnly, comparisonBitmap.PixelFormat);
outputBitmapData = outputBitmap.LockBits(new Rectangle(0, 0,
outputBitmap.Width, outputBitmap.Height), ImageLockMode.WriteOnly,
outputBitmap.PixelFormat);

//Get pointers to the first byte in each image
byte* originalScanPtr = (byte*)originalBitmapData.Scan0;

"Sergio Florez M." <se***********@hotmail.com> wrote in message
news:ek**************@TK2MSFTNGP11.phx.gbl...
How can I convert a Bitmap to a byte array? I've searched all over and none of the code samples I've managed to find work.

Actually, I just need to create a Byte array starting from an Image object.
--
El que persevera insiste
Sergio Florez M.
Medellín, Colombia

Nov 16 '05 #6
>"Sergio Florez M." <se***********@hotmail.com> wrote in message
news:ek**************@TK2MSFTNGP11.phx.gbl...
How can I convert a Bitmap to a byte array? I've searched all over and

none
of the code samples I've managed to find work.

Actually, I just need to create a Byte array starting from an Image

object.


Image has a Save method, one of whose overrides takes a Stream to save
it to. Once in the stream, just read the stream to a byte array.
--
Simon Smith
simon dot s at ghytred dot com
www.ghytred.com/NewsLook - NNTP Client for Outlook
Nov 16 '05 #7
>"Sergio Florez M." <se***********@hotmail.com> wrote in message
news:ek**************@TK2MSFTNGP11.phx.gbl...
How can I convert a Bitmap to a byte array? I've searched all over and

none
of the code samples I've managed to find work.

Actually, I just need to create a Byte array starting from an Image

object.


Image has a Save method, one of whose overrides takes a Stream to save
it to. Once in the stream, just read the stream to a byte array.
--
Simon Smith
simon dot s at ghytred dot com
www.ghytred.com/NewsLook - NNTP Client for Outlook
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Fritz Switzer | last post by:
With two controls a PictureBox and another "PictureBox like" , Pic2, a control from a dll, I'm stuck on cannot implicity convert System.Drawing.Image to System.Drawing.Bitmap error. How do I...
2
by: Eka Gautama | last post by:
Hi all, I need help for a sample code to convert a bitmap to byte as well from byte to bitmap.. Thanks for the help Eka
2
by: Clas Hortien | last post by:
Hi, i have a array of uint and want to display this array as a System.Web.UI.WebControls.Image. //========================================= uint ar = new uint; string fName;
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
1
by: Daniel | last post by:
I have looked everywhere on the web for an answer to this and the only thing I can find is converting the image format when the file is present on the local filesystem. What I want to do is use a...
3
by: Dennis | last post by:
I am trying to convert a bitmap to a JPEG MemoryStream and return a Byte array containing the resulting JPEG Image as follows: Public Function BmpToJPEG(ByVal BitMapIn As Bitmap, ByVal Quality As...
2
by: cosminb | last post by:
I have a native function which returns an image in the form of a BYTE* lpDIB array. How do I convert it to a Bitmap, so that I can work with it properly in C#? Thanks in advance, Cosmin
3
by: ShihChengYu | last post by:
Dear all: How to convert color image to bi-level image? I have confronted one problem when I build my OCR project. I used an software API function to enhance my project, but the API only...
4
by: eking | last post by:
//Thanks for any help,thank you!лл¡£ public override void FloodFill(Bitmap bmp, Point pt) { int ctr=timeGetTime(); //Debug.WriteLine("*******Flood Fill******"); //get the color's int...
2
by: Bill Fuller | last post by:
I have a Bitmap (bm) that I am able to save using, for example, bm.Save("file.jpg", System.Drawing.Imaging.ImageFormat.Jpeg). However, I would like to convert this to a byte array (byte). What is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.