Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

screenshot of a winforms control through C#

Is it possible to get a screenshot of a control in winforms without actually displaying it on the screen? How about a webBrowser component?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes. This can be done.

I whipped up a sample program to do it. Forgive my naming conventions and code organization, this was whipped up very quickly. You can modify it to better fit your needs, but this shows the basics.

I have a single form with three controls:

  • button1: Button with the default settings.
  • button2: Button with the Visible property set to false and the Text set to "button2 - not visible"
  • webBrowser1: WebBrowser control with the visibility set to false, set the size to 250, 101 (just so it fits on my form. The size is relevant when you look at the capture at the bottom of my answer. You'll need to size it accordingly.)

here's the code in Form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            PrintInvisibleControl(button2, @"C:utton.jpg");
            PrintInvisibleControl(webBrowser1, @"C:webbrowser.jpg");


        }

        private void PrintInvisibleControl(Control myControl, string filename)
        {

            Graphics g = myControl.CreateGraphics();
            //new bitmap object to save the image        
            Bitmap bmp = new Bitmap(myControl.Width, myControl.Height);
            //Drawing control to the bitmap        
            myControl.DrawToBitmap(bmp, new Rectangle(0, 0, myControl.Width, myControl.Height));
            bmp.Save(filename, ImageFormat.Jpeg);
            bmp.Dispose();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www.microsoft.com");
        }

    }
}

This resulted in the following captures:

alt text

alt text


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...