Select to view content in your preferred language

How to save an IMap (or IGraphicsContainer) as an Image (such as: jpg, png...) ?

2733
10
Jump to solution
09-30-2012 06:07 AM
TaiBui
by
Frequent Contributor
Hi,

I am having an IMap (AxPageLayoutControl.ActiveView.FocusMap) that includes some elements (Text, lines,...)

And, I want to create an Image (such as: jpg, png) that show all objects of the IMap (using C#, Visual Studio).

Do you know how to do that ?

Thanks and regards,

Tai
0 Kudos
1 Solution

Accepted Solutions
TaiBui
by
Frequent Contributor

how do you figure the screen resolution?


Hi Xtian79,

I found out another way, please see the link: http://forums.arcgis.com/threads/69207-How-to-store-elements-of-IMap%28IGraphicsContainer%29-as-Bina...

Kind regards,

View solution in original post

0 Kudos
10 Replies
Cristian_Galindo
Frequent Contributor
Hi, basically one way is to draw layer by layer in an image object,  then export it

                private static IDisplay myDisplay;
                private static Image myMapImage;

                Graphics g = Graphics.FromImage(myMapImage);
                g.Clear(Color.Transparent);
                myDisplay.StartDrawing(g.GetHdc().ToInt32(), 0);

                for (int i = Map.LayerCount - 1; i >= 0; i--)
                {
                    ILayer layer = MapControl.get_Layer(i);
                    layer.Draw(esriDrawPhase.esriDPGeography, myDisplay, null);
                }

                myDisplay.FinishDrawing();
                g.ReleaseHdc();


I hope this is helpful for you
0 Kudos
TaiBui
by
Frequent Contributor
Hi, basically one way is to draw layer by layer in an image object,  then export it

                private static IDisplay myDisplay;
                private static Image myMapImage;

                Graphics g = Graphics.FromImage(myMapImage);
                g.Clear(Color.Transparent);
                myDisplay.StartDrawing(g.GetHdc().ToInt32(), 0);

                for (int i = Map.LayerCount - 1; i >= 0; i--)
                {
                    ILayer layer = MapControl.get_Layer(i);
                    layer.Draw(esriDrawPhase.esriDPGeography, myDisplay, null);
                }

                myDisplay.FinishDrawing();
                g.ReleaseHdc();


I hope this is helpful for you


Thank you. But there is an error: Use of unassigned local variable 'myMapImage'
Kind regards,
Tai
0 Kudos
TaiBui
by
Frequent Contributor
I am thinking about converting elements of IMap into RasterDataset (in geodatabase). Then, export RasterDataset into Image. But I haven't known how to do that. Are there anyone can help ?

Please see my below screenshot:

[ATTACH=CONFIG]18127[/ATTACH]
0 Kudos
Cristian_Galindo
Frequent Contributor
Thank you. But there is an error: Use of unassigned local variable 'myMapImage'
Kind regards,
Tai


you are rigth, that's my mistake, you have to initialize the variable....
0 Kudos
TaiBui
by
Frequent Contributor
you are rigth, that's my mistake, you have to initialize the variable....


Could you please tell me how to do that ?
0 Kudos
TaiBui
by
Frequent Contributor
Hi,

After searching on the internet. I found the solution for this and used the below codes to export IMap to image:

private void SaveImapAsJpg(IMap map, string filePath)
        {
            IExport pExport = new ExportJPEGClass();
            IActiveView activeView = map as IActiveView;
            pExport.ExportFileName = filePath;
          
            pExport.Resolution = 100;
            tagRECT exportRect = activeView .ExportFrame  ;
            IEnvelope pPixelBoundsEnv = new EnvelopeClass();

            pPixelBoundsEnv.PutCoords(exportRECT.left, exportRECT.bottom, exportRECT.right, exportRECT.top);
          
            pExport.PixelBounds = pPixelBoundsEnv;

            int hDC;
            hDC = pExport.StartExporting();
            activeView.Output(hDC, (int)pExport.Resolution, ref exportRECT, activeView.FullExtent, null);
            pExport.FinishExporting();
            pExport.Cleanup();
        }

Please see the result in image1

But the Image is not same as IMap. When I replace the italic codes by below codes, the image is good (I am not sure why ?):

            System.Int32 screenResolution = 30;
            System.Int32 outputResolution = 200;

            pExport.Resolution = outputResolution;

            ESRI.ArcGIS.Display.tagRECT exportRECT;
            exportRECT.left = 0;
            exportRECT.top = 0;
            exportRECT.right = activeView.ExportFrame.right * (outputResolution / screenResolution);
            exportRECT.bottom = activeView.ExportFrame.bottom * (outputResolution / screenResolution);

Please see the result in image2

[ATTACH=CONFIG]18181[/ATTACH]
[ATTACH=CONFIG]18182[/ATTACH]
0 Kudos
Cristian_Galindo
Frequent Contributor
hi,
System.Int32 screenResolution = 30;
            System.Int32 outputResolution = 200;

            pExport.Resolution = outputResolution;

            ESRI.ArcGIS.Display.tagRECT exportRECT;
            exportRECT.left = 0;
            exportRECT.top = 0;
            exportRECT.right = activeView.ExportFrame.right * (outputResolution / screenResolution);
            exportRECT.bottom = activeView.ExportFrame.bottom * (outputResolution / screenResolution);



how do you figure the screen resolution?
0 Kudos
TaiBui
by
Frequent Contributor

how do you figure the screen resolution?


Hi Xtian79,

I found out another way, please see the link: http://forums.arcgis.com/threads/69207-How-to-store-elements-of-IMap%28IGraphicsContainer%29-as-Bina...

Kind regards,
0 Kudos
Cristian_Galindo
Frequent Contributor
Hi Xtian79,

I found out another way, please see the link: http://forums.arcgis.com/threads/69207-How-to-store-elements-of-IMap%28IGraphicsContainer%29-as-Bina...

Kind regards,


Hi!!!,

in the solution that you propose how do you figure the value of the screen resolution?, or is it just a random value? i trying to export an image to the printer, that information would be useful
0 Kudos