Click or drag to resize
CanvasBitmap Class
A bitmap is a 2D grid of pixels that form an image.
Inheritance Hierarchy
SystemObject
  Windows.UI.XamlDependencyObject
    Microsoft.Graphics.CanvasCanvasBitmap
      Microsoft.Graphics.CanvasCanvasRenderTarget

Namespace:  Microsoft.Graphics.Canvas
Assembly:  Microsoft.Graphics.Canvas (in Microsoft.Graphics.Canvas.dll) Version: 0.0.0.0
Syntax
C#
public class CanvasBitmap : DependencyObject, 
	ICanvasImage, IGraphicsEffectSource, IDisposable, IDirect3DSurface, ICanvasResourceCreatorWithDpi, 
	ICanvasResourceCreator

The CanvasBitmap type exposes the following members.

Properties
  NameDescription
Public propertyAlphaMode
Gets the alpha mode of this bitmap.
Public propertyBounds
Gets the bounds of the bitmap, in device independent pixels (DIPs).
Public propertyDescription
The description of this bitmap's underlying surface.
Public propertyDevice
The device associated with this bitmap.
Public propertyDpi
Gets the dots-per-inch (DPI) of this bitmap.
Public propertyFormat
Gets the pixel format of this bitmap.
Public propertySize
Gets the size of the bitmap, in device independent pixels (DIPs).
Public propertySizeInPixels
Gets the size of the bitmap, in pixels.
Top
Methods
  NameDescription
