Possibility to create live preview of WinForms form/control c#

448 views Asked by At

I'm looking for possibility to create a live preview of WinForms Form/Control. My application got two windows - one, where someone can draw on Image (which is in PictureBox), and second window, where someone can see what was been drawn on 1st window. At this moment, I tried to put the same PictureBox with image on second window, and draw the same lines on it like in window 1. Unfortunately preview like this is not live preview, because image on window 2 is refreshed when someone stop drawing on window 1. Below I'm sending a code, which is used to draw on 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));

    }

Have you any idea, how to force UI of second window to refresh after every point?

Hawex

0

There are 0 answers