Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

How to capture image on button click of a specific panel in c# .net windows application?

Please Help Me.

Thanks in Advance.

Ankit Agarwal
Software Engineer
Posted

Beginning with .NET FrameWork 2.0, you have the Graphics.CopyFromScreen method that makes this easy.
C#
// required in addition to the usual Win Forms stuff
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

// path to the Desktop
private string deskTopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

// counter for number of snaps
private int snapCount = 0;

private void SaveControlImage(Control theControl)
{
    snapCount ++;

    Bitmap controlBitMap = new Bitmap(theControl.Width, theControl.Height);
    Graphics g = Graphics.FromImage(controlBitMap);
    g.CopyFromScreen(PointToScreen(theControl.Location), new Point(0,0), theControl.Size);

    // example of saving to the desktop
    controlBitMap.Save(deskTopPath + @"/snap_" + snapCount.ToString() + @".png", ImageFormat.Png);
}

// assume this is the Click EventHandler for a Button on a Form,
// and that a Panel named 'YourPanelName is what you want to copy
// the screen-image of, and save to a file
private void SnapShotControl_Click(object sender, EventArgs e)
{
    SaveControlImage(YourPanelName);
}
 
Share this answer
 
Comments
Member 15001218 9-Jan-21 0:49am    
CopyFromScreen is not working in latest framwork of the asp.net is there any alternate for that
BillWoodruff 9-Jan-21 2:21am    
Sorry, I don't use ASP; perhaps post a new question; be sure and clarify if you are asking about a screenshot, or an active window shot, or a browser screen shot.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900