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

  • Home
  • SEARCH
  • 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 8019931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:32:30+00:00 2026-06-04T21:32:30+00:00

On the back of a Windows Form, I get a window DC, create a

  • 0

On the back of a Windows Form, I get a window DC, create a Graphics object with Graphics.FromHdc, and then dispose the Graphics object before releasing the DC.

Private Declare Function GetWindowDC Lib "user32.dll" (ByVal hwnd As IntPtr) As IntPtr
Private Declare Function ReleaseDC Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As Integer

Dim hdc As IntPtr = GetWindowDC(Me.Handle)

Try
    Using g As Graphics = Graphics.FromHdc(hdc)
        ' ... use g ...
    End Using
Finally
    ReleaseDC(Me.Handle, hdc)
End Try

The Microsoft documentation for Graphics.FromHdc shows similar code. (It uses Graphics.GetHdc and Graphics.ReleaseHdc, instead of Win32 GetWindowDc and ReleaseDC.) However, they release the DC before disposing the Graphics object:

' Get handle to device context.
Dim hdc As IntPtr = e.Graphics.GetHdc()

' Create new graphics object using handle to device context.
Dim newGraphics As Graphics = Graphics.FromHdc(hdc)

' Draw rectangle to screen.
newGraphics.DrawRectangle(New Pen(Color.Red, 3), 0, 0, 200, 100)

' Release handle to device context and dispose of the Graphics  ' object
e.Graphics.ReleaseHdc(hdc)
newGraphics.Dispose()

Why did they do it this way?
Should the DC be released before or after Graphics.Dispose?
It is possible that the wrong order can cause resource leaks or memory corruption?

  • 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-06-04T21:32:32+00:00Added an answer on June 4, 2026 at 9:32 pm

    From the Graphics.Dispose method:

    private void Dispose(bool disposing)
    {
    ..SNIP...
        if (this.nativeGraphics != IntPtr.Zero)
        {
            try
            {
                if (this.nativeHdc != IntPtr.Zero) <<---
                {
                    this.ReleaseHdc(); <<--- 
        }
    

    So it seems like it will release the hdc by itself tbh.

    [edit]

    It’s actually :

    [DllImport("gdiplus.dll", CharSet = CharSet.Unicode, EntryPoint = "GdipReleaseDC",     ExactSpelling = true, SetLastError = true)]
    private static extern int IntGdipReleaseDC(HandleRef graphics, HandleRef hdc);
    

    That is getting called, don’t know if the gdiplus release dc handles native device contexts too

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

Sidebar

Related Questions

A theoretically question: I have a C# Windows Form app, that sends back some
I have a bunch of windows forms. Each form has Back and Next buttons
I have a window (Windows form ) where i have two WPF user controls
I remember using Named Pipes for communicating between 2 machines (back in Windows NT).
I am building a server back-end for a mobile social network using Windows Azure.
How can I pass back some arguments from a window that is closed? For
Got a window.history.go(1) to stop the user hitting the back button, but was wondering
I need to open a new window from code-behind on post-back if a specific
This code seems to work perfectly: window.onhashchange = function(){ if (!window.location.href.match('#pop')) { //alert('go back
I have a borderless Windows Forms application. The main window creates other forms (simple

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.