使用 geckoFx 创建网站缩略图会导致错误

发布于 2024-11-29 12:37:56 字数 2043 浏览 1 评论 0原文

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

错误是使用来自 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,我有不同的方法,此方法工作正常,但它们不是适合我的项目。

I'm trying to create an image of a website using geckoFX in WPF/c#. When I run the code I get an error.

error is a Bitmap Width and height using from geckoWebBorwser (int)wWidth and (int) wHeight
error text is (Parameter is not valid) and System.ArgumentException

Error occurs on line

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

System.ArgumentException)

Example code:

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

With WebKit.Net this method works fine, With IE webBrowser I have different method and this method works fine, but they are not suitable for my project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夕嗳→ 2024-12-06 12:37:56

我创建了一个小样本(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 处收到错误。

I created a small sample ( the geckoFx was unknown but looked interesting ). The following code runs fine:

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

I got an error at browser.DrawToImage when I did not re sized the browser to be at least the size of the rectangle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文