This document is archived and information here might be outdated.  Recommended version.


How to set layer transparency (ArcObjects .NET 10.5 SDK)

How to set layer transparency


In this topic


Display transparency

Both symbols and images can use transparent colors. The transparency (alpha blend) algorithm is raster based. Vector drawings must first be converted to raster to support it. Transparent objects are drawn to a source bitmap. The background that the objects are drawn on must be stored in a background bitmap. Transparency is accomplished by blending the source bitmap into the background bitmap using a single transparency value for all the bits or a mask bitmap containing transparency values for each individual pixel. To support transparency, IDisplay provides the BackgroundDC attribute that can be used to get a bitmap containing all of the drawing that has happened in the current drawing sequence.
The transparency algorithm is encapsulated into the display filter object, TransparencyDisplayFilter. The same filter class can be used by feature layers, raster layers, elements, third parties, and so on. 
To draw with transparency, see the following code example:
[VB.NET]
Dim displayFilter As ITransparencyDisplayFilter=New TransparencyDisplayFilterClass()
displayFilter.Transparency=100
display.StartDrawing(hdc, cacheID)
display.Filter=displayFilter
DrawToDisplay()
display.Filter=0
display.FinishDrawing()
[C#]
ITransparencyDisplayFilter displayFilter=new TransparencyDisplayFilterClass();
displayFilter.Transparency=100;
display.StartDrawing(hdc, cacheID);
display.Filter=displayFilter;
DrawToDisplay();
display.Filter=0;
display.FinishDrawing();
For raster layers, DrawToDisplay() means getting the destination DC from the display and BitBlt (Bit-block Transfer: an intermediate file that is created by taking a "snapshot" of the screen pixels from a given rectangle) the raster image to it.
When the filter is specified, the display creates an internal filter cache that is used with the recording cache to provide the necessary raster info to the filter. Output is routed to the filter cache so that when the raster layer asks for the destination DC, it gets the filter cache DC. The display applies the filter when Display.Filter is set to zero. Apply receives the current background bitmap (recording cache), opaque raster layer image (filter cache), and  destination (window). The filter knows the transparency value is 100 so it does the blending and sends the results to the window.

Symbol transparency

How do you technically make symbols and images support transparency? The view objects (data and layout) handle drawing maps both to output devices (windows and printers) and to export files (bitmap and metafiles). All graphic elements and layers need to report whether or not they are using transparent colors. So that when the view starts to generate output, it can check if there are any transparent colors. If there are, it uses the following algorithm to produce output:
  • Divides the display surface into bands so that the in-memory bitmaps required to create transparent colors do not consume too much memory.
  • Creates a BackingDisplay the size of one band. A BackingDisplay has an internal device independent bitmap that has the same color depth and resolution as the output device. Drawing is directed to the bitmap and when drawing is finished, the bitmap is copied to the actual output device (or file). The layers and symbols treat this BackingDisplay like any other display so no special coding is required.
  • For each band, adjusts the display's transform to reference the current band and draw the view. The renderers automatically clip to the band since the transform's visible bounds equals the band rectangle.  
This results in a series of bitmaps (the bands) being copied to the output device or export file. If there are no transparent colors in the map, the metafile generates normally, containing series of vector graphics.






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine