GDI and Image displays

  • Thread starter Thread starter Glyn Meek
  • Start date Start date
G

Glyn Meek

I have a routine which uses the GDI to display an image using '.DrawImage'.

Sections of the image have been made transparent using '.MakeTransparent'.

Everything works well...

....BUT, I later need to replace the original image with a second image, also
with some transparent sections and cannot seem to find a way to do this. If
I merely use 'DrawImage to write the new image, sections of the old image
show through where the new image is transparent.

I have tried putting both images in Rectangles, and manipulating the
rectangles, but this doesn't seem to work either.

I tried to find a way to capture (and then restore) the original background
on the screen before the first image was drawn, but don't think this is
doable, and if it is I cannot find how to do it in the literature.

Any GDI experts out there that can give me a hint as to how to get rid of
the original image altogether? I can't just overwrite it with an image of
the background color (unless I can capture it in realtime before I start
drawing) as this may vary (particularly under XP where the background screen
has gradients)

Regards

Glyn J Meek
 
Check out this FAQ about how to take a screenshot using GDI+

http://www.syncfusion.com/FAQ/WinForms/FAQ_c73c.asp#q625q
*******Reprinted for convenience**************
C#]
internal class NativeMethods
{
[DllImport("user32.dll")]
public extern static IntPtr GetDesktopWindow();

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hwnd);

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern UInt64 BitBlt
(IntPtr hDestDC,
int x,
int y,
int nWidth,
int nHeight,
IntPtr hSrcDC,
int xSrc,
int ySrc,
System.Int32 dwRop);
}

// Save the screen capture into a jpg
public void SaveScreen()
{
Image myImage = new
Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
Graphics gr1 = Graphics.FromImage(myImage);
IntPtr dc1 = gr1.GetHdc();
IntPtr dc2 =
NativeMethods.GetWindowDC(NativeMethods.GetDesktopWindow());
NativeMethods.BitBlt(dc1, 0, 0,
Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height, dc2, 0, 0,
13369376);
gr1.ReleaseHdc(dc1);
myImage.Save("screenshot.jpg", ImageFormat.Jpeg);
}
*********************

Hope that helps,
Joel Martinez
Orlando .NET User Group
http://www.onetug.org
http://www.codecube.net
 
Back
Top