Select to view content in your preferred language

Export map to PNG parameters - VB Code

3318
2
09-06-2010 05:29 AM
KennethGibson
Emerging Contributor
I have some VB code which successfully exports an ArcMap document to PDF format with some PDF export settings such as:

pExportPDF.EmbedFonts = True
pExportPDF.Compressed = True

I would now like to alter the code to export the map to PNG format. As part of this process I would like to set the background colour and transparent colour settings to be white. I know I need to use something like:

pExportPNG.TransparentColor

but don't know how to specify the colour white.

I'd appreciate any help, thanks.
0 Kudos
2 Replies
KennethGibson
Emerging Contributor
If anyone is interested in an answer, then I found that this can be done with Python using the new arcpy.mapping module in ArcGIS 10. The code below was used to export a series of data driven pages which were set up in an MXD called TileExport.mxd. The exported tile names were derived from a NAME attribute field in the feature class used to define the data driven pages. You can see that the background and transparent colours can be set as parameters of ExportToPNG using a string representing the RGB colour values. I hope this is useful...


import arcpy

mxd = arcpy.mapping.MapDocument(r"C:\#workspace\TileExport.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
Field = "NAME"

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
  mxd.dataDrivenPages.currentPageID = pageNum
  pageName = mxd.dataDrivenPages.pageRow.getValue(Field)
  arcpy.mapping.ExportToPNG(mxd, r"C:\#workspace\Tile_" + str(pageName) + ".png", df,
                            df_export_width=5000,
                            df_export_height=5000,
                            resolution=254,
                            world_file=True,
                            color_mode="8-BIT_PALETTE",
                            background_color="255,255,255",
                            transparent_color="255,255,255")
del df
del mxd
0 Kudos
MichaelRobb
Honored Contributor
ArcPy yes, convenient new approach...

here is VB.net approach with good comments included!

                'Time to export the map
                pAnimProcessor.Message = "Exporting to PNG file..."

                'Create variables for map and extent

                'Set the current map and TELL IT TO LOOK AT THE LAYOUT VIEW!
                pMxDoc = m_application.Document
                pActiveView = pMxDoc.PageLayout

                'Create export object and set the resolution
                Dim pExport As IExport
                pExport = New ExportPNG

                'Get display bounds and graphics content
                Dim DisplayBounds As tagRECT
                If TypeOf pActiveView Is IPageLayout Then
                    DisplayBounds = pActiveView.ExportFrame
                Else
                    DisplayBounds.left = 0
                    DisplayBounds.top = 0
                    DisplayBounds.right = pActiveView.ScreenDisplay.DisplayTransformation.DeviceFrame.right
                    DisplayBounds.bottom = pActiveView.ScreenDisplay.DisplayTransformation.DeviceFrame.bottom
                End If

                'get the export rectangle
                Dim exportRect As tagRECT
                Dim outputresolution As Integer
                Dim screenresolution As Integer

                outputresolution = 300 'desired resolution
                screenresolution = 96 ' default screen resolution
                pExport.Resolution = outputresolution

                'The ExportFrame property gives us the dimensions appropriate for an export at screen resolution.
                ' Because we are exporting at a higher resolution (more pixels), we must multiply each dimesion
                ' by the ratio of OutputResolution to ScreenResolution. Instead of assigning the entire
                ' ExportFrame directly to the exportRECT, let's bring the values across one at a time and multiply
                ' the dimensions.

                With exportRect
                    .bottom = Convert.ToInt32(DisplayBounds.bottom * (outputResolution / screenResolution))
                    .left = Convert.ToInt32(DisplayBounds.left * (outputResolution / screenResolution))
                    .top = Convert.ToInt32(DisplayBounds.top * (outputResolution / screenResolution))
                    .right = Convert.ToInt32(DisplayBounds.right * (outputResolution / screenResolution))
                End With

                'Set the size and extent of the export
                Dim pPixelBoundsEnv As IEnvelope
                pPixelBoundsEnv = New Envelope
                pPixelBoundsEnv.PutCoords(exportRect.left, exportRect.top, exportRect.right, exportRect.bottom)
                pExport.PixelBounds = pPixelBoundsEnv

                'Set file name and export map
                Dim hDC As Long

                pExport.ExportFileName = OutputPath & "\" & [INSERT NAME HERE] & ".png"
                hDC = pExport.StartExporting

                pActiveView.Output(hDC, pExport.Resolution, exportRect, Nothing, Nothing)
                pExport.FinishExporting()
                pExport.Cleanup()
0 Kudos