how is this image stored in memory?

  • Thread starter Stephen.Schoenberger
  • Start date
S

Stephen.Schoenberger

I am using the following code to get a monochrome bitmap image into a
byte[] form

Rectangle rec = new Rectangle(0, 0, bmpSource.Width,
bmpSource.Height);
BitmapData bmpSData = bmpSource.LockBits(rec,
ImageLockMode.ReadWrite, bmpSource.PixelFormat);

// Get the address of the first line.
IntPtr iptr = bmpSData.Scan0;

// Declare an array to hold the bytes of the bitmap.
int ibytes = bmpSource.Width * bmpSource.Height;
byte[] bValues = new byte[ibytes];

// Copy the values into the array.
Marshal.Copy(iptr, bValues, 0, ibytes); //just use this!

how is the information stored? Meaning if I "read" bValues from 0 to
(bmp.Width) would that get me the first line in bmpSource? And what
would be the most effective way to save this bValues back to a Bitmap?

Thanks
 
B

Ben Voigt [C++ MVP]

I am using the following code to get a monochrome bitmap image into a
byte[] form

Rectangle rec = new Rectangle(0, 0, bmpSource.Width,
bmpSource.Height);
BitmapData bmpSData = bmpSource.LockBits(rec,
ImageLockMode.ReadWrite, bmpSource.PixelFormat);

Well, you asked to get the same format as the source, so to answer your
question "it depends".

If you want a fixed format, specify a particular PixelFormat, such as
PixelFormat.PixelFormat1bppIndexed or PixelFormat.PixelFormat32bppRGB
// Get the address of the first line.
IntPtr iptr = bmpSData.Scan0;

// Declare an array to hold the bytes of the bitmap.
int ibytes = bmpSource.Width * bmpSource.Height;
byte[] bValues = new byte[ibytes];

// Copy the values into the array.
Marshal.Copy(iptr, bValues, 0, ibytes); //just use this!

how is the information stored? Meaning if I "read" bValues from 0 to
(bmp.Width) would that get me the first line in bmpSource? And what
would be the most effective way to save this bValues back to a Bitmap?

Thanks
 
I

illusion.admins

I am using the following code to get a monochrome bitmap image into a
byte[] form
Rectangle rec = new Rectangle(0, 0, bmpSource.Width,
bmpSource.Height);
BitmapData bmpSData = bmpSource.LockBits(rec,
ImageLockMode.ReadWrite, bmpSource.PixelFormat);

Well, you asked to get the same format as the source, so to answer your
question "it depends".

If you want a fixed format, specify a particular PixelFormat, such as
PixelFormat.PixelFormat1bppIndexed or PixelFormat.PixelFormat32bppRGB


// Get the address of the first line.
IntPtr iptr = bmpSData.Scan0;
// Declare an array to hold the bytes of the bitmap.
int ibytes = bmpSource.Width * bmpSource.Height;
byte[] bValues = new byte[ibytes];
// Copy the values into the array.
Marshal.Copy(iptr, bValues, 0, ibytes); //just use this!
how is the information stored? Meaning if I "read" bValues from 0 to
(bmp.Width) would that get me the first line in bmpSource? And what
would be the most effective way to save this bValues back to a Bitmap?

How can I go about taking the array of bytes I have and "converting"
it back to a Bitmap?
 
B

Ben Voigt [C++ MVP]

How can I go about taking the array of bytes I have and "converting"
it back to a Bitmap?

Use the following Bitmap constructor:

public Bitmap (
int width,
int height,
int stride,
PixelFormat format,
IntPtr scan0
)
 
I

illusion.admins

Use the following Bitmap constructor:

public Bitmap (
int width,
int height,
int stride,
PixelFormat format,
IntPtr scan0
)

Is there a way to convert the bitmap that is in a byte[] array format
back to a bitmap?
 
B

Ben Voigt [C++ MVP]

Use the following Bitmap constructor:

public Bitmap (
int width,
int height,
int stride,
PixelFormat format,
IntPtr scan0
)

Is there a way to convert the bitmap that is in a byte[] array format
back to a bitmap?

Did you read the post you replied to?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top