Page 1 of 1

SaveToFile saving empty images

Posted: 05 Nov 2012, 20:37
by EBrown
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!

Re: SaveToFile saving empty images

Posted: 12 Nov 2012, 19:10
by support
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.