CreatePen with the wrong color

  • Thread starter Thread starter Sharon
  • Start date Start date
S

Sharon

Hello helping experts,

I'm using VC2005 with .NET Framework 2.0, C#.

I using the Win32 GDI function:
HPEN CreatePen(int fnPenStyle,int nWidth,COLORREF crColor)

as PInvoke as follow:

[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtr CreatePen(PenStyles enPenStyle, int nWidth, int
crColor);

But when I'm using/invoking the CreatePen by my C# code as follow:

Color color = Color.Yellow;
int crColor = color.ToArgb() & 0xFFFFFF; // or just color.ToArgb()
IntPtr gdiPen = CreatePen(/*PS_SOLID*/0, 1, GetRGBFromColor(PenColor));

The color I'm getting is not the color being drawn. In most of the colors
I'm setting, the color being drawn is deferent.


What am I doing wrong?
 
Hi Sharon,

Can you provide the code for the GetRGBFromColor function? It is possible
that it does not populate the structure correctly.

On another note, is there a specific reason to use the CreatePen API as
opposed to the Pen class which you can directly create from Color.Yellow?
 
Opps, sorry,

The GetRGBFromColor function is posted but not fully. Here it is:

Color color = Color.Yellow;
int crColor = color.ToArgb() & 0xFFFFFF; // or just color.ToArgb()
IntPtr gdiPen = CreatePen(/*PS_SOLID*/0, 1, crColor);


For the another note; I'm using the CreatePen API as of the Win32 GDI and not the GDI Plus of the >NET Framwork, because I need to implement the "rubber" drawing and the XOR drawing that can be done the Win32 GDI only (unfortunately).
 
Sharon,

Two things. One, I assume that you are also using:

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

to associate the pen with the hdc.

Second,
HPEN is defined on MSDN as:

HPEN CreatePen(
int fnPenStyle, // pen style
int nWidth, // pen width
COLORREF crColor // pen color
);

and COLORREF is defined on MSDN as:

0x00bbggrr

Notice that RGB is backward in the order, where ars ToArgb is in RGB
order.

So, with those two things in mind, this snippet will get you a yellow
line:

string strColor = "00" + Color.Yellow.B.ToString("X") +
Color.Yellow.R.ToString("X") + Color.Yellow.G.ToString("X");
hdc = e.Graphics.GetHdc();
gdiPen = CreatePen(PenStyles.PS_SOLID, 10, Int32.Parse(strColor,
System.Globalization.NumberStyles.HexNumber));
SelectObject(hdc, gdiPen);
DrawLine(e.Graphics, new Point(100,100), new Point(300,300));

L. Lee Saunders

my blog: http://oldschooldotnet.blogspot.com
Taking Dot Net "Old School" - Playing with old ideas/concepts using
the newest tools!
 
Thanks Lee,

Your post is handling a specific color, yellow. But the yellow is just an example for the problem I'm having.
The color is selected by the user via GUI,and I need to do some drawing using this color and by using Win32 GDI.

So the code my be:
Color color = Color.Yellow; // or any other color selected by the user.
// Converting the Color into COLORREF
int crColor = color.ToArgb() & 0xFFFFFF;
IntPtr gdiPen = CreatePen(/*PS_SOLID*/0, 1, crColor);

The trouble with that code the many of the selected colors are drawn as different color.

By your post it seems that the conversion of the .NET Framework Color into COLORREF is wrong?
Am I correct? How can I fix that?


P.S.: Yes, I'm using the SelectObject().
 
Sharon G. said:
Your post is handling a specific color, yellow. But the yellow is just an example for the problem I'm having.
The color is selected by the user via GUI,and I need to do some drawing using this color and by using Win32 GDI.

So the code my be:
Color color = Color.Yellow; // or any other color selected by the user.
// Converting the Color into COLORREF
int crColor = color.ToArgb() & 0xFFFFFF;
IntPtr gdiPen = CreatePen(/*PS_SOLID*/0, 1, crColor);

The trouble with that code the many of the selected colors are drawn as different color.

By your post it seems that the conversion of the .NET Framework Color into COLORREF is wrong?
Am I correct? How can I fix that?

You mentioned in a previous post that you were doing this via GDI because
you needed to use XOR drawing. Is that actually what you're doing? If so,
then the color you get will not be yellow. It will be the pixel from the
screen with the red and green values inverted. That is:

dest = dest ^ 0xffff00;

So, if you use your yellow pen to draw using XOR over a red field, what
you'll get is a green line, because red (0xff0000) xored with yellow
(0xffff00) is green (0x00ff00).
 
Yes, I'm using the Win32 GDI for XOR drawing in it works fine, but this XOR drawing is under condition that the selected color is transparent.
In the case I'm asking about, I'm not using XOR drawing ( not using SetROP2() ) and the color is set to a color other then transparent.

So back to the problem at hand:
it seems that the conversion of the .NET Framework Color into COLORREF is wrong?
Am I correct? How can I fix that?
 
Ok, I have found the fix:

The conversion from System.Drawing.Color to COLORREF is:

ColorTranslator.ToWin32(System.Drawing.Color)

It it's all well now (-:
 
Ok, I have found the fix:

The conversion from System.Drawing.Color to COLORREF is:

ColorTranslator.ToWin32(System.Drawing.Color)

It it's all well now (-:
 
Back
Top