Changing pixel color using GDI+

T

TC

Hi folks

In my current project I have to use the GDI+ flat API from MS Visual
Basic for Applications (VBA). I can't use C++, VB.NET, or anything
similar, for various reasons. It is working fine so far. I can open
images, edit them in various ways, save them as different types, & so
on.

But! How do you change the color of a specified pixel within an
*pallette-indexed* image?

I have an 8 bits-per-pixel palette-indexed PNG image.
GdipBitmapGetPixel returns the actual color value, so it must be
getting the pixel value then indexing into the color palette "behind
the scenes" to get that color. But when I use GdipBitmapSetPixel to
change the pixel color, I get error 2, invalid parameter.

I can understand why set-pixel won't accept an actual color value (for
an indexed image): what if that color was not in the palette? But it
doesn't seem to accept a palette index either (1, 2, 3 etc.).

So, how do you change the color of a specified pixel in an
indexed-pallette image?

TIA
TC
 
T

TC

Oops, deficient googling!
From John Hornick [MSFT]
Oct 29 2003, 8:40 am:

"Keep in mind that GDI+ can neither wrap a Graphics around nor
SetPixel() on an indexed-format Bitmap. For those, you'd need to
either DrawImage() them into a non-indexed Bitmap, or LockBits in the
native format and manipulate the pixels directly yourself."

Grrrr..
TC
 
M

Morten Wennevik

Not surprised,

Btw, it is well worth looking into LockBits as the performance is far
better than SetPixel, up to 90% time saved if you are creating a Bitmap
from scratch.


Oops, deficient googling!
From John Hornick [MSFT]
Oct 29 2003, 8:40 am:

"Keep in mind that GDI+ can neither wrap a Graphics around nor
SetPixel() on an indexed-format Bitmap. For those, you'd need to
either DrawImage() them into a non-indexed Bitmap, or LockBits in the
native format and manipulate the pixels directly yourself."

Grrrr..
TC
 
B

Bob Powell [MVP]

Take a look at the article on LockBits in the GDI+ FAQ. It'll explain how to
get at the pixels for the various pixel formats.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
T

TC

Morten said:
Not surprised,

At what? Sorry, I don't see what you're referring to there :)

Btw, it is well worth looking into LockBits as the performance is far
better than SetPixel, up to 90% time saved if you are creating a Bitmap
from scratch.

I started looking at LockBits last night. Unfortunately, there is a
dearth of information on how to use it from VB[A]. So far, I have (a)
drawn a bar across the top of the image (when I really expected a bar
down the left), and (b) got numerous loud explosions when I allocated
the user buffer to what I thought it should be, rather than to - ahem -
1mb! A few more late nights are required, methinks.

P.S. Can you tell me *why* it is not possible to derive a graphic
object from an indexed image? I can't see anything illogical about
wanting to do that. I also can't see why setpixel coulnd't let you edit
the pallete index value of the specified pixel. Surely that is a
reasonable thing to expect it to do?


Cheers,
TC

Oops, deficient googling!
From John Hornick [MSFT]
Oct 29 2003, 8:40 am:

"Keep in mind that GDI+ can neither wrap a Graphics around nor
SetPixel() on an indexed-format Bitmap. For those, you'd need to
either DrawImage() them into a non-indexed Bitmap, or LockBits in the
native format and manipulate the pixels directly yourself."

Grrrr..
TC
 
T

TC

Thanks Bob, your FAQ is a great source of information. I will check out
what it says on LockBits.

TC
 
M

Morten Wennevik

At what? Sorry, I don't see what you're referring to there :)

Good, question, and I'll be darned if I can remember what I was thinking
of at the time.
I think my mind was mixed up with someone asking about a 16 bpp
pixelformat.

Anyway, for how to use LockBits, Bob's GDI+ FAQ is a good source.

As for not being able to use Graphics or SetPixel on indexed images.
Well, not sure, but the same goes for most programs being able to do stuff
on images. Photoshop can't do much on a indexed image either, and you are
forced to convert it to rgb first.
 

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