A BitmapImage
object cannot be positioned inside a Canvas
because it's not a control. What you can do is to derive your own class from the Canvas
and override the OnRender()
method to draw your bitmap. Basically something like this:
class CanvasWithBitmap : Canvas
{
public CanvasWithBitmap()
{
_image = new BitmapImage(new Uri(@"c:\xyz.jpg"));
}
protected override void OnRender(DrawingContext dc)
{
dc.DrawImage(_image,
new Rect(0, 0, _image.PixelWidth, _image.PixelHeight));
}
private BitmapImage _image;
}
Of course you may need to expose the file path and coordinates inside the Canvas
through properties. If you do not want to declare your own class just to draw a bitmap then you can't use the BitmapImage
class directly. The control to display images is Image
, let's try this:
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(@"c:\xyz.jpg");
bitmap.EndInit();
Image image = new Image();
image.Source = bitmap;
Now you can put the Image
control where you want inside the Canvas
.