Can I use an array of Color Values to make a Bitmap ?

  • Thread starter Thread starter Dave Harvey
  • Start date Start date
D

Dave Harvey

I have an array of Color values, and from them I need to create Bitmap - so
far I have found 2 methods, both with drawbacks:

1) Create a bitmap of the right size, then "poke" the values into it
one-at-a-time using SetPixel - horribly slow and inefficient

2) Go out to unmanaged code and use the Scan0 field of the BitmapData
structure. I wish to avoid this, as I would like to have "clean" managed
code which won't get refused by "managed code only" machines.

So, why can't I find a simple method to pass an array of Color values to a
Bitmap constructor (or equivalent)? - this SHOULD be a fast, safe, managed
operation.

Am I missing something obvious, or is the .NET framework the one missing
something (like an important method or two?)

Dave Harvey
Medical Connections
 
Try using the Scan0 of the managed BitmapData that will be
returned upon using the LockBits() method of Bitmap object.

Hope this helps.

MC
 
MC,

It WOULD help, if I could find a way of copying data into the "scan0"
address in a managed way. I know I can do this easily in unmanaged code,
but I was looking for a way of doing so without labelling my code as
"unsafe"

Dave
 
I might have missed it but I'm not sure what language
you're using. Here's a great article using VB.NET

http://support.microsoft.com/default.aspx?scid=kb;en-
us;Q319591

The System.Runtime.InteropServices.Marshal class is your
best friend in these circumstances. Methods like the Copy
one will help you. It's not entirely Managed because the
underlying bitmap structure is not managed either.

HTH
MC
 
Back
Top