1

I want to call native gdi+ method to improve performance of text drawing. Can anyone provide example? I found the following code from reference source. I want to use this method in C# to improve the performance. How can i achieve this?

[DllImport(ExternDll.Gdiplus, SetLastError=true, ExactSpelling=true, 
    CharSet = System.Runtime.InteropServices.CharSet.Unicode)] // 3 = Unicode
[ResourceExposure(ResourceScope.None)]
internal static extern int GdipDrawString(HandleRef graphics, 
    string textString, int length, HandleRef font, 
    ref GPRECTF layoutRect, HandleRef stringFormat, HandleRef brush);

I don't know how to create object for HandleRef class. Please suggest me on this?

At present am using following code to draw the string.

Graphics.DrawString(text, font, brush, rect, format);
Crypt32
  • 12,850
  • 2
  • 41
  • 70
  • 4
    I will bet that there is something else in your code that is making things slow. Share that code with us to get help. Graphics.DrawString is really only useful for printing, otherwise, use TextRenderer.DrawText. – LarsTech Jul 20 '16 at 18:40
  • Please provide a link to where you got that example decliration, that will help us show you where HandleRef is defined. Also, I agree with Lars, I doubt this will make it much faster. Show the code you call Graphics.DrawString from, there is likely faster ways to do whatever you are trying to do – Scott Chamberlain Jul 20 '16 at 18:41
  • I got GDI+ native win32 code from the following link http://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Advanced/Gdiplus.cs,15ca78ad12cd45eb, –  Jul 20 '16 at 18:51
  • You most likely only imagine the performance problem. – TaW Jul 20 '16 at 19:00

2 Answers2

0

The simplest form of DrawString looks like this:

public void DrawString (string, Font, Brush, PointF);

Where string is the text that you want to draw, Font and Brush are the font and brushes used to draw the text, and PointF is the starting point of the text.

Drawing Text:

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.DrawString("Hello GDI+ World!",
new Font("Verdana", 16),
new SolidBrush(Color.Red),
new Point(20, 20));
}

This book will provide more info Graphics Programming with GDI+

Thennarasan
  • 698
  • 6
  • 11
0

A HandleRef is essentially an IntPtr to a handle and a reference to the object the handle belongs to. Using HandleRef prevents the GC from collecting the object until the native method is done with it.

Reference link: HandleRef

Kesavan D
  • 184
  • 5