Share via


MapTileBitmapRequest.PixelData Property

Definition

Gets the bitmap data for the CustomMapTileDataSource.

public IRandomAccessStreamReference PixelData { get; set; }

Property Value

The bitmap data.

Remarks

CustomMapTileDataSource supports drawing tiles in memory and returning them as a stream of pixels. The following code sample demonstrates one approach for drawing tiles in memory.

// Create new custom tile source
CustomMapTileDataSource customDataSource = new CustomMapTileDataSource();
customDataSource.BitmapRequested += customDataSource_BitmapRequestedAsync;
customTileSource = new MapTileSource(customDataSource);

/// <summary>
/// BitmapRequested event handler for CustomMapTileDataSource.BitmapRequested event
/// </summary>
/// <param name="sender">sender</param>
/// <param name="args">args for ZoomLevel, X, Y and the PixelData</param>
private async void customDataSource_BitmapRequestedAsync(CustomMapTileDataSource sender, MapTileBitmapRequestedEventArgs args)
{
    var deferral = args.Request.GetDeferral();
    args.Request.PixelData = await CreateBitmapAsStreamAsync();
    deferral.Complete();
}

/// <summary>
/// Create memory buffer with length of 256 * 256 * 4.  
/// This memory buffer is holding 256 * 256 pixels
/// </summary>
/// <returns>memory buffer that fills with red opaque pixels.</returns>
private async Task<Windows.Storage.Streams.RandomAccessStreamReference> CreateBitmapAsStreamAsync()
{
    int pixelHeight = 256;
    int pixelWidth = 256;
    int bpp = 4;

    byte[] bytes = new byte[pixelHeight * pixelWidth * bpp];

    for (int y = 0; y < pixelHeight; y++)
    {
        for (int x = 0; x < pixelWidth; x++)
        {
            int pixelIndex = y * pixelWidth + x;
            int byteIndex = pixelIndex * bpp;

            bytes[byteIndex] = 0xff;        // Red
            bytes[byteIndex + 1] = 0x00;    // Green
            bytes[byteIndex + 2] = 0x00;    // Blue
            bytes[byteIndex + 3] = 0x80;    // Alpha (0xff = fully opaque)
        }
    }

    // Create RandomAccessStream from byte array
    InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
    IOutputStream outputStream = randomAccessStream.GetOutputStreamAt(0);
    DataWriter writer = new DataWriter(outputStream);
    writer.WriteBytes(bytes);
    await writer.StoreAsync();
    await writer.FlushAsync();
    return RandomAccessStreamReference.CreateFromStream(randomAccessStream);
}

Applies to

See also