【问题标题】:Creating a website thumbnail with geckoFx leads to error使用 geckoFx 创建网站缩略图会导致错误
【发布时间】:2011-10-26 07:37:14
【问题描述】:

我正在尝试在 WPF/c# 中使用 geckoFX 创建网站的图像。运行代码时出现错误。

error 是使用 geckoWebBorwser (int)wWidth 和 (int) wHeight 的位图宽度和高度 错误文本是(参数无效)和 System.ArgumentException

在线出错

Bitmap bmp = new Bitmap(wWidth, wHeight); //Error here  (Parameter is not valid.

System.ArgumentException)

示例代码:

class GetWebCapture
{
    public static BitmapSource GetThumb(Skybound.Gecko.GeckoWebBrowser wb, int w, int h, int wWidth, int wHeight)
    {
        if (wb != null)
        {

            Bitmap bmp = new Bitmap(wWidth, wHeight);


            Rectangle rec = new Rectangle(0, 0, wWidth, wHeight);
            Rectangle rec1 = new Rectangle(0, 0, w, h);

            wb.DrawToBitmap(bmp, rec);

            Graphics gfx = Graphics.FromImage(bmp);
            gfx.CompositingQuality = CompositingQuality.HighQuality;
            gfx.SmoothingMode = SmoothingMode.HighQuality;
            gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;


            gfx.DrawImage(bmp, rec);

            Bitmap newImage = new Bitmap(290, 200);

            using (Graphics gr = Graphics.FromImage(newImage))
            {
                gr.SmoothingMode = SmoothingMode.AntiAlias;
                gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                gr.PixelOffsetMode = PixelOffsetMode.HighQuality;

                gr.DrawImage(bmp, new Rectangle(0, 0, 290, 200));
            }

            BitmapSource source = loadBitmaps(newImage);


            gfx.Dispose();
            bmp.Dispose();

            return source;

        }
        else
        {

            return null;
        }

  public static BitmapSource loadBitmaps(System.Drawing.Bitmap source)
    {
        return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(source.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
            System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
    }
}

使用 WebKit.Net 这个方法很好用,用 IE webBrowser 我有不同的方法,这个方法很好用,但是它们不适合我的项目。

【问题讨论】:

  • 你得到什么错误?请通过编辑您的帖子提供更多信息。为了更好地阅读,我已经对其进行了一些修改
  • 能否提供参数值?

标签: c# browser thumbnails geckofx


【解决方案1】:

我创建了一个小样本(geckoFx 未知,但看起来很有趣)。以下代码运行良好:

namespace WebsiteScreeshot {
  class Program {
    static void Main ( string[] args ) {
      if ( args.Length != 2 ) {
        Console.WriteLine( "You need to provide URL and image" );
        return;
      }

      Xpcom.Initialize( @"C:\Libs\xulrunner" );
      var browser = new GeckoWebBrowser();
      browser.CreateControl();
      while ( !browser.IsHandleCreated ) {
        System.Threading.Thread.Sleep( 200 );
        System.Windows.Forms.Application.DoEvents();
      }
      browser.Navigate( args[0] );
      while ( browser.IsBusy ) {
        System.Threading.Thread.Sleep( 200 );
        System.Windows.Forms.Application.DoEvents();
      }
      browser.Width = 1044;
      browser.Height = 768;
      using ( Bitmap bmp = new Bitmap( 1024, 768 ) ) {
        Rectangle rec = new Rectangle( 0, 0, 1024, 768 );
        browser.DrawToBitmap( bmp, rec );
        bmp.Save( args[1] );
      }
      browser = null;
    }
  }
}

当我没有将浏览器的大小调整为至少是矩形的大小时,我在 browser.DrawToImage 处遇到错误。

【讨论】:

  • 我用两个 if 语句更新代码 if(wb.Width > 5) if(wb.Height > 5)
猜你喜欢
  • 2015-03-24
  • 2014-09-26
  • 2015-12-08
  • 2017-08-27
  • 2012-10-18
  • 2012-07-20
  • 1970-01-01
  • 2011-08-23
  • 2011-06-02
相关资源
最近更新 更多