Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1113203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:49:19+00:00 2026-05-17T02:49:19+00:00

I’m using some code to enable me to render rotated text with the TextRenderer.DrawText

  • 0

I’m using some code to enable me to render rotated text with the TextRenderer.DrawText method. (By default, DrawText can only copy a straight forward x and y transform from a graphics object).

The code (C#) is from: connect.microsoft.com. See below for a VB conversion.

The code takes a graphics object, creates a device context and copies the transform matrix from the graphics object. It works, but I’d like, also, to set the TextRenderingHint, so I tried:

<DllImport("gdiplus.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)> _
Public Shared Function GdipSetTextRenderingHint(ByVal graphics As HandleRef, ByVal textRenderingHint As System.Drawing.Text.TextRenderingHint) As Integer
End Function

And then after the SetClip statement, I place: GdipSetTextRenderingHint(hDC, someHint)

This gives me a memory access violation error, so I think I should be using something other than hDC as the argument.

I can get it to work by creating the device context from the original graphics object, and then creating another graphics object from the device context. I then set the hint on the new graphics object. This seems a bit convoluted so I was wondering if it was possible through interop.

VB.Net code conversion:

Friend Class TextRendererDC
    Implements IDeviceContext
    Implements IDisposable

    Private graphics As Graphics
    Private dc As IntPtr

    Private Sub New()
    End Sub

    Public Sub New(ByVal g As Graphics)
        Me.graphics = g
    End Sub

    Public Function GetHdc() As IntPtr Implements System.Drawing.IDeviceContext.GetHdc

        Dim xform As NativeMethods.XFORM
        Dim clipRgn As IntPtr

        Using transf As Matrix = Me.graphics.Transform
            xform = New NativeMethods.XFORM(transf)
        End Using

        Using clip As Region = Me.graphics.Clip
            clipRgn = clip.GetHrgn(Me.graphics)
        End Using

        Me.dc = Me.graphics.GetHdc()

        Dim hDC As New HandleRef(Me, Me.dc)
        Dim hRegion As New HandleRef(Nothing, clipRgn)

        SetTransform(hDC, xform)
        SetClip(hDC, hRegion)
        // The below call creates a memory access violation.
        NativeMethods.GdipSetTextRenderingHint(hDC, System.Drawing.Text.TextRenderingHint.AntiAliasGridFit)

        Return Me.dc
    End Function

    Public Sub ReleaseHdc() Implements System.Drawing.IDeviceContext.ReleaseHdc
        If Me.dc <> IntPtr.Zero Then
            Me.graphics.ReleaseHdc()
            Me.dc = IntPtr.Zero
        End If
    End Sub

    Public Sub Dispose() Implements System.IDisposable.Dispose
        ReleaseHdc()
    End Sub

    Private Shared Sub SetTransform(ByVal hdc As HandleRef, ByVal xform As NativeMethods.XFORM)
        NativeMethods.SetGraphicsMode(hdc, NativeMethods.GM_ADVANCED)
        NativeMethods.SetWorldTransform(hdc, xform)
    End Sub

    Private Shared Sub SetClip(ByVal hdc As HandleRef, ByVal hRegion As HandleRef)
        NativeMethods.SelectClipRgn(hdc, hRegion)
    End Sub

    Private Class NativeMethods

        Public Const GM_ADVANCED As Integer = 2

        <DllImport("Gdi32")> _
        Public Shared Function SetGraphicsMode(ByVal hdc As HandleRef, ByVal mode As Integer) As Integer
        End Function

        <DllImport("Gdi32")> _
        Public Shared Function SetWorldTransform(ByVal hDC As HandleRef, ByVal xform As NativeMethods.XFORM) As Boolean
        End Function

        <DllImport("Gdi32")> _
        Public Shared Function SelectClipRgn(ByVal hDC As HandleRef, ByVal hRgn As HandleRef) As Integer
        End Function

        <DllImport("gdiplus.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)> _
        Public Shared Function GdipSetTextRenderingHint(ByVal graphics As HandleRef, ByVal textRenderingHint As System.Drawing.Text.TextRenderingHint) As Integer
        End Function

        <StructLayout(LayoutKind.Sequential)> _
        Public Class XFORM

            Public eM11 As Single
            Public eM12 As Single
            Public eM21 As Single
            Public eM22 As Single
            Public eDx As Single
            Public eDy As Single

            Public Sub New()
                Me.eM11 = 1.0!
                Me.eM22 = 1.0!
            End Sub

            Public Sub New(ByVal transform As Matrix)
                Me.eM11 = 1.0!
                Me.eM22 = 1.0!
                Me.eM11 = transform.Elements(0)
                Me.eM12 = transform.Elements(1)
                Me.eM21 = transform.Elements(2)
                Me.eM22 = transform.Elements(3)
                Me.eDx = transform.Elements(4)
                Me.eDy = transform.Elements(5)
            End Sub

        End Class

    End Class

End Class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:49:20+00:00Added an answer on May 17, 2026 at 2:49 am

    Wow, that fits the “a little knowledge could be dangerous” mold. Not even the native C++ programmers call the gdiplus entry point directly, they use the C++ wrapper in <gdiplus.h>

    The failure mode here is that your program is loading the wrong version of gdiplus.dll, the one in c:\windows\system32. The legacy version. The right one is in the Windows side-by-side cache, .NET’s System.Drawing assembly contains code to make sure it gets the right version of the DLL from the cache.

    Not the one you get. Yours isn’t even initialized, GdiplusStartup was never called. Kaboom.

    No clue what you’re trying to accomplish. The Graphics class has a TextRenderingHint property, no need for the killer poke.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
For some reason, after submitting a string like this Jack’s Spindle from a text
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.