Changing pixel color using GDI+

Win32 Programming


  • 1. SetRop2 R2_XORPEN on PostScript printer
    When I create a device context associated with a PostScript printer and set the mix mode to R2_XORPEN, subsequent drawing is not XOR'ed with the existing picture. Rather, it is drawn as if the mode were set to R2_COPYPEN. I assume this is because PostScript devices do not support XOR drawing. My question is, how do I tell whether a device supports XOR drawing? SetROP2 does not return an error. I don't see a GetDeviceCaps parameter that clearly identifies this information. Thanks in advance. David Liebtag
  • 2. OpenGL text with Vista
    I have an OpenGl application that works fine on XP and the graphics work ok on Vista but there is no text. It appears to me that the function wglUseFontBitmaps is not working on Vista. Anyone having a similar problem, or better yet, a solution?
  • 3. Maimum size rotated Blit size
    I am trying to send a bitmap to a high res DC. When I attempt to rotate very large bitmaps it fails. I am only trying to rotate, no scaling or skewing. I have tried PlgBlt, StretchBlt with a rotation transform and BitBlt with a rotation transform. They all seem to fail exactly the same and there doesn't seem to be any performance difference either. I suspect that all 3 paths lead to the same method in the end. What is the limit for a rotated Blit? I am just guessing, but it seems that limit I am hitting has to do with with the size of the source BitMap. If the source bitmap is greater than 24bit(16777216) pixels, it fails. The failure happens with both types of high res DCs I am trying to work with, Printer DCs and EMF DCs.
  • 4. WM_COLORSTATIC returns a Pattern brush
    I am trying to get the background color for a static control from the parent dialog. The static control is subclassed and in the OnEraseBkgnd function of (static control) i send the WM_CTLCOLORSTATIC message to the parent dialog. The parent dialog is a simple dialog, but in return for this message all I receive is a brush handle which has BS_PATTERN as the style, which shouldn't be the case, normally I should receive a solid brush. Can you please throw some light on this that what could be the reason for this thing. thanks Pankaj
  • 5. GetDC() overlapping window problem
    I need to read a pixel from a window. Because PrintWindow() is too slow I use GetDC(). However when using GetDC() the window needs to be visible. If the window, or a pixel on the window, is not visisble the value written in the pixel is 0xFFFFFFFF, so you can detect when something is blocking your pixel. When the pixel is blocked I call the SetForGroundWindow() function to get the window on top and read the pixel again. In most cases this works fine. However it does not always work. After calling the SetForGroundWindow function it takes some time for the window to be on top, if I call the GetDC / GetPixel function to fast I will not read the correct pixel / blocked pixel but the value of the pixel that is blocking the correct pixel. In this way I can not know if I read the correct pixel or. Does any one have any idea how to solve it? Using PrintWindow is not an option.

Changing pixel color using GDI+

Postby TC » Mon, 07 Mar 2005 12:54:32 GMT

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

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?


Re: Changing pixel color using GDI+

Postby TC » Mon, 07 Mar 2005 17:27:47 GMT

Oops, deficient googling!

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."


Re: Changing pixel color using GDI+

Postby Morten Wennevik » Mon, 07 Mar 2005 20:44:00 GMT

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.

Happy Coding!
Morten Wennevik [C# MVP]

Re: Changing pixel color using GDI+

Postby Bob Powell [MVP] » Mon, 07 Mar 2005 21:01:54 GMT

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

Answer those GDI+ questions with the GDI+ FAQ

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

Re: Changing pixel color using GDI+

Postby TC » Tue, 08 Mar 2005 10:12:50 GMT

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


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?



Re: Changing pixel color using GDI+

Postby TC » Tue, 08 Mar 2005 10:17:01 GMT

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


Re: Changing pixel color using GDI+

Postby Morten Wennevik » Tue, 08 Mar 2005 21:17:48 GMT

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  

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.

Happy Coding!
Morten Wennevik [C# MVP]

Return to Win32 Programming


Who is online

Users browsing this forum: No registered users and 73 guest