// Render a cached bitmap to a drawing context. void RenderMapArea(DrawingContext dc, CachedMapArea mapArea) { dc.DrawImage(mapArea.bitmap, new Rect(mapArea.portion.X, mapArea.portion.Y, mapArea.pixelSize * mapArea.bitmap.Width, mapArea.pixelSize * mapArea.bitmap.Height)); }
// Fill in the bitmap member of a cached map area with a bitmap, created from the current map. void CreateBitmap(CachedMapArea mapArea) { // Determine the size of the bitmap we need. int height = (int) Math.Ceiling(mapArea.portion.Height / mapArea.pixelSize); int width = (int) Math.Ceiling(mapArea.portion.Width / mapArea.pixelSize); // Create transformation matrix from map coords to the bitmap coords Matrix matrix = Matrix.Identity; matrix.ScalePrepend(1 / mapArea.pixelSize, 1 / mapArea.pixelSize); matrix.TranslatePrepend(- mapArea.portion.X, - mapArea.portion.Y); // Get the render options. RenderOptions renderOpts = new RenderOptions(); renderOpts.usePatternBitmaps = false; renderOpts.minResolution = (float) mapArea.pixelSize; // Create a visual of the map, appropriately transformed DrawingVisual visual = new DrawingVisual(); DrawingContext dc = visual.RenderOpen(); dc.PushTransform(new MatrixTransform(matrix)); using (map.Read()) map.Draw(dc, mapArea.portion, renderOpts); dc.Close(); // Draw it into a new bitmap. if (mapArea.bitmap != null && mapArea.bitmap.PixelWidth >= width && mapArea.bitmap.PixelHeight >= height) mapArea.bitmap.Clear(); // reuse the old bitmap again by clearing it. else mapArea.bitmap = new RenderTargetBitmap(width, height, 96.0, 96.0, PixelFormats.Pbgra32); mapArea.bitmap.Render(visual); // Mark as up to date. mapArea.isUpToDate = true; }