最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c# 實(shí)現(xiàn)網(wǎng)頁(yè)加載后將頁(yè)面截取為長(zhǎng)圖片

 更新時(shí)間:2021年01月13日 10:56:39   作者:UP技術(shù)控  
這篇文章主要介紹了c# 實(shí)現(xiàn)網(wǎng)頁(yè)加載后將頁(yè)面截取為長(zhǎng)圖片的方法,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下

背景

最近再做一個(gè)需求,需要對(duì)網(wǎng)頁(yè)生成預(yù)覽圖,如下圖

但是網(wǎng)頁(yè)千千萬(wàn),總不能一個(gè)個(gè)打開(kāi),截圖吧;于是想著能不能使用代碼來(lái)實(shí)現(xiàn)網(wǎng)頁(yè)的截圖。其實(shí)要實(shí)現(xiàn)這個(gè)功能,無(wú)非就是要么實(shí)現(xiàn)一個(gè)仿真瀏覽器,要么調(diào)用系統(tǒng)瀏覽器,再進(jìn)行截圖操作。

代碼實(shí)現(xiàn)

1、啟用線程Thread

void startPrintScreen(ScreenShotParam requestParam)
  {
   Thread thread = new Thread(new ParameterizedThreadStart(do_PrintScreen));
   thread.SetApartmentState(ApartmentState.STA);
   thread.Start(requestParam);
   if (requestParam.Wait)
   {
    thread.Join();
    FileInfo result = new FileInfo(requestParam.SavePath);
    long minSize = 1 * 1024;// 太小可能是空白圖,重抓
    int maxRepeat = 2;    
    while ((!result.Exists || result.Length <= minSize) && maxRepeat > 0)
    {
     thread = new Thread(new ParameterizedThreadStart(do_PrintScreen));
     thread.SetApartmentState(ApartmentState.STA);
     thread.Start(requestParam);
     thread.Join();
     maxRepeat--;
    }
   }
  }

2、模擬瀏覽器WebBrowser

void do_PrintScreen(object param)
  {
   try
   {
    ScreenShotParam screenShotParam = (ScreenShotParam)param;
    string requestUrl = screenShotParam.Url;
    string savePath = screenShotParam.SavePath;
    WebBrowser wb = new WebBrowser();
    wb.ScrollBarsEnabled = false;
    wb.ScriptErrorsSuppressed = true;
    wb.Navigate(requestUrl);
    logger.Debug("wb.Navigate");
    DateTime startTime = DateTime.Now;
    TimeSpan waitTime = new TimeSpan(0, 0, 0, 10, 0);// 10 second
    while (wb.ReadyState != WebBrowserReadyState.Complete)
    {
     Application.DoEvents();
     if (DateTime.Now - startTime > waitTime)
     {
      wb.Dispose();
      logger.Debug("wb.Dispose() timeout");
      return;
     }
    }

    wb.Width = screenShotParam.Left + screenShotParam.Width + screenShotParam.Left; // wb.Document.Body.ScrollRectangle.Width (避掉左右側(cè)的邊線);
    wb.Height = screenShotParam.Top + screenShotParam.Height; // wb.Document.Body.ScrollRectangle.Height;
    wb.ScrollBarsEnabled = false;
    wb.Document.Body.Style = "overflow:hidden";//hide scroll bar
    var doc = (wb.Document.DomDocument) as mshtml.IHTMLDocument2;
    var style = doc.createStyleSheet("", 0);
    style.cssText = @"img { border-style: none; }";

    Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
    wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
    wb.Dispose();
    logger.Debug("wb.Dispose()");

    bitmap = CutImage(bitmap, new Rectangle(screenShotParam.Left, screenShotParam.Top, screenShotParam.Width, screenShotParam.Height));
    bool needResize = screenShotParam.Width > screenShotParam.ResizeMaxWidth || screenShotParam.Height > screenShotParam.ResizeMaxWidth;
    if (needResize)
    {
     double greaterLength = bitmap.Width > bitmap.Height ? bitmap.Width : bitmap.Height;
     double ratio = screenShotParam.ResizeMaxWidth / greaterLength;
     bitmap = Resize(bitmap, ratio);
    }

    bitmap.Save(savePath, System.Drawing.Imaging.ImageFormat.Gif);
    bitmap.Dispose();
    logger.Debug("bitmap.Dispose();");
    logger.Debug("finish");

   }
   catch (Exception ex)
   {
    logger.Info($"exception: {ex.Message}");
   }
  }

