create bitmap

  • Thread starter Thread starter radu
  • Start date Start date
R

radu

i need a bit of help with few lines of code.
1. i have integer array a[500,500] = numbers from 0 to 256
2. i have to create a 256 colours bitmap of 500x500 pixels, asign
colours as matrix a.
ex. bitmap at pixel at row 3 col 5 = colour number is a[3,5]
and save it to disk as a 256 colour bitmap without any compresion as
c:\a.bmp.
no interface.
i have microsoft visual c# 2008 express edition.
help? thank you.
 
i need a bit of help with few lines of code.
1. i have integer array a[500,500] = numbers from 0 to 256
2. i have to create a 256 colours bitmap of 500x500 pixels, asign
colours as matrix a.
ex. bitmap at pixel at row 3 col 5 = colour number is a[3,5]
and save it to disk as a 256 colour bitmap without any compresion as
c:\a.bmp.
no interface.
i have microsoft visual c# 2008 express edition.
help? thank you.

It depends on whether you want it to be fast, or to have purely
verifiable code.

In any case, read about System.Bitmap.Class (http://msdn.microsoft.com/
en-us/library/system.drawing.bitmap.aspx), and its constructor and
Save() method in particular. To fill it with pixels, you can either
use Bitmap.LockBits() method to get access directly to its pixel
buffer, and then use Marshal.Copy() to copy data from your pixel array
into that buffer a scanline at a time - this is the fast-but-unsafe
way. If you want slow-but-safe, just use Bitmap.SetPixel() in a loop
to copy all pixels from your array.

Do read the docs for all of the above first, and if you still have
questions after that, feel free to ask.
 
i need a bit of help with few lines of code.
1. i have integer array a[500,500] = numbers from 0 to 256
2. i have to create a 256 colours bitmap of 500x500 pixels, asign
colours as matrix a.
ex. bitmap at pixel at row 3 col 5 = colour number is a[3,5]
and save it to disk as a 256 colour bitmap without any compresion as
c:\a.bmp.
no interface.
i have microsoft visual c# 2008 express edition.
help? thank you.

Just out of curiosity wouldn't the array be [255, 255]? And shouldn't
the colors go from 0 to 255, not 256?

Tom P.
 
radu said:
i need a bit of help with few lines of code.
1. i have integer array a[500,500] = numbers from 0 to 256
2. i have to create a 256 colours bitmap of 500x500 pixels, asign
colours as matrix a.
ex. bitmap at pixel at row 3 col 5 = colour number is a[3,5]
and save it to disk as a 256 colour bitmap without any compresion as
c:\a.bmp.
no interface.
i have microsoft visual c# 2008 express edition.
help? thank you.

The bmp format is fairly simple, especially as you have a fixed size and
bit depth. Just put the header and the data in a byte array, and save it
as a file.

You will need a color palette, though. A value from 0 to 255 is not a color.
 

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

Back
Top