WPF、C#:在图像控件中的现有位图上绘制一条线

发布于 2024-10-20 14:08:11 字数 809 浏览 3 评论 0原文

我在图像控件中有一个位图图像

每次用鼠标单击位图时,我都需要在单击鼠标的位置绘制一条红线。

我首先想到创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何止钟意 2024-10-27 14:08:11

当您执行Graphics.FromImage时,此Graphics类(以及System.Drawing.Pen)不属于WPF,它们是WPF的一部分来自 WinForms,它们在内部使用 Windows 的 GDI+ 调用进行绘制,并且无法在 WPF 上进行绘制。

如果您在编译第一行代码时没有出现错误,那么您的 bitmapImg 可能是一个 System.Drawing.Image (来自 WinForms)而不是 Image 控件来自 WPF (System.Window.Controls.Image)。

正如阿德里安提到的,最简单的方法可能是使用网格:

<Grid>
    <Image Source="your image" />
    <Line Name="line" Visibility="Hidden" Stroke="Red" StrokeThickness="1" />
</Grid>

然后,在您的单击事件处理程序中,您可以使该线可见并为其提供所需的坐标:

line.Visibility = Visible;
line.X1 = mouse_x;
line.Y1 = mouse_y;
line.X2 = ...;
line.Y2 = ...;

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:

<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 = ...;
瞄了个咪的 2024-10-27 14:08:11

您可以将背景透明的画布放在 BitmapImage 之上,然后根据需要绘制线条。
xaml 文件中的代码:

<Grid>
    <Image Source="C:\Users\sm143444\Desktop\download.jpg" />
    <Canvas Background="Transparent"  x:Name="draw" />
</Grid>

Xaml.cs 中的代码:

public MainWindow()
    {
        InitializeComponent();
        Point startPoint = new Point(50, 50);
        Line newLine = new Line();
        newLine.Stroke = Brushes.Black;
        newLine.Fill = Brushes.Black;
        newLine.StrokeLineJoin = PenLineJoin.Bevel;
        newLine.X1 = startPoint.X;
        newLine.Y1 = startPoint.Y;
        newLine.X2 = startPoint.X + 100;
        newLine.Y2 = startPoint.Y + 100;
        newLine.StrokeThickness = 2;
        this.draw.Children.Add(newLine);
    }

输出

或者您甚至可以添加 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:

<Grid>
    <Image Source="C:\Users\sm143444\Desktop\download.jpg" />
    <Canvas Background="Transparent"  x:Name="draw" />
</Grid>

Code from Xaml.cs:

public MainWindow()
    {
        InitializeComponent();
        Point startPoint = new Point(50, 50);
        Line newLine = new Line();
        newLine.Stroke = Brushes.Black;
        newLine.Fill = Brushes.Black;
        newLine.StrokeLineJoin = PenLineJoin.Bevel;
        newLine.X1 = startPoint.X;
        newLine.Y1 = startPoint.Y;
        newLine.X2 = startPoint.X + 100;
        newLine.Y2 = startPoint.Y + 100;
        newLine.StrokeThickness = 2;
        this.draw.Children.Add(newLine);
    }

output

Or you can even add a ZIndex to your image and Line so that they are laid on different layers on canvas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文