3、截圖操作

private static Bitmap CutImage(Bitmap source, Rectangle section)
  {
   // An empty bitmap which will hold the cropped image
   Bitmap bmp = new Bitmap(section.Width, section.Height);
   //using (Bitmap bmp = new Bitmap(section.Width, section.Height))
   {
    Graphics g = Graphics.FromImage(bmp);

    // Draw the given area (section) of the source image
    // at location 0,0 on the empty bitmap (bmp)
    g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);

    return bmp;
   }
  }

  private static Bitmap Resize(Bitmap originImage, Double times)
  {
   int width = Convert.ToInt32(originImage.Width * times);
   int height = Convert.ToInt32(originImage.Height * times);

   return ResizeProcess(originImage, originImage.Width, originImage.Height, width, height);
  }

完整代碼

public static string ScreenShotAndSaveAmazonS3(string account, string locale, Guid rule_ID, Guid template_ID)
  {
   
   //新的Template
   var url = string.Format("https://xxxx/public/previewtemplate?showTemplateName=0&locale={0}&inputTemplateId={1}&inputThemeId=&Account={2}",
    locale,
    template_ID,
    account
    );
   

   var tempPath = Tools.GetAppSetting("TempPath");

   //路徑準(zhǔn)備
   var userPath = AmazonS3.GetS3UploadDirectory(account, locale, AmazonS3.S3SubFolder.Template);
   var fileName = string.Format("{0}.gif", template_ID);
   var fullFilePath = Path.Combine(userPath.LocalDirectoryPath, fileName);
   logger.Debug("userPath: {0}, fileName: {1}, fullFilePath: {2}, url:{3}", userPath, fileName, fullFilePath, url);
 
   //開(kāi)始截圖,並暫存在本機(jī)
   var screen = new Screen();
   screen.ScreenShot(url, fullFilePath);

   //將截圖,儲(chǔ)存到 Amazon S3
   //var previewImageUrl = AmazonS3.UploadFile(fullFilePath, userPath.RemotePath + fileName);

   return string.Empty;
  }
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PrintScreen.Common
{
 public class Screen
 {
  protected static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

  public void ScreenShot(string url, string path
   , int width = 400, int height = 300
   , int left = 50, int top = 50
   , int resizeMaxWidth = 200, int wait = 1)
  {
   if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(path))
   {
    ScreenShotParam requestParam = new ScreenShotParam
    {
     Url = url,
     SavePath = path,
     Width = width,
     Height = height,
     Left = left,
     Top = top,
     ResizeMaxWidth = resizeMaxWidth,
     Wait = wait != 0
    };
    startPrintScreen(requestParam);
   }
  }

  void startPrintScreen(ScreenShotParam requestParam)
  {
   Thread thread = new Thread(new ParameterizedThreadStart(do_PrintScreen));
   thread.SetApartmentState(ApartmentState.STA);
   thread.Start(requestParam);
   if (requestParam.Wait)
   {
    thread.Join();
    FileInfo result = new FileInfo(requestParam.SavePath);
    long minSize = 1 * 1024;// 太小可能是空白圖,重抓
    int maxRepeat = 2;    
    while ((!result.Exists || result.Length <= minSize) && maxRepeat > 0)
    {
     thread = new Thread(new ParameterizedThreadStart(do_PrintScreen));
     thread.SetApartmentState(ApartmentState.STA);
     thread.Start(requestParam);
     thread.Join();
     maxRepeat--;
    }
   }
  }

  void do_PrintScreen(object param)
  {
   try
   {
    ScreenShotParam screenShotParam = (ScreenShotParam)param;
    string requestUrl = screenShotParam.Url;
    string savePath = screenShotParam.SavePath;
    WebBrowser wb = new WebBrowser();
    wb.ScrollBarsEnabled = false;
    wb.ScriptErrorsSuppressed = true;
    wb.Navigate(requestUrl);
    logger.Debug("wb.Navigate");
    DateTime startTime = DateTime.Now;
    TimeSpan waitTime = new TimeSpan(0, 0, 0, 10, 0);// 10 second
    while (wb.ReadyState != WebBrowserReadyState.Complete)
    {
     Application.DoEvents();
     if (DateTime.Now - startTime > waitTime)
     {
      wb.Dispose();
      logger.Debug("wb.Dispose() timeout");
      return;
     }
    }

    wb.Width = screenShotParam.Left + screenShotParam.Width + screenShotParam.Left; // wb.Document.Body.ScrollRectangle.Width (避掉左右側(cè)的邊線);
    wb.Height = screenShotParam.Top + screenShotParam.Height; // wb.Document.Body.ScrollRectangle.Height;
    wb.ScrollBarsEnabled = false;
    wb.Document.Body.Style = "overflow:hidden";//hide scroll bar
    var doc = (wb.Document.DomDocument) as mshtml.IHTMLDocument2;
    var style = doc.createStyleSheet("", 0);
    style.cssText = @"img { border-style: none; }";

    Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
    wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
    wb.Dispose();
    logger.Debug("wb.Dispose()");

    bitmap = CutImage(bitmap, new Rectangle(screenShotParam.Left, screenShotParam.Top, screenShotParam.Width, screenShotParam.Height));
    bool needResize = screenShotParam.Width > screenShotParam.ResizeMaxWidth || screenShotParam.Height > screenShotParam.ResizeMaxWidth;
    if (needResize)
    {
     double greaterLength = bitmap.Width > bitmap.Height ? bitmap.Width : bitmap.Height;
     double ratio = screenShotParam.ResizeMaxWidth / greaterLength;
     bitmap = Resize(bitmap, ratio);
    }

    bitmap.Save(savePath, System.Drawing.Imaging.ImageFormat.Gif);
    bitmap.Dispose();
    logger.Debug("bitmap.Dispose();");
    logger.Debug("finish");

   }
   catch (Exception ex)
   {
    logger.Info($"exception: {ex.Message}");
   }
  }

  private static Bitmap CutImage(Bitmap source, Rectangle section)
  {
   // An empty bitmap which will hold the cropped image
   Bitmap bmp = new Bitmap(section.Width, section.Height);
   //using (Bitmap bmp = new Bitmap(section.Width, section.Height))
   {
    Graphics g = Graphics.FromImage(bmp);

    // Draw the given area (section) of the source image
    // at location 0,0 on the empty bitmap (bmp)
    g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);

    return bmp;
   }
  }

  private static Bitmap Resize(Bitmap originImage, Double times)
  {
   int width = Convert.ToInt32(originImage.Width * times);
   int height = Convert.ToInt32(originImage.Height * times);

   return ResizeProcess(originImage, originImage.Width, originImage.Height, width, height);
  }

  private static Bitmap ResizeProcess(Bitmap originImage, int oriwidth, int oriheight, int width, int height)
  {
   Bitmap resizedbitmap = new Bitmap(width, height);
   //using (Bitmap resizedbitmap = new Bitmap(width, height))
   {
    Graphics g = Graphics.FromImage(resizedbitmap);
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    g.Clear(Color.Transparent);
    g.DrawImage(originImage, new Rectangle(0, 0, width, height), new Rectangle(0, 0, oriwidth, oriheight), GraphicsUnit.Pixel);
    return resizedbitmap;
   }
  }

 }

 class ScreenShotParam
 {
  public string Url { get; set; }
  public string SavePath { get; set; }
  public int Width { get; set; }
  public int Height { get; set; }
  public int Left { get; set; }
  public int Top { get; set; }
  /// <summary>
  /// 長(zhǎng)邊縮到指定長(zhǎng)度
  /// </summary>
  public int ResizeMaxWidth { get; set; }
  public bool Wait { get; set; }
 }

}

