1

我正在寻找创建 WinForms 表单/控件的实时预览的可能性。我的应用程序有两个窗口 - 一个可以在 Image 上绘制(在 PictureBox 中),第二个窗口可以看到在第一个窗口上绘制的内容。此时,我尝试在第二个窗口上放置与图像相同的 PictureBox,并在其上绘制与窗口 1 中相同的线条。不幸的是,这样的预览不是实时预览,因为当有人停止绘制时,窗口 2 上的图像会刷新窗口 1. 下面我发送一个代码,用于在 Image 上绘图。

public void AddNewPoint(int X, int Y)
{
    if (_firstPointInStroke)
    {
        _firstpoint = _mainCustomPictureBox.PointToClient(new System.Drawing.Point(X, Y));
        _firstPointInStroke = false;
            }
        _secondpoint = _mainCustomPictureBox.PointToClient(new System.Drawing.Point(X, Y));

        var g = Graphics.FromImage(_mainCustomPictureBox.Image);

        var wfactor = (double)_mainCustomPictureBox.Image.Width / _mainCustomPictureBox.Width;
        var hfactor = (double)_mainCustomPictureBox.Image.Height / _mainCustomPictureBox.Height;
        var resizeFactor = Math.Max(wfactor, hfactor);
        System.Windows.Shapes.Line currentLine;
        if (hfactor > wfactor)
        {
            _firstpoint.X = (float)((_firstpoint.X - ((_mainCustomPictureBox.Width - ((double)_mainCustomPictureBox.Image.Width / resizeFactor)) / 2)) * resizeFactor);
            _firstpoint.Y = (float)(_firstpoint.Y * resizeFactor);
            _secondpoint.X = (float)((_secondpoint.X - ((_mainCustomPictureBox.Width - ((double)_mainCustomPictureBox.Image.Width / resizeFactor)) / 2)) * resizeFactor);
            _secondpoint.Y = (float)(_secondpoint.Y * resizeFactor);
        }
        else
        {
            _firstpoint.X = (float)(_firstpoint.X * resizeFactor);
            _firstpoint.Y = (float)((_firstpoint.Y - ((_mainCustomPictureBox.Height - ((double)_mainCustomPictureBox.Image.Height / resizeFactor)) / 2)) * resizeFactor);
            _secondpoint.X = (float)(_secondpoint.X * resizeFactor);
            _secondpoint.Y = (float)((_secondpoint.Y - ((_mainCustomPictureBox.Height - ((double)_mainCustomPictureBox.Image.Height / resizeFactor)) / 2)) * resizeFactor);
        }
        currentLine = new System.Windows.Shapes.Line { X1 = _firstpoint.X, X2 = _secondpoint.X, Y1 = _firstpoint.Y, Y2 = _secondpoint.Y };
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        g.DrawLine(_pen, (float)currentLine.X1, (float)currentLine.Y1, (float)currentLine.X2, (float)currentLine.Y2);
        g.Dispose();
        _mainCustomPictureBox.Invalidate();
        if (_previewCustomPictureBox != null && _previewCustomPictureBox.Image != null)
        {
            var gg = Graphics.FromImage(_previewCustomPictureBox.Image);
            gg.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            gg.DrawLine(_pen, (float)currentLine.X1, (float)currentLine.Y1, (float)currentLine.X2, (float)currentLine.Y2);
            gg.Dispose();
            _previewCustomPictureBox.Invalidate();
        }
        _firstpoint = _mainCustomPictureBox.PointToClient(new System.Drawing.Point(X, Y));

    }

你有什么想法,如何强制第二个窗口的 UI 在每一点之后刷新?

Hawex

4

0 回答 0