WPF、C#:在图像控件中的现有位图上绘制一条线
我在图像控件中有一个位图图像
每次用鼠标单击位图时,我都需要在单击鼠标的位置绘制一条红线。
我首先想到创建一个 Line 对象,但发现我无法添加 Line 。我需要一块画布。但是,如果我将图像放入画布中,我的位图不会延伸到整个画布(我发现,位图的坐标决定了画布上的位置,因此我的位图显示错误。)
然后我尝试使用图形
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();
但是,我的位图上没有绘制任何内容......
现在我想,我可能必须将位图放入数组中,然后更改像素颜色以获取位图中的线条。我相信,这会非常缓慢。
我现在正在尝试使用 VisualDrawing 进行一些操作,但是,我还没有让它工作:-(
在 WPF C# 中将一条线放到现有位图上的好方法是什么????以及如何删除它?
我会很高兴获得任何帮助!谢谢!我已经将其发布在 MS 论坛页面上,但到目前为止还没有答案。
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.)
Then I tried using graphics
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.
I am now trying something with visualDrawing, however, I have not got it to work yet:-(
What is a good way to get a line onto an existing bitmap in WPF C#???? and how to remove it?
I would be glad for any help! Thank you! I posted it already on the MS forum page, but no answer so far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您执行
Graphics.FromImage
时,此Graphics
类(以及System.Drawing.Pen
)不属于WPF,它们是WPF的一部分来自 WinForms,它们在内部使用 Windows 的 GDI+ 调用进行绘制,并且无法在 WPF 上进行绘制。如果您在编译第一行代码时没有出现错误,那么您的
bitmapImg
可能是一个System.Drawing.Image
(来自 WinForms)而不是 Image 控件来自 WPF (System.Window.Controls.Image
)。正如阿德里安提到的,最简单的方法可能是使用网格:
然后,在您的单击事件处理程序中,您可以使该线可见并为其提供所需的坐标:
When you do
Graphics.FromImage
, thisGraphics
class (and also theSystem.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 aSystem.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:
您可以将背景透明的画布放在 BitmapImage 之上,然后根据需要绘制线条。
xaml 文件中的代码:
Xaml.cs 中的代码:
输出
或者您甚至可以添加 ZIndex 到您的图像和线条,以便将它们放置在画布上的不同图层上。
You can place a canvas with the background as transparent on top of your BitmapImage and then draw the line as required.
Code from xaml file:
Code from Xaml.cs:
output
Or you can even add a ZIndex to your image and Line so that they are laid on different layers on canvas.