创建WinForms表单/控件C#的实时预览的可能性

时间:2018-10-08 07:36:21

标签: c# winforms system.drawing system.graphics

我正在寻找创建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

0 个答案:

没有答案