根据URL自动生成网页缩略图的源代码

Admin | 2009-6-9 19:07:17 | TrackRecord: 3068 Times | Tag标签:asp.net 打印本页

您当前所处的位置是:〖首页〗→【文章页】 本站共有16个图文教程栏目,请用心拜读!

本站提供经典的Excel公式函数实例,Word排版技巧,PPT教程;同时更兼有Flash,PowerPoint,数据库等技术文章。

  一、原理及程序类型

  原理:通过IE在本地打开目标URL同时截图;

  本方法存在安全性的问题,千万不要为恶意网页生成缩略图,可能会给您带来一场灾难!

  程序类型:C#语句(CS结构的程序)。

  二、源代码

  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));
                  }
                }
              }
            }
          }
        }
  }

  通过以上代码获取URL页面的内容之后,使用如下代码可生成缩略图

  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);
        }
  }

  经过测试并验证,效果不是很好,但是还能使用,足够使用拉!



上一篇:下篇—水晶报表导出为word/excel文件    下一篇:如何将JS文件编译到ASP.NET项目里的D

会员评论列表:
针对本篇文章或本站,请您发表个人的建议或批评!
FreeBoxPc

谷歌搜索 百度搜索 本站仅与内容具备一定的实用价值的原创网站交换友情链接,力争为大众做出更优质的服务!
All Rights Reserved版权所有 本站备案信息:滇ICP备11001339号-2 站长联系方式 Email:dzwebs@126.com