• C#根据URL在线生成网页缩略图


    最近在找C#根据URL生成缩略图的相关资料,转载如下
    哪位仁兄有这方面的源码或开发经验请赐教,本人不胜感激
    我的邮箱zxjyuan@163.com
    原理是通过IE在本地打开目标URL同时截图,
    这种方法都有安全性的问题,如果你为恶意网页生成缩略图,可能就是一场灾难……
    private void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
           WebBrowser browser = (sender as WebBrowser);
         
           if (browser != null)
           {
             mshtml.IHTMLDocument2 document = (browser.Document.DomDocument as mshtml.IHTMLDocument2);
             if (document != null)
             {
               mshtml.IHTMLElement element = (document.body as mshtml.IHTMLElement);
               if (element != null)
               {
                 IHTMLElementRender render = (element as IHTMLElementRender);
                 if (render != null)
                 {
                   using (Graphics graphics = this.CreateGraphics())
                   {
                     IntPtr hdcDestination = graphics.GetHdc();
                     render.DrawToDC(hdcDestination);
                     IntPtr hdcMemory = GDI32.CreateCompatibleDC(hdcDestination);
                     IntPtr bitmap = GDI32.CreateCompatibleBitmap(
                       hdcDestination,
                       browser.ClientRectangle.Width,
                       browser.ClientRectangle.Height
                       );
                   
                     if (bitmap != IntPtr.Zero)
                     {
                       IntPtr hOld = (IntPtr)GDI32.SelectObject(hdcMemory, bitmap);
                       GDI32.BitBlt(
                         hdcMemory,
                         0, 0,
                         browser.ClientRectangle.Width, browser.ClientRectangle.Height,
                         hdcDestination,
                         0, 0,
                         TernaryRasterOperations.SRCCOPY
                         );
                       GDI32.SelectObject(hdcMemory, hOld);
                       GDI32.DeleteDC(hdcMemory);
                       graphics.ReleaseHdc(hdcDestination);
                     
                       SaveThumbnail(Image.FromHbitmap(bitmap));
                     }
                   }
                 }
               }
             }
           }
    }
    从这里获取了内容的话,我们就很容易生成缩略图了,代码如下
    //by crazycoder.cn
    private void SaveThumbnail(Image image)
    {
           if (image != null)
           {//创建一个位图图像
             Bitmap thumbnail = new Bitmap(160, 120, PixelFormat.Format24bppRgb);
             thumbnail.SetResolution(image.HorizontalResolution, image.VerticalResolution);
           
             using (Graphics resize = Graphics.FromImage(thumbnail))
             {
               resize.InterpolationMode = InterpolationMode.HighQualityBicubic;
               resize.DrawImage(image,
                 new Rectangle(0, 0, 160, 120),
                 new Rectangle(0, 0, _webBrowser.ClientRectangle.Width, _webBrowser.ClientRectangle.Height),
                 GraphicsUnit.Pixel);
             }
             thumbnail.Save(_file.FullName, ImageFormat.Png);
           }
    }
    
    下载整个项目代码  CrazyCoder_WebPageToImage.zip
    附:做得比较好的在线生成缩略图的网站
    http://www.wenwenba.com/
    http://www.websnapr.com/
    http://mozshot.nemui.org/
    http://webthumb.bluga.net/home
    
  • 相关阅读:
    Android9.0/8.1/6.0 默认给系统 app 授予所有权限
    Git——处理Warning: CocoaPods is installed but broken. Skipping pod install. You appear to have CocoaPods installed but it is not working.
    Git——error: RPC failed; curl 56 LibreSSL SSL_read:报错处理
    Git——To install: sudo gem install cocoapods 报错处理
    Xcode——清除缓存相关文件
    vue —— 拉取远程仓库在本地开发
    vue —— mac中配置Node、NPM环境
    Flutter——常见报错处理
    Git——从githup上克隆下一个完整的项目操作步骤
    Git——rebase实际操作遇到的问题
  • 原文地址:https://www.cnblogs.com/zxjyuan/p/1275988.html
Copyright © 2020-2023  润新知