WPF,C#:画一条线到图像控制现有的位图 [英] WPF, C#: Draw a line onto existing bitmap in image control

查看:898
本文介绍了WPF,C#:画一条线到图像控制现有的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像控制位图图像

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 onto it, at the place where I clicked the mouse.

我首先想到创建一个线的对象,但发现了,我不能添加一行。我需要的画布。但是,如果我把我的形象在一个画布,我的位图不超过整个画布伸展(我发现,位图的坐标确定在画布上的地方,所以我的位图显示错误)

I first thought of creating a Line object, but found out, that I cannot add the Line. I would need a canvas. But if I put my image in a canvas, my bitmap does not stretch over the whole canvas (I found out, that the coordinates of the bitmap determine the place on the canvas, so my bitmap is wrongly displayed.)

然后我试图用图形

Graphics graphics = Graphics.FromImage(bitmapImg);
graphics.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Red), 0, 0,  bitmapImg.Width, bitmapImg.Height); //not the line yet, just for testing
    graphics.DrawImage(bitmapImg, 0, 0, bitmapImg.Width,bitmapImg.Height);
        graphics.Dispose();

不过,我不`吨得到任何东西涂在我的位图........

However, I don`t get anything painted onto my bitmap........

现在我想,我可能必须得到位图到一个数组,然后改变像素的颜色得到位图线。我相信,这将是非常缓慢的。

Now I think, I probably have to get the bitmap into an array and then change the pixel color to get the line in the bitmap. I believe, that this would be very slow.

我现在正在试图与visualDrawing的东西,但是,我还没有得到它的工作还没有: - (

I am now trying something with visualDrawing, however, I have not got it to work yet:-(

什么是一个很好的方式来获得一个上线在WPF C#现有的位图????以及如何去除呢?

What is a good way to get a line onto an existing bitmap in WPF C#???? and how to remove it?

我会很高兴的任何帮助!谢谢!我张贴已经MS论坛页面上,但没有答案为止。

I would be glad for any help! Thank you! I posted it already on the MS forum page, but no answer so far.

推荐答案

当你做 Graphics.FromImage ,这个图形类(以及 System.Drawing.Pen来)不属于WPF,他们从WinForms的一部分,它们在内部使用Windows的GDI +调用来绘制和不能借鉴WPF之上。

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.

如果您编译code的第一线,当你没有得到一个错误,那么很可能你的 bitmapImg 系统。 Drawing.Image (从的WinForms)不是从WPF Image控件( System.Window.Controls.Image )。

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).

正如adrianm提到的,最简单的方法可能是使用一个网格:

As adrianm mentioned, the easiest way will probably be to use a Grid:

<Grid>
    <Image Source="your image" />
    <Line Name="line" Visibility="Hidden" Stroke="Red" StrokeThickness="1" />
</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 = ...;

这篇关于WPF,C#:画一条线到图像控制现有的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