DotNet Interop - Platform Bug?
james_cs
Member Posts: 39
I have a very simple piece of DotNet. I am trying to create a rectangle to show on a report. The code works the first two times it is run and then fails with the message
A call to System.Drawing.Graphics.FillRectangle failed with this message: Parameter is not valid.
Variables
GlobalTempPicture happens to be a temporary item record (table 27) but could be a TempBLOB
Code
it makes no difference if I clear all the variables. I can't see that I am doing anything wrong - is this a bug?
If anyone would like the object then please let me know.
thanks in advance!
James
A call to System.Drawing.Graphics.FillRectangle failed with this message: Parameter is not valid.
Variables
Name DataType Subtype Length LocalImage DotNet System.Drawing.Image.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' LocalImageFormat DotNet System.Drawing.Imaging.ImageFormat.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Bitmap DotNet System.Drawing.Bitmap.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Graphics DotNet System.Drawing.Graphics.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Brushes DotNet System.Drawing.Brushes.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' InStr InStream
GlobalTempPicture happens to be a temporary item record (table 27) but could be a TempBLOB
Code
GlobalTempPicture.DELETEALL; Bitmap := Bitmap.Bitmap(300,50); LocalImage := Bitmap; Graphics := Graphics.FromImage(LocalImage); //This line fails - I don;'t know why Graphics.FillRectangle(Brushes.Green,0,0,300,50); GlobalTempPicture.Picture.CREATEINSTREAM(InStr); LocalImage.Save(InStr,LocalImageFormat.Jpeg); GlobalTempPicture.INSERT;
it makes no difference if I clear all the variables. I can't see that I am doing anything wrong - is this a bug?
If anyone would like the object then please let me know.
thanks in advance!
James
0
Best Answer
-
I figured out why we get the error.
A comment in this article led me to the answer.
The reason it fails is because the brush (in this case a system-defined brush) gets disposed after the first run. This means that Brushes.Green now refers to null and null is not an acceptable parameter for FillRectangle.
The following code works:CLEAR(Item); Item.DELETEALL; Bitmap := Bitmap.Bitmap(300,50); Graphics := Graphics.FromImage(Bitmap); SolidBrush := SolidBrush.SolidBrush(Color.Green); Graphics.FillRectangle(SolidBrush,0,0,300,50); Item.Picture.CREATEINSTREAM(InputStream); Bitmap.Save(InputStream,ImageFormat.Jpeg); Item.INSERT;
Variables are the same as you use with these added:Color DotNet System.Drawing.Color.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' SolidBrush DotNet System.Drawing.SolidBrush.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
5
Answers
-
dou you run clear(<dotnet variable>) at the end of your code ?best regards
Franz Kalchmair, MVP
Alias: Jonathan Archer
please like / agree / verify my answer, if it was helpful for you. thx.
Blog: http://moxie4nav.wordpress.com/0 -
I think, you have to create a brush first, assign the color to it and then pass it to your function.
Hope, it helps
Oki0 -
I'm sorry to say that neither of these responses take the matter further. As stated - clearing the variables made no difference. Also defining the brush as a separate variable is completely equivalent to passing the Brushes enum through.
Equivalent code works well in C# but this C/SIDE code fails after the first two successful runs. I don't know why.0 -
I have been testing your code example and I get the exact same error.
My test do show though that it sometimes crashes on the first pass.
I will keep testing various options to see if I can get something working.0 -
I figured out why we get the error.
A comment in this article led me to the answer.
The reason it fails is because the brush (in this case a system-defined brush) gets disposed after the first run. This means that Brushes.Green now refers to null and null is not an acceptable parameter for FillRectangle.
The following code works:CLEAR(Item); Item.DELETEALL; Bitmap := Bitmap.Bitmap(300,50); Graphics := Graphics.FromImage(Bitmap); SolidBrush := SolidBrush.SolidBrush(Color.Green); Graphics.FillRectangle(SolidBrush,0,0,300,50); Item.Picture.CREATEINSTREAM(InputStream); Bitmap.Save(InputStream,ImageFormat.Jpeg); Item.INSERT;
Variables are the same as you use with these added:Color DotNet System.Drawing.Color.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' SolidBrush DotNet System.Drawing.SolidBrush.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
5 -
good catch
0 -
That's amazing - I have no idea why it works but it does work. I was under the impression that Brushes was a static and required neither to be created nor disposed. I can confirm that using the SolidBrush does not produce the same error though.
kaspermoerch - I would be interested to understand how you figured out that the Brushes was being disposed but that is probably a different discussion!
Many thanks for spending your time looking at this.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 323 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions


