Re: Graphics.GetHdc/Graphics.FromImage/R2_MASKPEN Woos

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Bob Powell [MVP] (bob_at__spamkiller_bobpowell.net)
Date: 01/11/05


Date: Tue, 11 Jan 2005 21:22:46 +0100

You're looking at it from the wrong angle. You cannot pass the handle of the
graphics object passed to you from a bitmap and successfully create a useful
GDI DC from it.

#1 for case 2 greate a compatible DC using the interop API's
#2 obtain a bitmap handle for the bitmap
#3 select it into the DC
#4 create a GDI+ object from the GDI handle and draw on it.

something like

Bitmap btmp=new Bitmap(200,200);
IntPtr gdiHande=CreateCompatibleDC(....);
IntPtr bmHandle=btmp.Handle();
Select(gdiHandle,bmHandle)
Graphics g=Graphics.FromHandle(gdiHandle)
Test(g)
... release the GDI DC
... select the old bitmap handle back into the DC
... destroy the gdi DC
etc.... etc.....

Obviously I've not compiled this but i'm sure you'll get the picture. (if
you'll pardon the pun)

-- 
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
<gdi_plus@yahoo.com> wrote in message 
news:1105459537.774095.88520@z14g2000cwz.googlegroups.com...
> Bob,
>
> I do not have a bitmap to draw on, I have a Graphics object.  My method
> is this:
> void TestDraw(Graphics g)
> This is part of a package that my customers use.  I have no telling
> whether they are passing a Graphics objects from a paint event (works
> fine) or from a Graphics.FromImage (problem).
> This is another code that shows the problem:
>
> // Test is part of a package that my customer can use
> // Note, that I have no control where g comes from
> private const uint SRCCOPY = 0x00CC0020;
> [DllImport("gdi32")] private static extern bool BitBlt(IntPtr hdcDest,
> int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int
> nXSrc, int nYSrc, uint dwRop);
> public void Test(Graphics g)
> {
> // 2 100 pixels boxes, red on the left, blue on the right
> g.FillRectangle(Brushes.Red, 0, 0, 100, 100);
> g.FillRectangle(Brushes.Blue, 100, 0, 100, 100);
>
> // now, I want to use GDI to blt the red box over the blue
> IntPtr hdc = g.GetHdc();
> // Case1: works fine
> // Case2: GDI does not "see" the surface of the Graphics object, the
> previous operations are ignored.
> // At this point, any GDI call that involves using the background of
> hdc (including ROP2 operations as in original problem)
> // do not work.  The background is pure black.
> BitBlt(hdc, 100, 0, 100, 100, hdc, 0, 0, SRCCOPY);
> g.ReleaseHdc(hdc);
> }
>
> Case1: Now if my customer uses this code, everything works fine:
> private void Form1_Paint(object sender,
> System.Windows.Forms.PaintEventArgs e)
> {
> Test(e.Graphics);
> }
>
> Case2: However, this is a problem:
> private void Form1_Paint(object sender,
> System.Windows.Forms.PaintEventArgs e)
> {
> Bitmap btmp = new Bitmap(200, 200);
> Graphics g = Graphics.FromImage(btmp);
> Test(g);   // PROBLEM!!!
> g.Dispose();
> e.Graphics.DrawImage(btmp, 0, 0, btmp.Width, btmp.Height);
> btmp.Dispose();
> }
> 


Relevant Pages

  • Re: How do I get a device context from an indexed bitmap?
    ... I am not sure how CreateDIBSection could help me, ... draw on it using GDI functions, such as RoundRect, etc? ... The MSDN documentation clearly states that you cannot get a Graphics object ... How do I get a device context handle to this bitmap so I can ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Cant StretchBlt an image loaded from my hard drive
    ... was trying to use a GDI function, StretchBlt, on a GDI+ graphics object ... backed by a bitmap. ... to new memory bitmap that is not initialized with the original bitmap's ... Using GDI on a GDI+ Graphics Object Backed by a Bitmap ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Copy custom region from Bitmap to another Bitmap
    ... Answer those GDI+ questions with the GDI+ FAQ ... > public static Bitmap Copy ... > int width = pt2.X-pt1.X; ... > Bitmap bmpCpy = new Bitmap; ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Can GDI+ create such a graph?
    ... I am new to GDI+, our client is interested in have their web-based ... project management generate some roadmaps that look like the one ... Create a graphics object ... Using drawing commands of graphics object to draw to bitmap ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: How do I get a device context from an indexed bitmap?
    ... draw on it using GDI functions, such as RoundRect, etc? ... The MSDN documentation clearly states that you cannot get a Graphics object ... How do I get a device context handle to this bitmap so I can ...
    (microsoft.public.dotnet.languages.vb)