473,508 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference between Graphics.DrawString and Win32 GDI ::DrawText

Maybe I am doing something wrong but has anyone else here noticed a
difference in the positioning of text between the Graphics.DrawString
method and the Win32 GDI DrawText function?

My text is getting shifted to the right by 3 pixels for some reason.
Everything else matches.

I am creating my fonts under C# .NET in a way that should produce the
same results as I am getting under C++ with the Win32 GDI API:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
class LOGFONT
{
public enum CharacterSet
{
ANSI_CHARSET = 0,
DEFAULT_CHARSET = 1,
SYMBOL_CHARSET = 2,
SHIFTJIS_CHARSET = 128,
HANGEUL_CHARSET = 129,
HANGUL_CHARSET = 129,
GB2312_CHARSET = 134,
CHINESEBIG5_CHARSET = 136,
OEM_CHARSET = 255,
JOHAB_CHARSET = 130,
HEBREW_CHARSET = 177,
ARABIC_CHARSET = 178,
GREEK_CHARSET = 161,
TURKISH_CHARSET = 162,
VIETNAMESE_CHARSET = 163,
THAI_CHARSET = 222,
EASTEUROPE_CHARSET = 238,
RUSSIAN_CHARSET = 204,
MAC_CHARSET = 77,
BALTIC_CHARSET = 186
}

public enum ClipPrecision
{
CLIP_DEFAULT_PRECIS = 0,
CLIP_CHARACTER_PRECIS = 1,
CLIP_STROKE_PRECIS = 2
}

public enum OutputPrecision
{
OUT_DEFAULT_PRECIS = 0,
OUT_STRING_PRECIS = 1,
OUT_CHARACTER_PRECIS = 2,
OUT_STROKE_PRECIS = 3,
OUT_TT_PRECIS = 4,
OUT_DEVICE_PRECIS = 5,
OUT_RASTER_PRECIS = 6,
OUT_TT_ONLY_PRECIS = 7,
OUT_OUTLINE_PRECIS = 8,
OUT_SCREEN_OUTLINE_PRECIS = 9
}

[Flags]
public enum PitchAndFamily
{
DEFAULT_PITCH = 0,
FIXED_PITCH = 1,
VARIABLE_PITCH = 2,
MONO_FONT = 8,
FF_DONTCARE = (0<<4),
FF_ROMAN = (1<<4),
FF_SWISS = (2<<4),
FF_MODERN = (3<<4),
FF_SCRIPT = (4<<4),
FF_DECORATIVE = (5<<4)
}

public enum Quality
{
DEFAULT_QUALITY = 0,
DRAFT_QUALITY = 1,
PROOF_QUALITY = 2,
NONANTIALIASED_QUALITY = 3,
ANTIALIASED_QUALITY = 4
}

public enum Weight
{
FW_DONTCARE = 0,
FW_THIN = 100,
FW_EXTRALIGHT = 200,
FW_LIGHT = 300,
FW_NORMAL = 400,
FW_MEDIUM = 500,
FW_SEMIBOLD = 600,
FW_BOLD = 700,
FW_EXTRABOLD = 800,
FW_HEAVY = 900,
FW_ULTRALIGHT = FW_EXTRALIGHT,
FW_REGULAR = FW_NORMAL,
FW_DEMIBOLD = FW_SEMIBOLD,
FW_ULTRABOLD = FW_EXTRABOLD,
FW_BLACK = FW_HEAVY
}

public int lfHeight;
public int lfWidth;
public int lfEscapement;
public int lfOrientation;
public int lfWeight;
public byte lfItalic;
public byte lfUnderline;
public byte lfStrikeOut;
public byte lfCharSet;
public byte lfOutPrecision;
public byte lfClipPrecision;
public byte lfQuality;
public byte lfPitchAndFamily;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string lfFaceName;
}

...

