locked
System.ArgumentException Message: Parameter is not valid. RRS feed

  • Question

  • Guys, I am getting the following exception from event log(Pls see code below). Any help would be highly appreciated. One issue seems

    Exception Type: System.ArgumentException
    Message: Parameter is not valid.
    ParamName: NULL
    Data: System.Collections.ListDictionaryInternal
    TargetSite: Void .ctor(Int32, Int32, System.Drawing.Imaging.PixelFormat)
    HelpLink: NULL
    Source: System.Drawing

    StackTrace Information
    *********************************************
       at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)
       at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)
       at WcfServices.HtmlToPngService.CreateBitmap(WebBrowser& browser) in 

    I am calling the following function of WCF service (hosted under IIS6/Win2003) in a loop. Each Request from client, wcf service creates 10 threads and there is a loop at client side which also runs a loop.

         

            private void runBrowserThread(string url,string imagePath)
            {
                try
                {
                    var th = new Thread(() =>
                    {
                        var browser = new WebBrowser();
                        browser.DocumentCompleted += browser_DocumentCompleted;
                        browser.ScrollBarsEnabled = false;
                        browser.ScriptErrorsSuppressed = true;
                        browser.Width = 720;
                        browser.Navigate(url);
                        browser.Name = imagePath;
                        Application.Run();
                    });
                    th.SetApartmentState(ApartmentState.STA);
                    th.Start();
                }
                catch (Exception ex)
                {
                    ExceptionManager.Publish(ex);
                }

            }

          private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
          {
                    var br = sender as WebBrowser;
                    CreateBitmap(ref br);
           }

            private void CreateBitmap(ref WebBrowser browser)
            {
                string OriginalPath = browser.Url.LocalPath.ToString();
                string ImagePath = browser.Name;

                HtmlServiceInfo obj;
                obj = lstSharedProcessedImages.Find(x => x.Url.Equals(OriginalPath, StringComparison.CurrentCultureIgnoreCase));

                try
                {
                    Bitmap localBitmap = null;
                    browser.Height = browser.Document.Body.ScrollRectangle.Height;
                    localBitmap = new Bitmap((browser.Width), (browser.Height));
                    browser.DrawToBitmap(localBitmap, new System.Drawing.Rectangle(0, 0, (browser.Width), (browser.Height)));

                    if (localBitmap != null)
                    {
                        localBitmap = (Bitmap)resizeImage((Image)localBitmap, new Size((localBitmap.Width * 2), (localBitmap.Height * 2)));
                        localBitmap.Save(ImagePath, System.Drawing.Imaging.ImageFormat.Png);
                        if (obj != null)
                            obj.Processed = true;
                    }
                }
                catch (Exception ex)
                {
                    obj.Processed = false;
                    ExceptionManager.Publish(ex);
                }
                finally
                {
                    browser.Dispose();
                }
            }

    Thursday, March 10, 2016 5:55 PM

Answers

  • Hello,

    My guess is browser.Height = browser.Document.Body.ScrollRectangle.Height; is returning a value that is not valid.  I suggest using the debugger or adding in a log to trace the value.

    Cheers,

    Jeff


    Jeff

    • Marked as answer by msdnlover Wednesday, March 23, 2016 9:46 PM
    Thursday, March 10, 2016 9:41 PM

All replies

  • Hello,

    My guess is browser.Height = browser.Document.Body.ScrollRectangle.Height; is returning a value that is not valid.  I suggest using the debugger or adding in a log to trace the value.

    Cheers,

    Jeff


    Jeff

    • Marked as answer by msdnlover Wednesday, March 23, 2016 9:46 PM
    Thursday, March 10, 2016 9:41 PM
  • The height is indeed an issue here. sometime the file is too big and the Height becomes a big number i.e. 72535. then the following line tries to create a bitmap with the width and height which is pretty large number. 

     localBitmap = new Bitmap((browser.Width), (browser.Height));

    Is there any size limit to the height ? In debugger I found that if the height is > 65535, it changes it back to the 65535 which seems like a limit of what it can handle. I wonder if there is a workaround for this situation ? Highly appreciate any input.


    • Edited by msdnlover Wednesday, March 23, 2016 9:56 PM
    Wednesday, March 23, 2016 9:51 PM