本帖最后由 chate 于 2012-02-09 16:20:19 编辑

解决方案 »

  1.   

    webform实现太麻烦,思路个人以为就是,操作ie等能html解析器。
    另外保存快照,现在应该都是html吧简单的,
    你可以把用webform调用winform运行,生成快照
      

  2.   

    这和ie也有关系吗?这是在服务器上的网页程序啊~~
    “把用webform调用winform运行,生成快照”,能给个例子吗?
    谢谢!
      

  3.   

    代码写得很明白了啊
    主要用到了WebBrowser.DrawToBitmap方法(继承自WebBrowserBase)。public void DrawToBitmap (
    Bitmap bitmap,
    Rectangle targetBounds
    )WebForm要想用WebBrowser类,估计要添加引用“System.Windows.Forms”。在项目上点右键-“添加引用”-找到对应你网站.net框架版本的System.Windows.Forms并添加,就有System.Windows.Forms.WebBrowser了
      

  4.   

    其实我只会asp,但查了很久资料知道asp里不能截图,无奈之中就找了这段代码。
    我把整个页面(1.aspx)代码都贴出来,能帮我直接修改吗?
    先谢了!<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="utf-8" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    <body>
    <%
    using System;
    using System.Drawing;
    using System.Threading;
    using System.Windows.Forms;
    /// <summary>
    /// 生成网页快照
    /// </summary>
    publicclassHtmlToImg
    {
        Bitmap m_Bitmap;
        string m_Url;
        int m_BrowserWidth, m_BrowserHeight, m_ThumbnailWidth, m_ThumbnailHeight; //这里出现错误信息:“编译器错误信息: CS1513: 应输入 }”
        public HtmlToImg(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
        {
            m_Url = Url;
            m_BrowserHeight = BrowserHeight;
            m_BrowserWidth = BrowserWidth;
            m_ThumbnailWidth = ThumbnailWidth;
            m_ThumbnailHeight = ThumbnailHeight;
        }
        publicstaticBitmap GetHtmlToImg(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
        {
            HtmlToImg thumbnailGenerator = newHtmlToImg(Url, BrowserWidth, BrowserHeight, ThumbnailWidth, ThumbnailHeight);
            return thumbnailGenerator.GenerateHtmlToImgImage();
        }
        publicBitmap GenerateHtmlToImgImage()
        {
            Thread m_thread = newThread(newThreadStart(_GenerateHtmlToImgImage));
            m_thread.SetApartmentState(ApartmentState.STA);
            m_thread.Start();
            m_thread.Join();
            return m_Bitmap;
        }
        privatevoid _GenerateHtmlToImgImage()
        {
            WebBrowser m_WebBrowser = newWebBrowser();
            m_WebBrowser.ScrollBarsEnabled = false;
            m_WebBrowser.Navigate(m_Url);
            m_WebBrowser.DocumentCompleted += newWebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
            while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
                Application.DoEvents();
            m_WebBrowser.Dispose();
        }
        privatevoid WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser m_WebBrowser = (WebBrowser)sender;
            m_WebBrowser.ClientSize = newSize(this.m_BrowserWidth, this.m_BrowserHeight);
            m_WebBrowser.ScrollBarsEnabled = false;
            m_Bitmap = newBitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
            m_WebBrowser.BringToFront();
            m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
            m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, null, IntPtr.Zero);
        }
    }//调用方法:        Bitmap bmp = HtmlToImg.GetHtmlToImg("http://www.baidu.com/", 800, 600, 800, 600);
            MemoryStream stream = newMemoryStream();
            bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] buff = stream.ToArray();
            //直接显示图片
            //Response.ContentType = "image/Jpeg";
            //Response.BinaryWrite(buff);
            //Response.End();        //保存图片
            FileStream fs = newFileStream(Server.MapPath(DateTime.Now.Date.ToShortDateString() + ".jpg"), FileMode.Create);
            stream.WriteTo(fs);
            stream.Close();
            fs.Close();
    %>
    </body>
    </html>
      

  5.   


    public class HtmlToImgHtmlToImg这是一个单独的类。。
    调用方法那段代码你应该放到一个事件处理程序中,比如按钮的点击事件。不然你放到page_load事件里也行,页面一打开就执行了。
      

  6.   

    我不会.net,是硬着头皮上的。你能帮我修改测试上述代码吗?
      

  7.   

    没想到asp.net和asp差别这么大!没有你在QQ上的耐心解释和远程,就是给了我代码也不会用!
    最高只能加到100分了,谢谢!