SaveToFile saving empty images

Discuss and ask questions about CAD .NET library.

Moderators: SDS, support, admin

Post Reply
EBrown
Posts: 3
Joined: 19 Oct 2012, 01:12

SaveToFile saving empty images

Post by EBrown » 05 Nov 2012, 20:37

It is not happening with *all* of my .dwg files, but it is with some of them - and I need to be able to print all of them.

I have a .zip file with two drawings in it, but after 3 tries, I still cannot upload the file for your review.

Here is my code snippet:

Code: Select all

        CADImage img = CADImage.CreateImageByExtension(strSrc);
        img.UseDoubleBuffering = false;

        img.LoadFromFile(strSrc);
        img.SetCurrentLayout(0);
        img.RefreshCurrentLayout();
        img.GetExtents();

        // Save a BMP and convert that to PDF - since direct save-to-pdf costs another 1.5k from CADImport.NET folks

        //save raster image
        if (img.Painter != null) {

          Bitmap bmp = new Bitmap(1, 1);
          Graphics g = Graphics.FromImage(bmp);
          img.SetNewMMToPixel(g);
          double scale = (img.AbsWidth / img.MMToPixelX) * 72;    // assume scale is in points and scale up...

          DRect r = new DRect(0, 0, scale, scale), ext = img.Extents;

          System.Drawing.Imaging.ImageFormat fmtOut = System.Drawing.Imaging.ImageFormat.Png;     //  .ImageFormat.Bmp;   // 
          img.ChangeDrawMode(DrawGraphicsMode.Export, null);      // DrawGraphicsMode.GDIPlus, null);
          // !!!!!!!!!!!!!!!!! WARNING! !!!!!!!!!!!!!!!!!
          // even though it's a PNG file format, something about the .png extension causes a crash 
          // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
          img.SaveToFile(strDest + ".img", fmtOut, r);
          string imgDest = strDest + ".png";
          if (File.Exists(imgDest)) File.Delete(imgDest);
          System.IO.File.Move(strDest + ".img", strDest + ".png");

          g = null;
          bmp = null;
        }
Is there anything in my code that sticks out as likely to cause a problem?

I am also finding that XREFs are not being exported when I export from a .dwg file that has them.

Could these two issues be related?

Thanks for your assistance!

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

Re: SaveToFile saving empty images

Post by support » 12 Nov 2012, 19:10

Hello.
The crash can be caused by this code fragment:

Code: Select all

          Graphics g = Graphics.FromImage(bmp);
          img.SetNewMMToPixel(g);
          double scale = (img.AbsWidth / img.MMToPixelX) * 72;    // assume scale is in points and scale up...
the function CADImage.SetNewMMToPixel calculates a value that is inversely proportional to DPI of a Graphics object. It is designed to be used with image control's Graphics. Calling the function with an alternative Graphics object as argument can result in crash because of too big AbsWidth parameter. You can check the extensions of your drawings. Also we're not sure why scale multiplied by 72.
Please check using CADImage.SaveToFile method in the Viewer demo project.

We have technical works on the forum recently that poosible caused the issue with uploading. Please try again to upload the files if the foregoing answer won't help.

Also have you considered using PDF printer for your task instead conversion ->raster->PDF?

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

Post Reply