简体   繁体   中英

Screenshot in C#/ASP.NET

I am trying to take screen shot in C# but its not working.

protected void btnscreenshot_click(object sender, EventArgs e)
{
    Thread thread = new Thread(GenerateThumbnail);
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join();
}
private void GenerateThumbnail()
{
    WebBrowser webrowse = new WebBrowser();
    webrowse.ScrollBarsEnabled = false;
    webrowse.AllowNavigation = true;
    webrowse.Navigate("www.mindfiresolutions.com");
    //webrowse.Width = 1024;
    //webrowse.Height = 768;
    webrowse.Width = 1024;
    webrowse.Height = 1024;
    webrowse.DocumentCompleted += webbrowse_DocumentCompleted;
    while (webrowse.ReadyState != WebBrowserReadyState.Complete)
    {
        System.Windows.Forms.Application.DoEvents();
    }
}
private void webbrowse_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
                    WebBrowser webrowse = sender as WebBrowser;
    //Bitmap bitmap = new Bitmap(webrowse.Width, webrowse.Height);
    Bitmap bitmap = new Bitmap(1024, 1024);
    webrowse.DrawToBitmap(bitmap, webrowse.Bounds);
    MemoryStream stream = new MemoryStream();
    bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
    bitmap.Save(Server.MapPath("~/Data/Screen.jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg);
    byte[] strbytes = stream.ToArray();
    imgscreenshot.Visible = true;
    imgscreenshot.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(strbytes);
    string dd = imgscreenshot.ImageUrl;
}

You can try below code from this SOURCE

ScreenCapture sc = new ScreenCapture();
// capture entire screen, and save it to a file
Image img = sc.CaptureScreen();
this.imageDisplay.Image = img;
// capture this window, and save it
sc.CaptureWindowToFile(this.Handle,"C:\\temp2.gif",ImageFormat.Gif);

Or alternatively you can do as below:

Rectangle bounds = Screen.GetBounds(Point.Empty);
using(Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
    using(Graphics g = Graphics.FromImage(bitmap))
    {
         g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
    }
    bitmap.Save("test.jpg", ImageFormat.Jpeg);
}

for capturing current window

 Rectangle bounds = this.Bounds;
 using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
 {
    using (Graphics g = Graphics.FromImage(bitmap))
    {
        g.CopyFromScreen(new Point(bounds.Left,bounds.Top), Point.Empty, bounds.Size);
    }
    bitmap.Save("C://test.jpg", ImageFormat.Jpeg);
 }

SO SOURCE

UPDATE:

I've created a dummy project and onclick of a button I was capturing screenshot and below is the sample code:

In default.aspx

<asp:Button ID="btnCapture" runat="server" OnClick="btnCapture_Click" Text="Capture Screen"/>

default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using ScreenCaptureDemo;
using System.Windows.Forms;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnCapture_Click(object sender, EventArgs e)
    {
        Rectangle bounds = Screen.GetBounds(Point.Empty);
        using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
            }
            bitmap.Save(Server.MapPath("~/Content/test.jpg"), ImageFormat.Jpeg); //Change Content to any folder name you desire
        }
    }
}

and below is the image I got:

拍摄的影像

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM