【问题标题】:How to convert .aspx to Image in asp.net with forms authentication如何使用表单身份验证将 .aspx 转换为 asp.net 中的图像
【发布时间】:2014-05-31 12:34:27
【问题描述】:

我试过这个代码

    public System.Drawing.Bitmap CaptureWebPage(string 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(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));

        return bmp; // return the bitmap for processing
    }
    protected void btnConvert_Click(object sender, EventArgs e)
    {
        Bitmap bitmap = new Bitmap(CaptureWebPage(txtUrl.Text));
        Response.ContentType = "image/jpeg";
        bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
        bitmap.Dispose();
        bitmap.Dispose();
        Response.End();
    }

我能够捕获 google.com,但无法捕获我们网站的图像,因为它包含表单身份验证,因此它将我重定向到登录页面并捕获登录页面。请帮助通过身份验证。我们可以使用会话、cookie 或任何与 WebBrowser 一起验证它的东西。请提出建议。

【问题讨论】:

    标签: html asp.net image converter


    【解决方案1】:

    在您网站上的预期页面上尝试:

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            System.IO.StringWriter sw = new System.IO.StringWriter(sb);
            Page.RenderControl(new HtmlTextWriter(sw));
    

    然后使用字符串生成器中的 HTML 来呈现页面,如下例所示(参考:Convert HTML string to image):

    using System;
    using System.Drawing;
    using System.Threading;
    using System.Windows.Forms;
    
        class Program
        {
            static void Main(string[] args)
            {
                var source =  @"
                <!DOCTYPE html>
                <html>
                    <body>
                        <p>An image from W3Schools:</p>
                        <img 
                            src=""http://www.w3schools.com/images/w3schools_green.jpg"" 
                            alt=""W3Schools.com"" 
                            width=""104"" 
                            height=""142"">
                    </body>
                </html>";
                StartBrowser(source);
                Console.ReadLine();
            }
    
            private static void StartBrowser(string source)
            {
                var th = new Thread(() =>
                {
                    var webBrowser = new WebBrowser();
                    webBrowser.ScrollBarsEnabled = false;
                    webBrowser.DocumentCompleted +=
                        webBrowser_DocumentCompleted;
                    webBrowser.DocumentText = source;
                    Application.Run();
                });
                th.SetApartmentState(ApartmentState.STA);
                th.Start();
            }
    
            static void 
                webBrowser_DocumentCompleted(
                object sender, 
                WebBrowserDocumentCompletedEventArgs e)
            {
                var webBrowser = (WebBrowser)sender;
                using (Bitmap bitmap = 
                    new Bitmap(
                        webBrowser.Width, 
                        webBrowser.Height))
                {
                    webBrowser
                        .DrawToBitmap(
                        bitmap, 
                        new System.Drawing
                            .Rectangle(0, 0, bitmap.Width, bitmap.Height));
                    bitmap.Save(@"filename.jpg", 
                        System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-10
      • 1970-01-01
      • 2013-02-06
      • 2017-09-28
      相关资源
      最近更新 更多