Adding display caching
Some drawing sequences can take awhile to complete. A simple way to optimize your application is to enable display caching. This refers to the ScreenDisplay's ability to record your drawing sequence into a bitmap and use the bitmap to refresh the picture box's window whenever the Paint method is called.
How to cache layers
Set the cached flag on the layers you want to have their own display cache. Then reactivate the view.
[VB.NET] Set the cached flag on the layers you want to have their own display cache. Then reactivate the view.
Private Sub EnableLayerCaches(application As IApplication)
Dim m_map As IMapDocument=application.Document
Dim activeView As IActiveView=m_map.ActiveView
Dim i As Integer
For i=0 To m_map.LayerCount - 1 Step i + 1
'chkCustomCaches is the check box on a form
m_map.get_Layer(i).Cached=(chkCustomCaches.Value=IIf(1 , True , False))
Next
...
activeView.Deactivate()
activeView.Activate(activeView.ScreenDisplay.hWnd)
activeView.Refresh()
End Sub
[C#] private void EnableLayerCaches(IApplication application)
{
IMapDocument m_map=application.Document;
IActiveView activeView=m_map.ActiveView;
int i;
for (i=0; i <= m_map.LayerCount - 1; i++)
{
//chkCustomCaches is the check box on a form
m_map.get_Layer(i).Cached=(chkCustomCaches.Value == 1 ? true : false);
}
... activeView.Deactivate();
activeView.Activate(activeView.ScreenDisplay.hWnd);
activeView.Refresh();
}
The cache is used until your data changes and you call IScreenDisplay.Invalidate to indicate that the cache is invalid. There are two kinds of caches:
- Recording
- User allocated
The following code uses a recording to implement a display cache in the application's Paint method:
[VB.NET] Private Sub Picture1_Paint()
If (m_screenDisplay.IsCacheDirty(esriScreenRecording)) Then
m_screenDisplay.StartRecording()
'Make a call to your Draw function. For example:
MyDraw(m_screenDisplay, Picture1.hDC)
m_screenDisplay.StopRecording()
Else
Dim rect As tagRECT
m_screenDisplay.DrawCache(Picture1.hDC, esriScreenRecording, rect, rect)
End If
End Sub
[C#] private void Picture1_Paint()
{
if ((m_screenDisplay.IsCacheDirty(esriScreenRecording)))
{
m_screenDisplay.StartRecording();
//Make a call to your Draw function. For example:
MyDraw(m_screenDisplay, Picture1.hDC);
m_screenDisplay.StopRecording();
}
else
{
tagRECT rect;
m_screenDisplay.DrawCache(Picture1.hDC, esriScreenRecording, rect, rect);
}
}
When you execute this code, you will see that nothing is drawn on the screen. This is due to the ScreenRecording cache not having its dirty flag set. To ensure that your Draw function is called when the first paint message is received, you must invalidate the cache. For example, add the following line to the end of a Form_Load method:
[VB.NET] m_screenDisplay.Invalidate(Nothing, True, CShort(esriScreenCache.esriScreenRecording))
[C#] m_screenDisplay.Invalidate(null, true, (short)esriScreenCache.esriScreenRecording);
Utilizing multiple caches
Some applications (for example, ArcMap) can require multiple display caches. To utilize multiple caches, follow these steps:
- Add a new cache using IScreenDisplay.AddCache. Save the cache ID that is returned.
- To draw to your cache, specify the cache ID to StartDrawing.
- To invalidate your cache, specify the cache ID to Invalidate.
- To draw from your cache, specify the cache ID to DrawCache.
To make your application support its own cache, add a member variable to hold the new cache. See the following:
[VB.NET] Private m_lCacheID As Long
m_lCacheID=m_screenDisplay.AddCache
[C#] private long m_lCacheID;
m_lCacheID=m_screenDisplay.AddCache;
Change the appropriate calls to use the m_lCacheID variable and remove the start and stop recording from the previously used Paintmethod.
Recording cache
ScreenDisplay can record what is drawn. Use StartRecording and StopRecording to let the display know exactly what to record. Use DrawCache to display the recording. Use CacheMemDC to get a handle to the memory device context for the recording bitmap. This functionality has the following important uses:
ScreenDisplay can record what is drawn. Use StartRecording and StopRecording to let the display know exactly what to record. Use DrawCache to display the recording. Use CacheMemDC to get a handle to the memory device context for the recording bitmap. This functionality has the following important uses:
- First, a single bitmap backing store is simple to implement; use a drawing sequence similar to the following code example.
If (m_screenDisplay.IsCacheDirty(CShort(esriScreenCache.esriScreenRecording))) Then
m_screenDisplay.StartRecording()
m_draw.StartDrawing(hDC, CShort(esriScreenCache.esriNoScreenCache))
DrawContents()
m_draw.FinishDrawing()
m_screenDisplay.StopRecording()
Else
m_screenDisplay.DrawCache(Picture1.hDC, CShort(esriScreenCache.esriScreenRecording, 0, 0))
End If
[C#] if ((m_screenDisplay.IsCacheDirty((short)esriScreenCache.esriScreenRecording)))
{
m_screenDisplay.StartRecording();
m_draw.StartDrawing(hDC, (short)esriScreenCache.esriNoScreenCache);
DrawContents();
m_draw.FinishDrawing();
m_screenDisplay.StopRecording();
}
else
{
m_screenDisplay.DrawCache(Picture1.hDC, (short)
esriScreenCache.esriScreenRecording, 0, 0);
}
- Second, clients can use allocated display caches (created with IScreenDisplay.AddCache) to cache different phases of their view drawing while still having a single bitmap (the recording) to use for quick refreshes. Finally, you can access the recording bitmap while drawing to implement interesting advanced rendering techniques such as translucency.
Caveats
If any part of your map contains transparency it will affect how things get refreshed. When a transparent layer draws, everything below it becomes part of the layer's rendering. As a result, the transparent layer must redraw from scratch every time that something below it changes.
Text also has transparency if the anti-aliasing setting is turned on in Microsoft Windows. This means that the text uses the layers that draw below it to carry out the anti-aliasing (the edges of the text are blended into the background). As a result, the annotation or auto-labels must draw whenever a layer changes.
See Also:
How to redraw the displayDevelopment 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 |