Public methodConvertDipsToPixels
Converts units from device independent pixels (DIPs) to physical pixels based on the DPI of this bitmap.
Public methodConvertPixelsToDips
Converts units from physical pixels to device independent pixels (DIPs) based on the DPI of this bitmap.
Public methodCopyPixelsFromBitmap(CanvasBitmap)
Copies the entire bitmap specified into this bitmap, at position (0, 0).
Public methodCopyPixelsFromBitmap(CanvasBitmap, Int32, Int32)
Copies the entire bitmap specified into this bitmap at the point specified.
Public methodCopyPixelsFromBitmap(CanvasBitmap, Int32, Int32, Int32, Int32, Int32, Int32)
Copies the specified region of a bitmap into this bitmap, at the point specified.
Public methodStatic memberCreateFromBytes(ICanvasResourceCreator, Byte, Int32, Int32, DirectXPixelFormat)
Creates a CanvasBitmap from an array of bytes, using the specified pixel width/height, premultiplied alpha and default (96) DPI.
Public methodStatic memberCreateFromBytes(ICanvasResourceCreator, IBuffer, Int32, Int32, DirectXPixelFormat)
Creates a CanvasBitmap from the bytes of the specified buffer, using the specified pixel width/height, premultiplied alpha and default (96) DPI.
Public methodStatic memberCreateFromBytes(ICanvasResourceCreator, Byte, Int32, Int32, DirectXPixelFormat, Single)
Creates a CanvasBitmap from an array of bytes, using the specified pixel width/height, specified DPI, and premultiplied alpha.
Public methodStatic memberCreateFromBytes(ICanvasResourceCreator, IBuffer, Int32, Int32, DirectXPixelFormat, Single)
Creates a CanvasBitmap from the bytes of the specified buffer, using the specified pixel width/height, specified DPI, and premultiplied alpha.
Public methodStatic memberCreateFromBytes(ICanvasResourceCreator, Byte, Int32, Int32, DirectXPixelFormat, Single, CanvasAlphaMode)
Creates a CanvasBitmap from an array of bytes, using the specified pixel width/height, DPI and alpha behavior.
Public methodStatic memberCreateFromBytes(ICanvasResourceCreator, IBuffer, Int32, Int32, DirectXPixelFormat, Single, CanvasAlphaMode)
Creates a CanvasBitmap from the bytes of the specified buffer, using the specified pixel width/height, DPI and alpha behavior.
Public methodStatic memberCreateFromColors(ICanvasResourceCreator, Color, Int32, Int32)
Creates a CanvasBitmap from an array of colors, using the specified pixel width/height, premultiplied alpha and default (96) DPI.
Public methodStatic memberCreateFromColors(ICanvasResourceCreator, Color, Int32, Int32, Single)
Creates a CanvasBitmap from an array of colors, using the specified pixel width/height, specified DPI, and premultiplied alpha.
Public methodStatic memberCreateFromColors(ICanvasResourceCreator, Color, Int32, Int32, Single, CanvasAlphaMode)
Creates a CanvasBitmap from an array of colors, using the specified pixel width/height, DPI and alpha behavior.
Public methodStatic memberCreateFromDirect3D11Surface(ICanvasResourceCreator, IDirect3DSurface)
Creates a CanvasBitmap from an existing Direct3D graphics surface.
Public methodStatic memberCreateFromDirect3D11Surface(ICanvasResourceCreator, IDirect3DSurface, Single)
Creates a CanvasBitmap from an existing Direct3D graphics surface.
Public methodStatic memberCreateFromDirect3D11Surface(ICanvasResourceCreator, IDirect3DSurface, Single, CanvasAlphaMode)
Creates a CanvasBitmap from an existing Direct3D graphics surface, using the specified DPI and alpha behavior.
Public methodStatic memberCreateFromSoftwareBitmap
Creates a CanvasBitmap from a SoftwareBitmap.
Public methodDispose
Releases all resources used by the CanvasBitmap.
Public methodGetBounds(ICanvasResourceCreator)
Retrieves the bounds of this CanvasBitmap.
Public methodGetBounds(ICanvasResourceCreator, Matrix3x2)
Retrieves the bounds of this CanvasBitmap.
Public methodGetPixelBytes
Returns an array of raw byte data for the entire bitmap.
Public methodGetPixelBytes(IBuffer)
Copies raw byte data for the entire bitmap into the specified buffer.
Public methodGetPixelBytes(Int32, Int32, Int32, Int32)
Returns an array of raw byte data for a subregion of the bitmap.
Public methodGetPixelBytes(IBuffer, Int32, Int32, Int32, Int32)
Copies raw byte data for a subregion of the bitmap into the specified buffer.
Public methodGetPixelColors
Returns an array of color data for the entire bitmap.
Public methodGetPixelColors(Int32, Int32, Int32, Int32)
Returns an array of color data for a subregion of the bitmap.
Public methodStatic memberLoadAsync(ICanvasResourceCreator, String)
Loads a bitmap from an image file (jpeg, png, etc.)
Public methodStatic memberLoadAsync(ICanvasResourceCreator, Uri)
Loads a bitmap from an image file (jpeg, png, etc.) located at a URI.
Public methodStatic memberLoadAsync(ICanvasResourceCreator, IRandomAccessStream)
Loads a bitmap from a stream.
Public methodStatic memberLoadAsync(ICanvasResourceCreator, String, Single)
Loads a bitmap from an image file (jpeg, png, etc.), and assigns it the specified DPI.
Public methodStatic memberLoadAsync(ICanvasResourceCreator, Uri, Single)
Loads a bitmap from an image file (jpeg, png, etc.) located at a URI, and assigns it the specified DPI.
Public methodStatic memberLoadAsync(ICanvasResourceCreator, IRandomAccessStream, Single)
Loads a bitmap from a stream, and assigns it the specified alpha behavior.
Public methodStatic memberLoadAsync(ICanvasResourceCreator, String, Single, CanvasAlphaMode)
Loads a bitmap from an image file (jpeg, png, etc.), and assigns it the specified DPI and alpha behavior.
Public methodStatic memberLoadAsync(ICanvasResourceCreator, Uri, Single, CanvasAlphaMode)
Loads a bitmap from an image file (jpeg, png, etc.) located at a URI, and assigns it the specified DPI and alpha behavior.
Public methodStatic memberLoadAsync(ICanvasResourceCreator, IRandomAccessStream, Single, CanvasAlphaMode)
Loads a bitmap from a stream, and assigns it the specified DPI and alpha behavior.
Public methodSaveAsync(String)
Saves the entire bitmap to a file with the specified file name, using a default quality level of 0.9 and CanvasBitmapFileFormat.Auto.
Public methodSaveAsync(String, CanvasBitmapFileFormat)
Saves the entire bitmap to a file with the specified file name and file format, and a default quality level of 0.9.
Public methodSaveAsync(IRandomAccessStream, CanvasBitmapFileFormat)
Saves the entire bitmap to the specified stream with the specified file format, using a default quality level of 0.9.
Public methodSaveAsync(String, CanvasBitmapFileFormat, Single)
Saves the entire bitmap to a file with the specified file name and file format, and quality level.
Public methodSaveAsync(IRandomAccessStream, CanvasBitmapFileFormat, Single)
Saves the entire bitmap to the specified stream with the specified file format and quality level.
Public methodSetPixelBytes(Byte)
Sets the byte data of the bitmap from the specified array.
Public methodSetPixelBytes(IBuffer)
Sets the byte data of the bitmap from the specified buffer.
Public methodSetPixelBytes(Byte, Int32, Int32, Int32, Int32)
Sets the byte data of a subregion of the bitmap.
Public methodSetPixelBytes(IBuffer, Int32, Int32, Int32, Int32)
Sets the byte data of a subregion of the bitmap from the specified buffer.
Public methodSetPixelColors(Color)
Sets the color data of the bitmap from the specified array.
Public methodSetPixelColors(Color, Int32, Int32, Int32, Int32)
Sets the color data for a subregion of the bitmap from the specified array.
Top
Remarks

When using Direct2D interop, this Win2D class corresponds to an ID2D1Bitmap1 without the D2D1_BITMAP_OPTIONS_TARGET flag.

Examples

To load and draw a bitmap, first add a CanvasControl to your XAML page.

Add the image files you want to display (png, jpeg, etc.) to your Visual Studio project, and make sure their Build Action is set to Content.

In the page implementation class (eg. MainPage.xaml.cs), declare fields to hold the bitmaps:

CanvasBitmap cat, mouse;

Use the CreateResources event to asynchronously load the bitmaps:

void myWidget_CreateResources(CanvasControl sender, object args)
{
    args.TrackAsyncAction(CreateResourcesAsync(sender).AsAsyncAction());
}

async Task CreateResourcesAsync(CanvasControl sender)
{
    cat = await CanvasBitmap.LoadAsync(sender, "ShawnsCat.jpg");
    mouse = await CanvasBitmap.LoadAsync(sender, "Mouse.png");
}

Use the Draw event to display them:

void myWidget_Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
    args.DrawingSession.DrawImage(cat, 42, 42);
    args.DrawingSession.DrawImage(mouse, 123, 45);
}
See Also