Scott Salyer
Scott Salyer

Reputation: 2475

Export ASP.NET Content to Image

Does anyone know of any tools where you can take the content of an HTML control (presuambly div) and export it to one of a few various image formats (jpg, png and bmp would suffice)? I'd prefer the rendering to be done server-side (GDI is fine) and just create an image I can return with an HttpHandler so it doesn't need saved if at all possible. I know a number of reporting tools (SSRS, Telerik Reporting) offer export to image options, but I can't figure out how they do this.

Upvotes: 1

Views: 2237

Answers (2)

Glory Raj
Glory Raj

Reputation: 17701

Well, If you want to take a screenshot of web page and save it is a image format(what ever you want) you can do like this....

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Web.Services;
using System.Text;
using System.Windows.Forms;//must be included for capturing screenshot

public partial class capture_webpage_screenshot : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSave_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(25, 25, 25, 25, bitmap.Size);
        bitmap.Save(@"c:\screenshot\myscreenshot.bmp", ImageFormat.Bmp);

    }
}

and you have to follow these steps:

Just you have to do the following steps.

  • Right click the project name
  • Add reference -> .net tab and then choose system.windows.forms namespace then press ok button.
  • In .aspx code behind file you have to import the System.Windows.Formsnamespace and that’s it.

By customizing parameters used in CopyFromScreen() function, you can figure out how to capture the small portion of the screen, if required.

Create a folder and give folder path in c# code just like above i have given. You can customize the code as well and give the dynamic name of bitmap file every time it is generated.

I hope it will helps you...

Upvotes: 0

Mustafa Shabib
Mustafa Shabib

Reputation: 740

You can spin up IE or some other browser on your server side and use GDI to take a screenshot of that window, but pinpointing a specific div will take more work.

If you need client-side screenshots, you'll need to use Java or something similar.

How to take screenshot of div from a webpage using C# and ASP.NET?

may also help.

Upvotes: 2

Related Questions