Possible Duplicate:
How to render bitmap into canvas in WPF?
What I want is simple. I want to place a BitmapImage into a Canvas in C#. My app is based on WPF. I searched for this and I found similar questions, but I could not find what I’m looking for.
In short, I have this:
BitmapImage img = new BitmapImage(new Uri("c:\\xyz.jpg"));
And I want to put this in the canvas, the mean is not that important, it can be a rectangle or whatever.
A
BitmapImageobject cannot be positioned inside aCanvasbecause it’s not a control. What you can do is to derive your own class from theCanvasand override theOnRender()method to draw your bitmap. Basically something like this:Of course you may need to expose the file path and coordinates inside the
Canvasthrough properties. If you do not want to declare your own class just to draw a bitmap then you can’t use theBitmapImageclass directly. The control to display images isImage, let’s try this:Now you can put the
Imagecontrol where you want inside theCanvas.