Can't export DWG to big enough image

Discuss and ask questions about CAD .NET library.

Moderators: SDS, support, admin

Post Reply
aleksey
Posts: 5
Joined: 26 Jan 2018, 13:03

Can't export DWG to big enough image

Post by aleksey » 26 Jan 2018, 15:34

Hi!
I'm trying to export dwg to tiff file. The drawing is on A0 format sheet, so to see clear text I need the dpi be quite high (more then 300).
I use this method:
CADImport.CADImage.SaveToFile(string FileName, ImageFormat ImgFormat, DRect curRect, PixelFormat pixelFormat)

Code: Select all

public void SaveImage(string sourceFile, string filename, int resolution)
{
    var cadImage = CADImage.CreateImageByExtension(sourceFile);
    cadImage.IsWithoutMargins = true;
    cadImage.LoadFromFile(sourceFile);
    var pExtentse = cadImage.PureExtents;
    DRect tmpRect = new DRect(0, 0, (pExtentse.Width / 25.4) * resolution, (pExtentse.Height / 25.4) * resolution);
    cadImage.Painter.Settings.BackgroundColor = Color.White.ToArgb();
    cadImage.Painter.Settings.IsShowBackground = true;
    cadImage.SaveToFile(filename, ImageFormat.Tiff, tmpRect);
}
But resulted images are not bigger in the bigger side then 5000 pixels, regardless of given size in curRect argument.
It turns out that there is a static field CADConst.MaxRasterImageSize that represents that limit.
By setting this field to 10000, I get exception when trying to save images with somewhat around 300 000 000 pixel.
Exception thrown: 'System.Runtime.InteropServices.ExternalException' in System.Drawing.dll
System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+.
в System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
в System.Drawing.Image.Save(String filename, ImageFormat format)
в CADImport.CADGraphicsGDI.SaveToFile(String FileName, ImageFormat ImgFormat, DRect curRect, PixelFormat pixelFormat, RectangleF rect, Graphics e)
в CADImport.CADGraphicsGDI.SaveToFile(String FileName, ImageFormat ImgFormat, DRect curRect, RectangleF rect, Graphics e)
в CADImport.CADImage.SaveToFile(String FileName, ImageFormat ImgFormat, DRect curRect)
Can you suggest the solution for my problem? What I need is to able to save images in maximum possible size(I know about limitation of GDI).

support
Posts: 3271
Joined: 30 Mar 2005, 11:36
Contact:

Re: Can't export DWG to big enough image

Post by support » 29 Jan 2018, 21:38

Hello Aleksey,

We do not recommend to use the drawing extents (CADImage.Extents or CADImage.PureExtents) in calculation of the output image size, because the extents may be extremely large or small that results in inadequate image size. You should hardcode the output image width according to your requirements, then calculate MaxRasterImageSize and tmpRect values based on the hardcoded width. For example:

Code: Select all

var rasterImageWidth = 10000;
CADConst.MaxRasterImageSize = rasterImageWidth;
DRect tmpRect = new DRect(0, 0, rasterImageWidth, (rasterImageWidth * cadImage.AbsHeight / cadImage.AbsWidth)); 
But in this case the output image size (in pixels) will still be limited by Microsoft CLR (2GB per single object):

2 GB = 2,147,483,648 bytes / 4 bytes (per pixel) = 536,870,912 pixels


Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

aleksey
Posts: 5
Joined: 26 Jan 2018, 13:03

Re: Can't export DWG to big enough image

Post by aleksey » 30 Jan 2018, 11:00

Thank you for answer, Mikhail!
But as I wrote before, the SaveToFile method throws exception even when there are less then 400 000 000 pixels. What could be the cause of that? Is it possible to save image as big as the limit you are mentioned, around - 530 000 000 pixels?

support
Posts: 3271
Joined: 30 Mar 2005, 11:36
Contact:

Re: Can't export DWG to big enough image

Post by support » 30 Jan 2018, 17:13

Aleksey,

Could you please tell what values of pExtentse.Width, pExtentse.Height and resolution cause the GDI+ error in your code? We will test this problem.

To generate a TIFF file with the size around 536,870,912 pixels, you should use the code I posted.


Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

aleksey
Posts: 5
Joined: 26 Jan 2018, 13:03

Re: Can't export DWG to big enough image

Post by aleksey » 31 Jan 2018, 12:13

