如何在图像中绘制移动线

How to draw a moving line in an image

我正在尝试在白色矩形上绘制一条水平线,对应于它上面的光标位置。

public static void CreateMapHistoGramModifiedByColourBar(int LineYindex)
{
    float[] dashValues = { 1, 1, 1 };
    Pen blackPen = new Pen(Color.Black, 1);
    blackPen.DashPattern = dashValues;
    Point P1 = new Point(0, LineYindex);
    Point P2 = new Point(RefBarWidth, LineYindex);
    using (Graphics g = Graphics.FromImage(Image.FromFile(WaferMapHistogramFileName)))
    {
        g.DrawLine(blackPen, P1, P2);
    }
}

上面的代码似乎没有对保存的图像做任何事情。我该如何解决这个问题?

使用 Cursor.Position.YCursor.Position.X 获取鼠标 在屏幕上的位置 。然后您可以抵消这些以说明您的白色矩形在表格中的位置。

如果您希望此行在您移动鼠标时刷新,请将 redraw/repaint 事件绑定到鼠标移动事件 MouseMove。如果这一切都在 PictureBox 内绘制,请在重绘

之前先使用 pictureBox1.InitialImage = null;

判断这条线:

the above mentioned code does not seem to be doing anything to the saved image

听起来您想在图像上画一条线,然后将新图像保存回来。目前你所做的只是加载一个 Image 对象,在其上绘制然后丢弃它:

using (Graphics g = Graphics.FromImage(Image.FromFile(WaferMapHistogramFileName)))
{
    g.DrawLine(blackPen, P1, P2);
}

请注意 Image class 也实现了 IDisposable 所以你应该处理它。

把所有这些放在一起,你应该得到这样的东西:

using (Image image = Image.FromFile(WaferMapHistogramFileName))
using (Graphics g = Graphics.FromImage(image))
{
    g.DrawLine(blackPen, P1, P2);

    image.Save(@"Save somewhere, here: WaferMapHistogramFileName?");
}

请注意,将图像保存回 WaferMapHistogramFileName 将覆盖原始图像。


旁注;来自这一行:

corresponding to the cursor position

根据光标在图像上的位置判断,您在图像上画了一条线。如果是这种情况,你应该确保你正在偏移光标位置,因为这会给你它在 ScreenFormControl.

上的位置