You must Sign In to post a response.
  • Category: ASP.NET

    I tried this code but shows error

    I am capturing the webpage as screen shot and save the screen shot in folder.

    Capture image code as follows

    protected void btnCapture_Click(object sender, EventArgs e)
    {

    Bitmap bitmap = new Bitmap
    (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);

    Graphics graphics = Graphics.FromImage(bitmap as System.Drawing.Image);
    graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);

    bitmap.Save(Server.MapPath(@"File\ScreenShot.bmp"), ImageFormat.Bmp);

    }

    But when i run the above code shows error as follows
    A generic error occurred in GDI+.

    please kindly help me what is the problem in my above code.
  • #762715
    Hello Rao,

    Refer the below code :

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td><b>Enter URL:</b></td>
    <td><asp:TextBox ID="txtweburl" runat="server" /></td>
    </tr>
    <tr>
    <td></td>
    <td>
    <asp:Button ID="btnscreenshot" Text="Capture Screen" runat="server" OnClick="btnscreenshot_click" />
    </td>
    </tr>
    </table>
    <br />
    <asp:Image ID="imgscreenshot" runat="server" Visible = "false" Height="550" Width="1000" />
    </div>
    </form>
    </body>
    </html>

    Code - behind :

    using System.Drawing;
    using System.IO;
    using System.Threading;
    using System.Windows.Forms;

    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(txtweburl.Text.Trim());
    webrowse.Width = 1024;
    webrowse.Height = 768;
    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);
    webrowse.DrawToBitmap(bitmap, webrowse.Bounds);
    MemoryStream stream = new MemoryStream();
    bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
    byte[] strbytes = stream.ToArray();
    imgscreenshot.Visible = true;
    imgscreenshot.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(strbytes);
    }


    See the attached image. It's an output of this code.

    Hope this will work for you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment

  • #762716
    Hello Rao,

    I have worked on your code and finally solve it. You can refer this code :

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="ButtonCapture" runat="server" Text="Capture Screen"
    onclick="ButtonCapture_Click" />
    <br />
    <asp:Image ID="imgscreenshot" runat="server" Visible = "false" Height="500" Width="800" />
    </div>
    </form>
    </body>
    </html>


    Code - behind :

    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Windows.Forms;

    protected void ButtonCapture_Click(object sender, EventArgs e)
    {
    Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

    Graphics graphics = Graphics.FromImage(bitmap as System.Drawing.Image);
    graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);

    bitmap.Save(Server.MapPath(@"Images\Screenshot.bmp"), ImageFormat.Bmp);

    imgscreenshot.Visible = true;
    imgscreenshot.ImageUrl = "~/Images/Screenshot.bmp";
    }


    See the attached image. It's an output i have got using this code.

    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment


Sign In to post your comments