How to create a graphics object of an entire Form?

G

Guest

I am working on a Windows Forms application that uses the MDI style. I am
using C# in VS.NET 2003 . I want to provide a feature that allows the user
to capture the entire form's image and save it to a file.

The problem I have run into is that I am currently only capturing the client
area of the form. This doesn't include the form's title bar and borders. I
need to include the title bar in the image since the title bar's text has
useful information. I have included the code I am using to generate the
image below.

How can I modify this code to capture the entire form?

Thanks,
Dave

======== Code ========

public void CreateFormGraphic( Form form, string filename )
{
Graphics graphic = form.CreateGraphics(); // Client area only!!!???
Size s = form.ClientSize;
this.displayImage = new Bitmap( s.Width, s.Height, graphic );

Graphics memGraphic = Graphics.FromImage( this.displayImage );
IntPtr dc1 = graphic.GetHdc();
IntPtr dc2 = memGraphic.GetHdc();
User32DllImports.BitBlt( dc2, 0, 0, s.Width, s.Height, dc1, 0, 0,
User32DllImports.SRCCOPY );

graphic.ReleaseHdc( dc1 );
memGraphic.ReleaseHdc( dc2 );

this.displayImage.Save( filename, ImageFormat.Jpeg );
this.displayImage.Dispose();
this.displayImage = null;
}

=====================
 
S

simida

Maybe
Size s = form.ClientSize should be changed to this

Size s = form.Size?


Dave Leach 写é“:
 
J

Jeffrey Tan[MSFT]

Hi Dave,

Yes, I can reproduce this behavior. It seems that the Graphics object
obtained from Control.CreateGraphics always uses the client DC. To
workaround this issue, you may p/invoke GetWindowDC win32 API to obtain the
entire window DC, the code snippet below works on my side:

[DllImport("gdi32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool BitBlt(IntPtr hDC, int x, int y, int nWidth, int
nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);
[DllImport("user32.dll", ExactSpelling=true)]
public static extern IntPtr GetWindowDC(IntPtr hWnd);

Bitmap displayImage =null;
const int SRCCOPY=0x00CC0020;
public void CreateFormGraphic( Form form, string filename )
{
Size s = form.Size;
this.displayImage = new Bitmap( s.Width, s.Height );

Graphics memGraphic = Graphics.FromImage( this.displayImage );
IntPtr dc1 = GetWindowDC(form.Handle);
IntPtr dc2 = memGraphic.GetHdc();
BitBlt( dc2, 0, 0, s.Width, s.Height, dc1, 0, 0,
SRCCOPY );

memGraphic.ReleaseHdc( dc2 );

this.displayImage.Save( filename);
this.displayImage.Dispose();
this.displayImage = null;
}

private void button1_Click(object sender, System.EventArgs e)
{
CreateFormGraphic(this, @"c:\test.bmp");
}
Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Jeffrey,

Good to hear from you again! The solution you provided is just what I
needed and works great.

Thanks,
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top