Windows Develop Bookmark and Share   
 index > Windows Forms General > Capturing Panel as Bitmap using VS 2003 C#
 

Capturing Panel as Bitmap using VS 2003 C#

I located a post on how to accomplish this task using VS 2005. I have an older project that we are not ready to upgrade into VS 2005 or 2008. I have an open task to modify functionality involving a panel. The panel is initialized and the background image is set to an existing image. The existing image is one of an electronic form that we stored in a SQL table using SQL Server. I have added a picturebox that overlays this background image. The picturebox displays a signature of the user captured using an USB signature pad. Once this signature is applied I need to convert it, along with the background as a single bitmap. The user needs the ability to print this or send to their optical system.

We use a similar method in projects developed under 2005, by utilizing the DrawToBitmap method. This method is not available in 2003. Therefore I am looking for a solution to accomplish this same task. The following is the code from a 2005 project, I need to do something similar in 2003.

...

PrintForm pf = new PrintForm();

pf.Bitmaps.Add(FormUtil.GetBitmap(this.panel1));

pf.Print();

...

public class FormUtil

{

public static Bitmap GetBitmap(Control c)

{

if (c.Parent is TabPage)

{

TabControl tc = (TabControl)c.Parent.Parent;

TabPage tp = (TabPage)c.Parent;

int cur = tc.SelectedIndex;

tc.SelectedTab = tp;

Bitmap bm = new Bitmap(c.Width, c.Height);

c.DrawToBitmap(bm, new Rectangle(0, 0, bm.Width, bm.Height));

tc.SelectedIndex = cur;

return bm;

}

else

{

Bitmap bm = new Bitmap(c.Width, c.Height);

c.DrawToBitmap(bm, new Rectangle(0, 0, bm.Width, bm.Height));

return bm;

}

}

}

findev  Friday, March 21, 2008 4:29 PM

Bob Powell has a good example on how to do this on his webpage. If you ever need any gdi help hes got tons of examples there.

http://www.bobpowell.net/capture.htm

Code Snippet

[DllImport("gdi32.dll")]

private static extern bool BitBlt(

IntPtr hdcDest, // handle to destination DC

int nXDest, // x-coord of destination upper-left corner

int nYDest, // y-coord of destination upper-left corner

int nWidth, // width of destination rectangle

int nHeight, // height of destination rectangle

IntPtr hdcSrc, // handle to source DC

int nXSrc, // x-coordinate of source upper-left corner

int nYSrc, // y-coordinate of source upper-left corner

System.Int32 dwRop // raster operation code

);

[DllImport("User32.dll")]

public extern static System.IntPtr GetDC(System.IntPtr hWnd);


[DllImport("User32.dll")]

public extern static int ReleaseDC(System.IntPtr hWnd, System.IntPtr

void SaveImage()

{

System.IntPtr srcDC=GetDC(this.pictureBox1.Handle);

Bitmap bm=new Bitmap(this.pictureBox1.Width,this.pictureBox1.Height);

Graphics g=Graphics.FromImage(bm);

System.IntPtr bmDC=g.GetHdc();

BitBlt(bmDC,0,0,bm.Width,bm.Height,srcDC,0,0,0x00CC0020 /*SRCCOPY*/);

ReleaseDC(this.pictureBox1.Handle, srcDC);

g.ReleaseHdc(bmDC);

g.Dispose();
}

AbdElRaheim  Friday, March 21, 2008 4:39 PM

Bob Powell has a good example on how to do this on his webpage. If you ever need any gdi help hes got tons of examples there.

http://www.bobpowell.net/capture.htm

Code Snippet

[DllImport("gdi32.dll")]

private static extern bool BitBlt(

IntPtr hdcDest, // handle to destination DC

int nXDest, // x-coord of destination upper-left corner

int nYDest, // y-coord of destination upper-left corner

int nWidth, // width of destination rectangle

int nHeight, // height of destination rectangle

IntPtr hdcSrc, // handle to source DC

int nXSrc, // x-coordinate of source upper-left corner

int nYSrc, // y-coordinate of source upper-left corner

System.Int32 dwRop // raster operation code

);

[DllImport("User32.dll")]

public extern static System.IntPtr GetDC(System.IntPtr hWnd);


[DllImport("User32.dll")]

public extern static int ReleaseDC(System.IntPtr hWnd, System.IntPtr

void SaveImage()

{

System.IntPtr srcDC=GetDC(this.pictureBox1.Handle);

Bitmap bm=new Bitmap(this.pictureBox1.Width,this.pictureBox1.Height);

Graphics g=Graphics.FromImage(bm);

System.IntPtr bmDC=g.GetHdc();

BitBlt(bmDC,0,0,bm.Width,bm.Height,srcDC,0,0,0x00CC0020 /*SRCCOPY*/);

ReleaseDC(this.pictureBox1.Handle, srcDC);

g.ReleaseHdc(bmDC);

g.Dispose();
}

AbdElRaheim  Friday, March 21, 2008 4:39 PM

You can use google to search for other answers

Custom Search

More Threads

• Multiline Textbox
• New to .NET and UIP Application Block
• ANSWER IN NEED FOR *.MSI FILES
• KeyPreview = true, how do you then detect enter/return?
• Combobox with display member and value???
• same context menu strip on multiple controls???Possible????
• why WebBrowser.ShowPrintDialog only prints out the first page
• Persisting User Data across application executions
• How to change single screen resolution with multiple displays
• How do you execute the text in a rich text box control?