Graphics.CopyFromScreen() problems

  • Thread starter Thread starter rory.groves
  • Start date Start date
R

rory.groves

C#, NET 2.0:

I am using CopyFromScreen to copy a background user control to a
foreground usercontrol to create the appearance of transparency.

It works perfectly 95% of the time. However it only paint what is
visible. So if there is another window on top of the application when
CopyFromScreen is called (such as when first starting the
application), then it paints part of the other window as my
usercontrol background.

Is there any way to export a region of the Graphics object into a
Bitmap without using CopyFromScreen?

Or, is there a CopyFromHDC() or equivalent?


Thanks.
 
[...]
Is there any way to export a region of the Graphics object into a
Bitmap without using CopyFromScreen?

That depends on where you got the Graphics object. But not per se, no.
You draw _into_ Graphics objects, not _from_ them.
Or, is there a CopyFromHDC() or equivalent?

Depending on exactly what you're trying to do, you might want to look at
Control.DrawToBitmap(), or possibly at using p/invoke to send a WM_PRINT
or WM_PRINTCLIENT message to the control in question.

It is possible something in WPF would do what you want, but I don't know
anything about that.

Pete
 
That depends on where you got the Graphics object.  But not per se, no.  
You draw _into_ Graphics objects, not _from_ them.

I do have access to the background control's graphics object, but, as
i mentioned, don't know how to "extract" the controul's bitmap into
another bitmap without using CopyFromScreen().

Here is a simplified representation of my code:

Rectangle rc = new
Rectangle(this.PointToScreen(foregroundControl.Location),
foregroundControl.Size);
using (Bitmap bitmap = new Bitmap(rc.Width, rc.Height,
PixelFormat.Format32bppArgb))
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(rc.Left, rc.Top, 0, 0, rc.Size);
foregroundControl.BackBitmap = bitmap;
}
foregroundControl.Invalidate(true);


Again, to re-iterate, CopyFromScreen() includes overlapping windows
and therefore messes up the foregroundControl's appearance. I need to
figure out how to copy the backgroundControl's bitmap without using
CopyFromScreen.


Thanks
 
Back
Top