Re: Graphics.GetHdc/Graphics.FromImage/R2_MASKPEN Woos
From: Bob Powell [MVP] (bob_at__spamkiller_bobpowell.net)
Date: 01/11/05
- Next message: Bob Powell [MVP]: "Re: large bitmaps for printing (GDI+)"
- Previous message: gdi_plus_at_yahoo.com: "Re: Graphics.GetHdc/Graphics.FromImage/R2_MASKPEN Woos"
- In reply to: gdi_plus_at_yahoo.com: "Re: Graphics.GetHdc/Graphics.FromImage/R2_MASKPEN Woos"
- Messages sorted by: [ date ] [ thread ]
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(); > } >
- Next message: Bob Powell [MVP]: "Re: large bitmaps for printing (GDI+)"
- Previous message: gdi_plus_at_yahoo.com: "Re: Graphics.GetHdc/Graphics.FromImage/R2_MASKPEN Woos"
- In reply to: gdi_plus_at_yahoo.com: "Re: Graphics.GetHdc/Graphics.FromImage/R2_MASKPEN Woos"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|