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 6794809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:16:00+00:00 2026-05-26T18:16:00+00:00

In C#, am able to draw an ellipse on a screen device context taken

  • 0

In C#, am able to draw an ellipse on a screen device context taken from the Windows Calculator (calc.exe) window using the .NET System.Graphics.DrawEllipse method.

I would like to be able to do the same thing with the GDI32 Ellipse method. How can I make Ellipse draw to the screen?

In the following code, this line works:
CalculatorGraphics.DrawEllipse(penRed, 50, 50, 50, 50);
But this line does not:
PlatformInvokeGDI32.Ellipse(hDC, 100, 100, 100, 100);
What is the problem?

        //In size variable we shall keep the size of the window.
        SIZE size;

        //Win32 API functions are imported in classes
        //PlatformInvokeGDI32
        //PlatformInvokeUSER32.cs

        //Get handle of calc.exe window.
        IntPtr hwnd = PlatformInvokeUSER32.FindWindow("SciCalc", "Calculator");

        //Get window dimensions
        PlatformInvokeUSER32.RECT rect;
        PlatformInvokeUSER32.GetWindowRect(hwnd, out rect);
        size.cx = rect._Right - rect._Left;
        size.cy = rect._Bottom - rect._Top;

        //Get the device context of Calculator.
        IntPtr hDC = PlatformInvokeUSER32.GetDC(hwnd);

        //Draw on the Calculator surface.
        Graphics CalculatorGraphics = Graphics.FromHdc(hDC);
        Color colorRed = Color.FromName("Red");
        Pen penRed = new Pen(colorRed);
        CalculatorGraphics.DrawEllipse(penRed, 50, 50, 50, 50);
        CalculatorGraphics.Save();

        PlatformInvokeGDI32.COLORREF cl;
        cl.R = 255;
        cl.G = 0;
        cl.B = 0;

        PlatformInvokeGDI32.SetDCBrushColor(hDC, cl);
        PlatformInvokeGDI32.SetDCPenColor(hDC, cl);
        //PlatformInvokeGDI32.SetBkColor(hDC, cl);
        PlatformInvokeGDI32.Ellipse(hDC, 100, 100, 100, 100);
        PlatformInvokeGDI32.SaveDC(hDC);


        //Here we make a compatible device context in memory for screen device context.
        IntPtr hMemDC = PlatformInvokeGDI32.CreateCompatibleDC(hDC);


        //Create a compatible bitmap of window size and using screen device context.
        m_HBitmap = PlatformInvokeGDI32.CreateCompatibleBitmap(hDC, size.cx, size.cy);

        //As m_HBitmap is IntPtr we can not check it against null. For this purspose IntPtr.Zero is used.
        if (m_HBitmap != IntPtr.Zero)
        {
            //Here we select the compatible bitmap in memeory device context and keeps the refrence to Old bitmap.
            IntPtr hOld = (IntPtr)PlatformInvokeGDI32.SelectObject(hMemDC, m_HBitmap);
            //We copy the Bitmap to the memory device context.
            PlatformInvokeGDI32.BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, 0, 0, PlatformInvokeGDI32.SRCCOPY);
            //We select the old bitmap back to the memory device context.
            PlatformInvokeGDI32.SelectObject(hMemDC, hOld);
            //We delete the memory device context.
            PlatformInvokeGDI32.DeleteDC(hMemDC);
            //We release the screen device context.
            PlatformInvokeUSER32.ReleaseDC(hwnd, hDC);
            //Image is created by Image bitmap handle and returned.
            return System.Drawing.Image.FromHbitmap(m_HBitmap);
        }
        //If m_HBitmap is null retunrn null.
        return null;
  • 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-26T18:16:01+00:00Added an answer on May 26, 2026 at 6:16 pm

    I see how I misunderstood the definition of the Ellipse method.

    The last two parameters of the Ellipse method to refer to the distance from the left-most point and right-most point. Instead, they refer to the position of the right-most point and bottom-most point.

    The last two parameters of the DrawEllipse method do refer to the horizontal and vertical distance of the bottom-right point from the top-left point of the bounding rectangle.

    Ellipse(hDC, 100, 100, 200, 200);
    

    Means approximately the same thing as

    DrawEllipse(penRed, 100, 100, 100, 100); 
    

    Here is the MSDN documentation on the Ellipse method:

    hdc [in]
    A handle to the device context.

    nLeftRect [in]
    The x-coordinate, in logical coordinates, of the upper-left corner of the bounding rectangle.

    nTopRect [in]
    The y-coordinate, in logical coordinates, of the upper-left corner of the bounding rectangle.

    nRightRect [in]
    The x-coordinate, in logical coordinates, of the lower-right corner of the bounding rectangle.

    nBottomRect [in]
    The y-coordinate, in logical coordinates, of the lower-right corner of the bounding rectangle.

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

Sidebar

Related Questions

I'm using an Adorner in .NET 3.5, and I'm able to draw by overriding
I would like to be able to draw a file line using native Windows
I am able to read emails in from Microsoft Exchange using an IMAP Client
I'm using php and gd library to draw a triangle. I'm able to draw
I'd like to be able to draw on the root window in Linux. I.e.
Using Python I want to be able to draw text at different angles using
I'm using Emacs, with CLISP and Slime, and want to be able to draw
I am being able to draw the image using something like the code below:
I simply want the user to be able to draw on the screen with
I am able to display and save a screen device to DIB bitmap, and

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.