1

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;
}
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
Kratika Sharma
  • 291
  • 1
  • 5
  • 16

1 Answers1

1

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:

Image Captured

Community
  • 1
  • 1
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
  • it tried but on server it is not working String filepath = Server.MapPath(StatePath); Bitmap captureBitmap = new Bitmap(xheight, xwidth, PixelFormat.Format32bppArgb); System.Drawing.Rectangle captureRectangle = Screen.AllScreens[0].Bounds;//capture our Current Screen Graphics captureGraphics = Graphics.FromImage(captureBitmap); //Creating a New Graphics Object captureGraphics.CopyFromScreen(182, 207, 0, 0, captureRectangle.Size); captureBitmap.Save(filepath, ImageFormat.Jpeg); – Kratika Sharma May 13 '15 at 14:30
  • which method you tried?? Any exceptions you are getting?? – Guruprasad J Rao May 14 '15 at 03:25
  • i m not getting but i think this line not working on server captureGraphics.CopyFromScreen(182, 207, 0, 0, captureRectangle.Size); captureBitmap.Save(filepath, ImageFormat.Jpeg); – Kratika Sharma May 14 '15 at 08:43
  • which method you are trying?? did you try first method?? – Guruprasad J Rao May 14 '15 at 08:47
  • @KratikaSharma.. Updated the answer!! Check and let me know.. :) – Guruprasad J Rao May 14 '15 at 09:15
  • i have tried all above methods but nothing is working. – Kratika Sharma May 14 '15 at 09:22
  • Can you post your aspx page's button control or complete aspx page?? because the above update whatever I've given worked for me and then I posted it here!! – Guruprasad J Rao May 14 '15 at 09:24
  • please have a look i already have pasted it. is there any other way to send my test pages? – Kratika Sharma May 14 '15 at 10:38
  • you have pasted `.cs` code behind file codes.. I asked designer code!! – Guruprasad J Rao May 14 '15 at 10:40
  • its working on local host but when i uploaded it on server its not working. – Kratika Sharma May 14 '15 at 11:48
  • System.ComponentModel.Win32Exception: The handle is invalid – Kratika Sharma May 14 '15 at 13:59
  • [Win32Exception (0x80004005): The handle is invalid] – Kratika Sharma May 14 '15 at 13:59
  • [Win32Exception (0x80004005): The handle is invalid] System.Drawing.Graphics.CopyFromScreen(Int32 sourceX, Int32 sourceY, Int32 destinationX, Int32 destinationY, Size blockRegionSize, CopyPixelOperation copyPixelOperation) +653 System.Drawing.Graphics.CopyFromScreen(Int32 sourceX, Int32 sourceY, Int32 destinationX, Int32 destinationY, Size blockRegionSize) +35 System.Drawing.Graphics.CopyFromScreen(Point upperLeftSource, Point upperLeftDestination, Size blockRegionSize) +54 testCapture.CaptureMy() in d:\WeatherPortal\testCapture.aspx.cs:121 – Kratika Sharma May 14 '15 at 14:06
  • @KrathikaSharma.. That's because there is no screen on the server. It worked on your development platform because the server was your client screen. Now since you deployed your code into some other server, it will not capture and it is explained **[here](https://social.msdn.microsoft.com/Forums/en-US/74e789ea-160b-4b89-8296-3274d1bff1de/copyfromscreen-giving-error-in-server?forum=csharplanguage)** . If you want to capture the clients screen, it will be with javascript code. Alternatively you can visit **[here](http://www.csharphelp.com/2006/11/capturing-the-screen-image-using-c/)** – Guruprasad J Rao May 15 '15 at 03:27
  • Anytime!! Happy coding.. :) – Guruprasad J Rao May 15 '15 at 07:16
  • link u reffered is great but there is a problem on local its fine but on server its saving blank(black ) image.. – Kratika Sharma Jul 03 '15 at 08:50
  • @KratikaSharma. I have explained why you cannot achieve that in my previous comments!! – Guruprasad J Rao Jul 03 '15 at 08:58
  • i was asking because u reffered me http://www.csharphelp.com/2006/11/capturing-the-screen-image-using-c/ – Kratika Sharma Jul 03 '15 at 09:01
  • so i thought u might have any idea.. thanks for ur support until now – Kratika Sharma Jul 03 '15 at 09:02