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

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
 
M

MD Cobb

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

Hope this helps.

MC
 
D

Dave Harvey

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
 
M

MD Cobb

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
 

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