gdi_plus
1/11/2005 4:06:00 PM
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();
}