I have a bitmap image in an image control
I need to draw a red line on the bitmap each time I click with the mouse on
When you do Graphics.FromImage
, this Graphics
class (and also the System.Drawing.Pen
) do not belong to WPF, they are part from WinForms and they are internally using Windows' GDI+ calls to draw and cannot draw on top of WPF.
If you didn't got an error when compiling the first line of your code, then probably your bitmapImg
is a System.Drawing.Image
(from WinForms) not an Image control from WPF (System.Window.Controls.Image
).
As adrianm mentioned, the easiest way will probably be to use a Grid:
Then, in your click event handler you can make the line visible and give it the coordinates you want:
line.Visibility = Visible;
line.X1 = mouse_x;
line.Y1 = mouse_y;
line.X2 = ...;
line.Y2 = ...;