效果

以上就是c# 實(shí)現(xiàn)網(wǎng)頁(yè)加載后將頁(yè)面截取為長(zhǎng)圖片的詳細(xì)內(nèi)容,更多關(guān)于c# 頁(yè)面截取為圖片的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#使用WinRar命令進(jìn)行壓縮和解壓縮操作的實(shí)現(xiàn)方法

    C#使用WinRar命令進(jìn)行壓縮和解壓縮操作的實(shí)現(xiàn)方法

    這篇文章主要介紹了C#使用WinRar命令進(jìn)行壓縮和解壓縮操作的實(shí)現(xiàn)方法,涉及C#基于Process類(lèi)操作WinRar命令的相關(guān)實(shí)現(xiàn)技巧,代碼簡(jiǎn)潔實(shí)用,需要的朋友可以參考下
    2016-06-06
  • C#9.0 新特性簡(jiǎn)介

    C#9.0 新特性簡(jiǎn)介

    這篇文章主要介紹了C#9.0 新特性簡(jiǎn)介,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • C#實(shí)現(xiàn)農(nóng)歷日歷的方法

    C#實(shí)現(xiàn)農(nóng)歷日歷的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)農(nóng)歷日歷的方法,詳細(xì)分析了使用C#實(shí)現(xiàn)農(nóng)歷日歷的完整步驟,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-01-01
  • C#實(shí)現(xiàn)俄羅斯方塊

    C#實(shí)現(xiàn)俄羅斯方塊

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)俄羅斯方塊小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • C# 設(shè)計(jì)模式系列教程-簡(jiǎn)單工廠模式

    C# 設(shè)計(jì)模式系列教程-簡(jiǎn)單工廠模式

    簡(jiǎn)單工廠模式職責(zé)單一,實(shí)現(xiàn)簡(jiǎn)單,且實(shí)現(xiàn)了客戶(hù)端代碼與具體實(shí)現(xiàn)的解耦。
    2016-06-06
  • 深入理解C#中常見(jiàn)的委托

    深入理解C#中常見(jiàn)的委托

    本篇文章是對(duì)C#中常見(jiàn)的委托進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • C# 參考之訪問(wèn)關(guān)鍵字:base、this

    C# 參考之訪問(wèn)關(guān)鍵字:base、this

    由于靜態(tài)成員函數(shù)存在于類(lèi)一級(jí),并且不是對(duì)象的一部分,因此沒(méi)有 this 指針。在靜態(tài)方法中引用 this 是錯(cuò)誤的。 索引器允許類(lèi)或結(jié)構(gòu)的實(shí)例按照與數(shù)組相同的方式進(jìn)行索引。索引器類(lèi)似于屬性,不同之處在于它們的訪問(wèn)器采用參數(shù)。
    2008-03-03
  • C#實(shí)現(xiàn)無(wú)損壓縮圖片的示例詳解

    C#實(shí)現(xiàn)無(wú)損壓縮圖片的示例詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)無(wú)損壓縮圖片功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以了解一下
    2022-12-12
  • C#利用GDI繪制常見(jiàn)圖形和文字

    C#利用GDI繪制常見(jiàn)圖形和文字

    本文主要介紹了C#中利用GDI來(lái)繪制圖形和文字的方法,并提供的簡(jiǎn)單的示例供大家參考學(xué)習(xí),希望能夠?qū)Υ蠹矣兴鶐椭?/div> 2016-03-03
  • C#導(dǎo)出文本內(nèi)容到word文檔的方法

    C#導(dǎo)出文本內(nèi)容到word文檔的方法

    這篇文章主要介紹了C#導(dǎo)出文本內(nèi)容到word文檔的方法,涉及C#操作word文檔的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04

最新評(píng)論

永州市| 西和县| 子长县| 如东县| 洪江市| 海原县| 黄梅县| 合山市| 英吉沙县| 筠连县| 上虞市| 泗洪县| 稻城县| 宁明县| 汉川市| 那坡县| 田阳县| 荥阳市| 新平| 株洲市| 二手房| 昌乐县| 安阳市| 兴安县| 历史| 富顺县| 中宁县| 平邑县| 荥经县| 临安市| 茶陵县| 新津县| 武胜县| 广昌县| 金平| 云梦县| 定日县| 洛南县| 顺平县| 凉城县| 乌拉特前旗|