C# Take ScreenShot of .net control within applicat

2019-02-05 05:01发布

Does anyone know how to take a screenshot using C# and limit it to take a picture of a specific container/portion of an application. I do not want the whole screen or whole window of the application.

My Panel is simply called: panel1 User would click a "button" and take screenshot of panel1 and attach to email.

I would like to take a screen shot of that section only and then save locally to the C:\ Drive and/or attach or embed into an outlook email.

I read other various things on the internet but most of them had to deal with creating complex changes in take a screenshot of a web browser control which I am not looking for.

5条回答
Summer. ? 凉城
2楼-- · 2019-02-05 05:19

Slight modification of Killercam's answer:

    public static Bitmap takeComponentScreenShot(Control control)
    {
        // find absolute position of the control in the screen.
        Control ctrl  = control;
        Rectangle rect = new Rectangle(Point.Empty, ctrl.Size);
        do
        {
            rect.Offset(ctrl.Location);
            ctrl = ctrl.Parent;
        }
        while (ctrl != null);

        Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
        Graphics g = Graphics.FromImage(bmp);

        g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);

        return bmp;
    }

This method will take screenshot immediately. For example, if you change visibility of a button within the panel just before calling this function, the bitmap will contain the button. If this is not desired, use keyboardP's answer.

查看更多
SAY GOODBYE
3楼-- · 2019-02-05 05:28

I've found this to save a control as bitmap:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void Button1_Click(object sender, EventArgs e)
    {
        SaveAsBitmap(panel1,"C:\\path\\to\\your\\outputfile.bmp");
    }

    public void SaveAsBitmap(Control control, string fileName)
    {   
        //get the instance of the graphics from the control
        Graphics g = control.CreateGraphics();

        //new bitmap object to save the image
        Bitmap bmp = new Bitmap(control.Width, control.Height);

        //Drawing control to the bitmap
        control.DrawToBitmap(bmp, new Rectangle(0, 0, control.Width, control.Height));

        bmp.Save(fileName);
        bmp.Dispose();

    }
}

I've found something about outlook here, but I couldn't test it, because I don't have outlook installed on my PC.

查看更多
Animai°情兽
4楼-- · 2019-02-05 05:34

If you just want to the Panel's screenshot, you can use the built-in DrawToBitmap method.

Bitmap bmp = new Bitmap(myPanel.Width, myPanel.Height);
myPanel.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
bmp.Save(@"C:\MyPanelImage.bmp");

Just note that some controls may not work with this functionality such as the WebBrowser and RichTextBox controls but it should work for most other controls (textbox, labels etc..)

查看更多
Evening l夕情丶
5楼-- · 2019-02-05 05:36

An improvement to Asif´s answer:

public static Bitmap takeComponentScreenShot(Control control)
{
    // find absolute position of the control in the screen.
    Rectangle rect=control.RectangleToScreen(control.Bounds);

    Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
    Graphics g = Graphics.FromImage(bmp);

    g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);

    return bmp;
}
查看更多
老娘就宠你
6楼-- · 2019-02-05 05:42

I do this using something like

public static void TakeCroppedScreenShot(
    string fileName, int x, int y, int width, int height, ImageFormat format)
{
    Rectangle r = new Rectangle(x, y, width, height);
    Bitmap bmp = new Bitmap(r.Width, r.Height, PixelFormat.Format32bppArgb);
    Graphics g = Graphics.FromImage(bmp);
    g.CopyFromScreen(r.Left, r.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
    bmp.Save(fileName, format);
}

I hope this helps

查看更多
登录 后发表回答