码迷,mamicode.com
首页 > Windows程序 > 详细

C#网页转换为图片

时间:2018-02-27 14:57:13      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:oev   引用   browser   bit   windows   ext   art   body   ali   

调用:

引用 System.Windows.Forms 命名空间:

WebsiteToImage websiteToImage = new WebsiteToImage("页面路径", @"图片保存路径");
websiteToImage.Generate();

 

 

 

 

被调用代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Drawing;

using System.Threading;

using System.Windows.Forms;

using System.Drawing.Imaging;

using System.IO;

namespace Business {

 

public class WebsiteToImage {

       private Bitmap m_Bitmap;

      private string m_Url;

      private string m_FileName = string.Empty;

public WebsiteToImage(string url)

{

        m_Url = url;

}

/// </summary>
/// <param name="url">网页路径</param>
/// <param name="fileName">照片存储路径</param>

public WebsiteToImage(string url, string fileName)

{

    m_Url = url;

    m_FileName = fileName;

public Bitmap Generate()

{

       var m_thread = new Thread(_Generate);

      m_thread.SetApartmentState(ApartmentState.STA);

      m_thread.Start();

      m_thread.Join();

      return m_Bitmap;

}

private void _Generate()

{

     var browser = new WebBrowser{ScrollBarsEnabled = false };

     browser.Navigate(m_Url);

     browser.DocumentCompleted += WebBrowser_DocumentCompleted;

     while (browser.ReadyState != WebBrowserReadyState.Complete)

         {

             Application.DoEvents();

        }

      browser.Dispose();

    }

 

 

private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

    {

          var browser = (WebBrowser)sender;

         //宽,高

        browser.ClientSize = new Size(618,500);

        browser.ScrollBarsEnabled = false;

      //宽,高

     m_Bitmap = new Bitmap(618, 500);

     browser.BringToFront();

    browser.DrawToBitmap(m_Bitmap, browser.Bounds);

     if (m_FileName.Length > 0)

         { 

            m_Bitmap.SaveJPG100(m_FileName);

        }

    }

}

 

public static class BitmapExtensions

{

         public static void SaveJPG100(this Bitmap bmp, string filename)

          { 

             var encoderParameters = new EncoderParameters(1);

           encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);

          bmp.Save(filename, GetEncoder(ImageFormat.Jpeg), encoderParameters);

         }

      public static void SaveJPG100(this Bitmap bmp, Stream stream)

      {

         var encoderParameters = new EncoderParameters(1);

         encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);

        bmp.Save(stream, GetEncoder(ImageFormat.Jpeg), encoderParameters);

     }

 

  public static ImageCodecInfo GetEncoder(ImageFormat format)

   {

      var codecs = ImageCodecInfo.GetImageDecoders();

      foreach (var codec in codecs)

       {

            if (codec.FormatID == format.Guid)

             {

                 return codec;

            }

    }

     

      return null;

      }

   }

}

C#网页转换为图片

标签:oev   引用   browser   bit   windows   ext   art   body   ali   

原文地址:https://www.cnblogs.com/zlkkk/p/8478480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!