Can't export DWG to big enough image
Moderators: SDS, support, admin
Can't export DWG to big enough image
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)
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.
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);
}
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.
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).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)
Re: Can't export DWG to big enough image
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:
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
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));
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
Chat support on Skype: cadsofttools.support
Re: Can't export DWG to big enough image
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?
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?
Re: Can't export DWG to big enough image
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
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
Chat support on Skype: cadsofttools.support
Re: Can't export DWG to big enough image
Mikhail,
I rewrote the code, using your example.
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):
CADImport 12.1.20.21127
The drawing, I tested the code with, is in the attachment.
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);
}
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):
The second mashine specifications (DRect Width was 22000 and Height was 15539):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
What could be the reason of that behaviour?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
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
Re: Can't export DWG to big enough image
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
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
Chat support on Skype: cadsofttools.support
Re: Can't export DWG to big enough image
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.
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.
Re: Can't export DWG to big enough image
Hello Aleksey,
To avoid this problem during unit testing, run Visual Studio with administrative permissions.
Mikhail
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
Chat support on Skype: cadsofttools.support
Re: Can't export DWG to big enough image
Hello!
Unfortunately, your suggestion didn't work for me. The same exception was thrown when i ran unit tests with or without administrative permissions.
Unfortunately, your suggestion didn't work for me. The same exception was thrown when i ran unit tests with or without administrative permissions.
Re: Can't export DWG to big enough image
Hello Aleksey,
Could you please specify your Visual Studio version and path used for saving TIFF files?
Mikhail
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
Chat support on Skype: cadsofttools.support