LOGFONT lf = new LOGFONT();
lf.lfCharSet = (byte)LOGFONT.CharacterSet.DEFAULT_CHARSET;
lf.lfClipPrecision =
(byte)LOGFONT.ClipPrecision.CLIP_DEFAULT_PRECIS;
lf.lfEscapement = 0;
lf.lfFaceName = "Arial";
lf.lfHeight = -(int)((points * dpi.Height)/72.0F);
lf.lfItalic = 0;
lf.lfOrientation = 0;
lf.lfOutPrecision =
(byte)LOGFONT.OutputPrecision.OUT_DEFAULT_PRECIS;
lf.lfPitchAndFamily = (byte)(LOGFONT.PitchAndFamily.DEFAULT_PITCH
| LOGFONT.PitchAndFamily.FF_DONTCARE);
lf.lfQuality = (byte)LOGFONT.Quality.DEFAULT_QUALITY;
lf.lfStrikeOut = 0;
lf.lfUnderline = 0;
lf.lfWeight = (int)LOGFONT.Weight.FW_BOLD;
lf.lfWidth = 0;
Font font = Font.FromLogFont(lf);
I am using GraphicsUnit.Pixel, the default transform and the default
settings for all other Graphics settings. The results of other
graphics drawing operations via C# .NET are being offset from the
upper left hand corner of the device context the same # of pixels as
they were with the Win32 GDI API.
Thanks to anyone who replies. (Please reply to the newgroup instead of
to my yahoo e-mail account, which I never check.)

DrDevious
Nov 22 '05 #1
1 4758
I notice that the Escapement property doesn't seem to mean anything anymore
if Graphics.DrawString is used. I have to TranslateTransform and
RotateTransform to place rotated text where I want it to go. Maybe I will
have to use the old Win32 DrawText method or re-write all my Text routines to
use the Transforms (much simpler).

Thanks for the LOGFONT sample!! I couldn't find the structure in the .NET
Framework and MSDN Library or On-line documentation.

"DrDevious" wrote:
Maybe I am doing something wrong but has anyone else here noticed a
difference in the positioning of text between the Graphics.DrawString
method and the Win32 GDI DrawText function?

My text is getting shifted to the right by 3 pixels for some reason.
Everything else matches.

I am creating my fonts under C# .NET in a way that should produce the
same results as I am getting under C++ with the Win32 GDI API:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
class LOGFONT
{
public enum CharacterSet
{
ANSI_CHARSET = 0,
DEFAULT_CHARSET = 1,
SYMBOL_CHARSET = 2,
SHIFTJIS_CHARSET = 128,
HANGEUL_CHARSET = 129,
HANGUL_CHARSET = 129,
GB2312_CHARSET = 134,
CHINESEBIG5_CHARSET = 136,
OEM_CHARSET = 255,
JOHAB_CHARSET = 130,
HEBREW_CHARSET = 177,
ARABIC_CHARSET = 178,
GREEK_CHARSET = 161,
TURKISH_CHARSET = 162,
VIETNAMESE_CHARSET = 163,
THAI_CHARSET = 222,
EASTEUROPE_CHARSET = 238,
RUSSIAN_CHARSET = 204,
MAC_CHARSET = 77,
BALTIC_CHARSET = 186
}

public enum ClipPrecision
{
CLIP_DEFAULT_PRECIS = 0,
CLIP_CHARACTER_PRECIS = 1,
CLIP_STROKE_PRECIS = 2
}

public enum OutputPrecision
{
OUT_DEFAULT_PRECIS = 0,
OUT_STRING_PRECIS = 1,
OUT_CHARACTER_PRECIS = 2,
OUT_STROKE_PRECIS = 3,
OUT_TT_PRECIS = 4,
OUT_DEVICE_PRECIS = 5,
OUT_RASTER_PRECIS = 6,
OUT_TT_ONLY_PRECIS = 7,
OUT_OUTLINE_PRECIS = 8,
OUT_SCREEN_OUTLINE_PRECIS = 9
}

[Flags]
public enum PitchAndFamily
{
DEFAULT_PITCH = 0,
FIXED_PITCH = 1,
VARIABLE_PITCH = 2,
MONO_FONT = 8,
FF_DONTCARE = (0<<4),
FF_ROMAN = (1<<4),
FF_SWISS = (2<<4),
FF_MODERN = (3<<4),
FF_SCRIPT = (4<<4),
FF_DECORATIVE = (5<<4)
}

public enum Quality
{
DEFAULT_QUALITY = 0,
DRAFT_QUALITY = 1,
PROOF_QUALITY = 2,
NONANTIALIASED_QUALITY = 3,
ANTIALIASED_QUALITY = 4
}

public enum Weight
{
FW_DONTCARE = 0,
FW_THIN = 100,
FW_EXTRALIGHT = 200,
FW_LIGHT = 300,
FW_NORMAL = 400,
FW_MEDIUM = 500,
FW_SEMIBOLD = 600,
FW_BOLD = 700,
FW_EXTRABOLD = 800,
FW_HEAVY = 900,
FW_ULTRALIGHT = FW_EXTRALIGHT,
FW_REGULAR = FW_NORMAL,
FW_DEMIBOLD = FW_SEMIBOLD,
FW_ULTRABOLD = FW_EXTRABOLD,
FW_BLACK = FW_HEAVY
}

public int lfHeight;
public int lfWidth;
public int lfEscapement;
public int lfOrientation;
public int lfWeight;
public byte lfItalic;
public byte lfUnderline;
public byte lfStrikeOut;
public byte lfCharSet;
public byte lfOutPrecision;
public byte lfClipPrecision;
public byte lfQuality;
public byte lfPitchAndFamily;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string lfFaceName;
}

