njcrawford / EmbroideryReader

Open source .PES file viewer
GNU General Public License v2.0
39 stars 20 forks source link

ArgumentException in designToBitmap() when printing #10

Closed njcrawford closed 8 years ago

njcrawford commented 8 years ago

From automated error report: Time: 2016-04-02 11:03:27 Program: embroideryReader, Version=2.1.0.0, Culture=neutral, PublicKeyToken=null Program location: C:\Program Files (x86)\NJCrawford Software\Embroidery Reader\embroideryReader.exe OS: Microsoft Windows NT 6.2.9200.0 (Note: this is Windows 8, possibly 8.1) OS Culture: en-US Framework: 4.0.30319.42000

Error: System.ArgumentException (Parameter is not valid.) at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format) at PesFile.PesFile.designToBitmap(Single threadThickness, Boolean filterUglyStitches, Double filterUglyStitchesThreshold, Single scale) at embroideryReader.frmMain.printDocument1_PrintPage(Object sender, PrintPageEventArgs e) at System.Drawing.Printing.PrintDocument.OnPrintPage(PrintPageEventArgs e) at System.Drawing.Printing.PrintController.PrintLoop(PrintDocument document) at System.Drawing.Printing.PrintController.Print(PrintDocument document) at System.Drawing.Printing.PrintDocument.Print() at embroideryReader.frmMain.printToolStripMenuItem_Click(Object sender, EventArgs e) at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e) at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e) at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea) at Sy (Report clipped)

njcrawford commented 8 years ago

Additional information: The crash reporter adds that they have a Canon MG5620 printer. The Canon website claims its resolution is 4800x1200 DPI, which is within the expected range. The reporter also sent a few of the designs that were causing this problem. So far, I'm unable to reproduce the issue on my development system.

njcrawford commented 8 years ago

I believe the root cause of this was the printer scaling code, mentioned in #12. I wasn't able to get a crash report from beta 2.2.1, but manually walking through the code and doing calculations by hand show that the old scaling code would try to generate a bitmap larger than 2GB[1] when printing a large design at 4800x1200 DPI. (it appears that the X resolution was used for both X and Y, so it was actually calculating for 4800x4800 DPI) The new, correct scaling code seems to stay well within the 2GB limit. I'm calling this closed for now, pending a test from the original reporter.

[1] Learned something new - .Net GC doesn't allow objects larger than 2GB, even when running on a 64 bit framework.

njcrawford commented 8 years ago

Another error report, this time from beta 2.2.1. The printer resolution is coming back as -3... I don't know what that means, but I'm guessing it's an error number.

Time: 2016-04-14 09:05:04 Program: embroideryReader, Version=2.2.1.0, Culture=neutral, PublicKeyToken=null Program location: C:\Program Files (x86)\Embroidery Reader\embroideryReader.exe OS: Windows 10 Home release 1511 (6.3 build 10586) (x64) OS Culture: en-US Framework: 4.0.30319.42000 (x64)

Error: System.ArgumentOutOfRangeException (Specified argument was out of the range of valid values. Parameter name: Printer X DPI claims to be '-3', expected range 20 - 10000) at embroideryReader.frmMain.printDocument1_PrintPage(Object sender, PrintPageEventArgs e) at System.Drawing.Printing.PrintDocument.OnPrintPage(PrintPageEventArgs e) at System.Drawing.Printing.PrintController.PrintLoop(PrintDocument document) at System.Drawing.Printing.PrintController.Print(PrintDocument document) at System.Drawing.Printing.PrintDocument.Print() at embroideryReader.frmMain.printToolStripMenuItem_Click(Object sender, EventArgs e) at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e) at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e) at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)% (Report clipped)

njcrawford commented 8 years ago

PrinterResolution.X is allowed to return negative numbers to indicate general print quality instead of a specific DPI. See https://msdn.microsoft.com/en-us/library/system.drawing.printing.printerresolution.x(v=vs.110).aspx for more.

The value -3 corresponds to DMRES_MEDIUM, according to http://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Advanced/Gdiplus.cs

This has been fixed for beta 2.2.2, because it no longer uses printer resolution values. This issue should be completely resolved by 1b33c47.