Mikhail,
I rewrote the code, using your example.

Code: Select all

public static void SaveImage(string sourceFile, string filename)
{
    var cadImage = CADImage.CreateImageByExtension(sourceFile);
    cadImage.IsWithoutMargins = true;
    cadImage.LoadFromFile(sourceFile);

    int maxSideSizeInPixels = 9000;
    CADConst.MaxRasterImageSize = maxSideSizeInPixels;
    DRect tmpRect;

    if (cadImage.AbsWidth > cadImage.AbsHeight)
        tmpRect = new DRect(0, 0, maxSideSizeInPixels, (maxSideSizeInPixels * cadImage.AbsHeight / cadImage.AbsWidth));
    else
        tmpRect = new DRect(0, 0, (maxSideSizeInPixels * cadImage.AbsWidth / cadImage.AbsHeight), maxSideSizeInPixels);


    cadImage.Painter.Settings.BackgroundColor = Color.White.ToArgb();
    cadImage.Painter.Settings.IsShowBackground = true;
    cadImage.SaveToFile(filename, ImageFormat.Tiff, tmpRect);
}
Exception was thrown, when DRect Width was 9000 and Height was 6357.
I also ran this code on other mashine. The result was different. Exception was thrown, when DRect Width was 22000 and Height was 15539.
Also, this result was unstable, the exception was thrown in some attempts, and in some it wasn't(with same dRect size).

The first mashine specifications (DRect Width was 9000 and Height was 6357):
Windows 10 Pro V6.3 Build 15063 BuildLabEx 15063.0.amd64fre.rs2_release.170317-1834
Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Physical Memory 32.00GB
Video Adapter nVidia GM107GL [Quadro K620] (Quadro K620)
Video Chip K620 Bios Version82.7.4e.0.13
The second mashine specifications (DRect Width was 22000 and Height was 15539):
Windows 10 Pro V6.3 Build 16299 BuildLabEx 16299.15.amd64fre.rs3_release.170928-1534
Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Physical Memory 16.00GB
Video Adapter nVidia GK107 [Quadro K600] (Quadro K600)
Video Chip K600 Bios Version80.7.c5.0.1
What could be the reason of that behaviour?

CADImport 12.1.20.21127

The drawing, I tested the code with, is in the attachment.
Attachments
testDrawing.zip
(19.04 KiB) Downloaded 1101 times

support
Posts: 3271
Joined: 30 Mar 2005, 11:36
Contact:

Re: Can't export DWG to big enough image

Post by support » 08 Feb 2018, 20:05

Hello Aleksey,

Sorry for the delay in my reply.

Judging by build numbers of your Windows 10 systems, you tested the problem with Windows 10 Insider Preview builds (10.0.15063.0 and 10.0.16299.15) of Redstone 2 and 3 updates. I tested the sample code on earlier version of Windows 10: Threshold 2 — November Update (1511) Build 10.0.10586.1176 and couldn't reproduce this behaviour.

Could you please test the code on Windows 10 1511 (November Update) or 1607 (Anniversary Update) to conclude whether Windows 10 updates affect this problem?


Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

aleksey
Posts: 5
Joined: 26 Jan 2018, 13:03

Re: Can't export DWG to big enough image

Post by aleksey » 09 Feb 2018, 14:45

Hello Mikhail, thank you for your answers!
As it turns out, the exception was thrown only when the code was executed from the unit test. When I run the same code from compiled executable, there is no problems what so ever. I also tested this code on different mashines and again without crashes.

support
Posts: 3271
Joined: 30 Mar 2005, 11:36
Contact:

Re: Can't export DWG to big enough image

Post by support » 19 Feb 2018, 22:03

Hello Aleksey,

To avoid this problem during unit testing, run Visual Studio with administrative permissions.


Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

aleksey
Posts: 5
Joined: 26 Jan 2018, 13:03

Re: Can't export DWG to big enough image

Post by aleksey » 01 Mar 2018, 16:24

Hello!
Unfortunately, your suggestion didn't work for me. The same exception was thrown when i ran unit tests with or without administrative permissions.

support
Posts: 3271
Joined: 30 Mar 2005, 11:36
Contact:

Re: Can't export DWG to big enough image

Post by support » 06 Mar 2018, 17:00

Hello Aleksey,

Could you please specify your Visual Studio version and path used for saving TIFF files?


Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

Post Reply