...

LOGFONT lf = new LOGFONT();
lf.lfCharSet = (byte)LOGFONT.CharacterSet.DEFAULT_CHARSET;
lf.lfClipPrecision =
(byte)LOGFONT.ClipPrecision.CLIP_DEFAULT_PRECIS;
lf.lfEscapement = 0;
lf.lfFaceName = "Arial";
lf.lfHeight = -(int)((points * dpi.Height)/72.0F);
lf.lfItalic = 0;
lf.lfOrientation = 0;
lf.lfOutPrecision =
(byte)LOGFONT.OutputPrecision.OUT_DEFAULT_PRECIS;
lf.lfPitchAndFamily = (byte)(LOGFONT.PitchAndFamily.DEFAULT_PITCH
| LOGFONT.PitchAndFamily.FF_DONTCARE);
lf.lfQuality = (byte)LOGFONT.Quality.DEFAULT_QUALITY;
lf.lfStrikeOut = 0;
lf.lfUnderline = 0;
lf.lfWeight = (int)LOGFONT.Weight.FW_BOLD;
lf.lfWidth = 0;
Font font = Font.FromLogFont(lf);
I am using GraphicsUnit.Pixel, the default transform and the default
settings for all other Graphics settings. The results of other
graphics drawing operations via C# .NET are being offset from the
upper left hand corner of the device context the same # of pixels as
they were with the Win32 GDI API.
Thanks to anyone who replies. (Please reply to the newgroup instead of
to my yahoo e-mail account, which I never check.)

DrDevious

Nov 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1308
by: DrDevious | last post by:
Maybe I am doing something wrong but has anyone else here noticed a difference in the positioning of text between the Graphics.DrawString method and the Win32 GDI DrawText function? My text is...
5
27122
by: Charles A. Lackman | last post by:
Hello, I have created a complete PrintDocument and need to create an image from it. How is this done? e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality...
7
2561
by: Peter Row | last post by:
Hi, I've started work on my own control some parts of which use standard controls, others I need to draw on my controls surface to get the display output I require, however.... I seem to be...
3
2825
by: Allen | last post by:
I've got a control that you can resize the contents of one of the text fields inside it. When the contents are resized to smaller than the text, I remove some of the end of the text and...
3
1598
by: Harley | last post by:
I am trying to build graphic charts in a vb.net module callable from another form. I have translated this code from VB6 where it worked well. I can make the code work to bring up and display the...
1
1165
by: Harley | last post by:
I am trying to build graphic charts in a vb.net module callable from another form. I have translated this code from VB6 where it worked well. I can make the code work to bring up and display the...
1
8068
by: Nathan Laff | last post by:
I have a custom label, that as the width changes, the height will change to fit all the text in the label... I use to do this like this... int newHeight =...
2
12630
by: Tony Johansson | last post by:
Hello! If I use the DrawString below with object of StringFormat as the last object it works good. If I instead remove object StringFormat below as the last object of DrawString I get some rows...
6
10649
vekipeki
by: vekipeki | last post by:
I am having a problem with basic drawing of unicode characters in Windows 2000 and XP. I have written a simplest possible C# WinForms program to test it (just create a new Windows Forms C#...
6
2664
by: Dilip | last post by:
Howdy Folks I have a display where the Graphics.DrawString function is called to display something. Since the text seems to be larger than its bounding rectangle, the call basically splits the...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
435
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.