Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Drawing on a image control's image.

$
0
0

Hi,

I've been checking all of the examples I can find, but I can't find anything that seems to do what I need.

I have an image control on my window. It has a bitmap image as the image source. From the code behind, I need to grab the bitmap image from the image control, and draw a rectangle onto it, at a specific location.

The rectangle will be 30x30 but I need to specify the X and Y location of the rectangle. I've found many examples of drawing visual and render target bitmap, but I wasn't able to get any of them working correctly.

I just want an outline of a rectangle, I do not want a filled rectangle. Here is one code example I found on Google, but it doesn't work, because it creates a new render target bitmap. I need to use an already existing image and draw on top of that (the image in the image control);

            Pen pen = new Pen(Brushes.DarkMagenta, 2D);
            Rect rect = new Rect(destinationX + 1, destinationY + 1, 30, 30);
            DrawingVisual dv = new DrawingVisual();
            DrawingContext dc = dv.RenderOpen();
            dc.DrawRectangle(null, pen, rect);
            dc.Close();
            RenderTargetBitmap bmp = new RenderTargetBitmap();
            bmp.Render(dv);
            imgMap.Source = bmp;

So here I will post two images as an example. I drew these in paint, but this is what I want. Lets say the image control is using this blue/purple bitmap image as image source:

All I want to do:

1. Grab the image controls image source (bitmap)

2. Draw a rectangle onto it.

3. Send the updated image back to the image control

Here is the outcome:

It seems to simple, and I can do it VERY easy with Windows Forms, but I cannot do it with WPF.

Edit: I don't want to use a rectangle control and make it appear on screen. I want to draw right into the bitmap. This way, when I save the bitmap, the rectangle I drew will be saved to the *.bmp file.

Thank You.



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>