locked
How to call GDipDrawString method in c#? RRS feed

  • Question

  • I want to call the 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 do not how to create object for HandleRef class.  Please suggest me on this?

    At present am using following code to draw the string.

     Graphics.DrawString()

    Wednesday, July 20, 2016 6:14 PM

Answers

  • In order to call this function, you will have to repeat the operations that can be seen in the Reference Sources for Graphics.DrawString. You will also find there the definition of HandleRef.

    Therefore, are you sure that you will obtain a substantial performance improvement?

    Maybe there are other solutions for your problem.

    • Proposed as answer by Andrey Zakharov Thursday, July 21, 2016 7:58 AM
    • Marked as answer by DotNet Wang Friday, July 29, 2016 7:03 AM
    Wednesday, July 20, 2016 6:37 PM
  • Hi Sivasubramaniv,

    Welcome to MSDN forum.

    >>I do not how to create object for HandleRef class.  Please suggest me on this?

    If you want to create object for HandleRef, please see MSDN document. Here is example about how to use HandleRef.

    From the Viorel_ said we know Graphics.DrawString Method that draws the specified text string at the specified location with the specified Brush and Font objects. Here is example how to use this function, please refer to it.

    public void DrawStringPointF(PaintEventArgs e)
    {
    
        // Create string to draw.
        String drawString = "Sample Text";
    
        // Create font and brush.
        Font drawFont = new Font("Arial", 16);
        SolidBrush drawBrush = new SolidBrush(Color.Black);
    
        // Create point for upper-left corner of drawing.
        PointF drawPoint = new PointF(150.0F, 150.0F);
    
        // Draw string to screen.
        e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint);
    }

    For more details, please see the link: https://msdn.microsoft.com/en-us/library/76c5db29%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    If your issue has been resolved, please mark an useful post as answer.

    Best Regards,

    Hart


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.


    • Edited by Hart Wang Thursday, July 21, 2016 7:56 AM
    • Marked as answer by DotNet Wang Friday, July 29, 2016 7:03 AM
    Thursday, July 21, 2016 7:55 AM

All replies

  • In order to call this function, you will have to repeat the operations that can be seen in the Reference Sources for Graphics.DrawString. You will also find there the definition of HandleRef.

    Therefore, are you sure that you will obtain a substantial performance improvement?

    Maybe there are other solutions for your problem.

    • Proposed as answer by Andrey Zakharov Thursday, July 21, 2016 7:58 AM
    • Marked as answer by DotNet Wang Friday, July 29, 2016 7:03 AM
    Wednesday, July 20, 2016 6:37 PM
  • Can you share the simple sample to call the GdipDrawString() win32 API in C#? 
    Wednesday, July 20, 2016 6:56 PM
  • Hi!

    There is a wrapper method provided by microsoft (https://msdn.microsoft.com/en-us/library/windows/desktop/ms535991(v=vs.85).aspx)

    Take a look also at the internal implementation. (http://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Graphics.cs,3184fd5c9344dbdd)

    It reveals the essence of many parameters.

    So, just use code like this

                foreach (var w in SomeUtilityClass.GetChildWindows(IntPtr.Zero)) //We get all the windows
                {
                    var g = Graphics.FromHwnd(w);
                    g.DrawString("+++++++++++++++++++++++++++++++++++", new Font(FontFamily.GenericSerif, 40), Brushes.Red, new PointF(100, 100));
                }

    and "DrawString" method has within himself calls the function "GdipDrawString".

    If you have more questions, I'll be glad to answer them)


    Wednesday, July 20, 2016 7:48 PM
  • I'm curious to know why you think this will do you any good.  The Windows Forms Graphics class is little more than a very thin layer around GdiPlus.  No matter how you do it, SOMEONE has to cross the managed/unmanaged boundary.  Whether it is you doing it or the thin layer doing it makes essentially no difference.

    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Thursday, July 21, 2016 7:20 AM
  • Hi Sivasubramaniv,

    Welcome to MSDN forum.

    >>I do not how to create object for HandleRef class.  Please suggest me on this?

    If you want to create object for HandleRef, please see MSDN document. Here is example about how to use HandleRef.

    From the Viorel_ said we know Graphics.DrawString Method that draws the specified text string at the specified location with the specified Brush and Font objects. Here is example how to use this function, please refer to it.

    public void DrawStringPointF(PaintEventArgs e)
    {
    
        // Create string to draw.
        String drawString = "Sample Text";
    
        // Create font and brush.
        Font drawFont = new Font("Arial", 16);
        SolidBrush drawBrush = new SolidBrush(Color.Black);
    
        // Create point for upper-left corner of drawing.
        PointF drawPoint = new PointF(150.0F, 150.0F);
    
        // Draw string to screen.
        e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint);
    }

    For more details, please see the link: https://msdn.microsoft.com/en-us/library/76c5db29%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    If your issue has been resolved, please mark an useful post as answer.

    Best Regards,

    Hart


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.


    • Edited by Hart Wang Thursday, July 21, 2016 7:56 AM
    • Marked as answer by DotNet Wang Friday, July 29, 2016 7:03 AM
    Thursday, July 21, 2016 7:55 AM