您的位置:首页 > 其它

convert div content into image

2013-05-15 18:01 477 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Threading;

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

    System.Drawing.Bitmap bitMap=null;
    protected void btnGenerateImage_Click(object sender, EventArgs e)
    {
        LetUserDownload();
    }
    public void DisplayImage()
    {
        //ApartmentState:specifies the state of a system.threading.thread
        //STA:Thread will create and enter a single-threaded apartment
        Thread t = new Thread(CaptureWebPageToDisplay);
        t.SetApartmentState(ApartmentState.STA);
        t.Start("http://localhost/");
        Thread.Sleep(5000);
        Response.ContentType = "image/jpeg";
        bitMap.Save(Response.OutputStream, ImageFormat.Jpeg);
        bitMap.Dispose();
        bitMap.Dispose();
        Response.Flush();
    }
    public void LetUserDownload()
    {
        //ApartmentState:specifies the state of a system.threading.thread
        //STA:Thread will create and enter a single-threaded apartment
        Thread t = new Thread(CaptureWebPageToDisplay);
        t.SetApartmentState(ApartmentState.STA);
        t.Start("http://localhost/");
        Thread.Sleep(5000);
        Response.Clear();
        Response.ContentType = "image/jpeg";
        Response.AddHeader("content-disposition", "attachment;filename=imageafda.jpeg");
        string filePath = Server.MapPath("~/Images")+ "/FileName.Jpeg";
        bitMap.Save(filePath , ImageFormat.Jpeg);
        bitMap.Dispose();
        Response.WriteFile(filePath);
        
        Response.Flush();
    }
    public void CaptureWebPageToDisplay(object URL)
    {
        // create a hidden web browser, which will navigate to the page 
        System.Windows.Forms.WebBrowser web = new System.Windows.Forms.WebBrowser();
        // we don't want scrollbars on our image 
        web.ScrollBarsEnabled = false;
        // don't let any errors shine through 
        web.ScriptErrorsSuppressed = true;
        // let's load up that page! 
        web.Navigate((string)URL);

        // wait until the page is fully loaded 
        while (web.ReadyState != WebBrowserReadyState.Complete)
            System.Windows.Forms.Application.DoEvents();
        System.Threading.Thread.Sleep(1500); // allow time for page scripts to update 
        // the appearance of the page 

        // set the size of our web browser to be the same size as the page 
        int width = web.Document.Body.ScrollRectangle.Width;
        int height = web.Document.Body.ScrollRectangle.Height;
        web.Width = width;
        web.Height = height;
        // a bitmap that we will draw to 
        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height);
        // draw the web browser to the bitmap 
        web.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, width, height));
        bitMap = bmp;
    }
   }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: