ArcGIS Engine/Objects 10.8 System.Out.Of.Memory errors when trying to access exported JPEG

862
2
12-16-2020 11:14 PM
YolandiB
New Contributor

Hi, we are currently experiencing issues when trying to export JPEG images from a Mxd.  For some reason we keep getting System.Out.Of.Memory errors.  This sometimes happens during the first export, sometimes after 10, and other times after 50.  The error also does not occur on all client machines.

  We have spent some time looking at the system resources during execution and truth be told it seems that the error message might be misleading.  And that for some reason .NET cannot always access the file generated.

I have not been able to find any article on the Esri Forums to help resolve the issue.  To help explain what we are trying to accomplish I have included some code snippets.  We are using ArcGIS Engine 10.8 with .NET 4.6.2 as developing environment.

Basically the user presses a button to create a report.  We then use the ActiveView and IExportJPEG with a predefined IEnvelope to add an image to our document.  The error occurs as soon as we try to access the file saved from the ActiveView.Output method with FileStream and sometimes when trying to access the Image from Bitmap.FromStream.

using (FileStream fst = new FileStream(sTmpFilePath, FileMode.Open, FileAccess.Read))
{
                        using (Image img = Bitmap.FromStream(fst))
                        {
                                                //Image is added to document
                        }
}

The image is exported as follows (mapPrintObject contains references to the ActiveView and user settings for the export):

private void GetImageFromMap(cMapPrintObject mapPrintObject,ref IExportJPEG exiJPG, ref IEnvelope envPrintRegion, string sTmpFile)

{
                        int iScreenRes = Convert.ToInt32(mapPrintObject._ActiveView.ScreenDisplay.DisplayTransformation.Resolution);
                       ESRI.ArcGIS.esriSystem.tagRECT tRect = new ESRI.ArcGIS.esriSystem.tagRECT();
                       IEnvelope pBounds = new EnvelopeClass();
                       exiJPG = new ExportJPEGClass();
                        tRect.left = 0;
                        tRect.top = 0;
                        tRect.right = Convert.ToInt32(mapPrintObject._dImageWidth * (mapPrintObject._iExportRes / iScreenRes));
                        tRect.bottom = Convert.ToInt32(mapPrintObject._dImageHeight) * (mapPrintObject._iExportRes / iScreenRes);
                        pBounds.PutCoords(tRect.left, tRect.top, tRect.right, tRect.bottom);
                        exiJPG.Quality = 70;
                         ((IExport)exiJPG).PixelBounds = pBounds;
                         ((IExport)exiJPG).ExportFileName = sTmpFile;
                         ((IExport)exiJPG).Resolution = mapPrintObject._iExportRes;
                        int hdc = ((IExport)exiJPG).StartExporting();
                        mapPrintObject._ActiveView.Output(hdc, Convert.ToInt32(((IExport)exiJPG).Resolution), ref tRect, envPrintRegion, null);
                         ((IExport)exiJPG).FinishExporting();
                        ((IExport)exiJPG).Cleanup();
}

The image is created every time and I have attached an example to this post. 

To be honest I'm not sure what else I could try. Any advice? Are we using the correct functionality?  Is there a way to work with the Imagestream directly to avoid possible issues with access to the images? 

0 Kudos
2 Replies
KirkKuykendall1
Occasional Contributor III

This sample takes a different approach (not sure if it's any better though).

https://github.com/Esri/arcobjects-sdk-community-samples/blob/master/Net/GraphicsPipeline/ExportActi...

 

0 Kudos
YolandiB
New Contributor

Hi,

thanks for the sample. This seems to fix the issue with System.Out.Of.Memory, but I am unable to set the height and width of the image.  The extent of the image exported is correct, but the image is always the same size. Any ideas?

0 Kudos