Accord.Controls.VideoSourcePlayer.VideoSourcePlayer_Paint C# (CSharp) Method

VideoSourcePlayer_Paint() private method

private VideoSourcePlayer_Paint ( object sender, PaintEventArgs e ) : void
sender object
e PaintEventArgs
return void
        private void VideoSourcePlayer_Paint(object sender, PaintEventArgs e)
        {
            if (!Visible)
            {
                return;
            }

            // is it required to update control's size/position
            if ((needSizeUpdate) || (firstFrameNotProcessed))
            {
                UpdatePosition();
                needSizeUpdate = false;
            }

            lock (sync)
            {
                Graphics g = e.Graphics;
                Rectangle rect = this.ClientRectangle;
                Pen borderPen = new Pen(borderColor, 1);

                // draw rectangle
                g.DrawRectangle(borderPen, rect.X, rect.Y, rect.Width - 1, rect.Height - 1);

                if (videoSource != null)
                {
                    if ((currentFrame != null) && (lastMessage == null))
                    {
                        Bitmap frame = (convertedFrame != null) ? convertedFrame : currentFrame;

                        if (keepRatio)
                        {
                            double ratio = (double)frame.Width / frame.Height;
                            Rectangle newRect = rect;

                            if (rect.Width < rect.Height * ratio)
                            {
                                newRect.Height = (int)(rect.Width / ratio);
                            }
                            else
                            {
                                newRect.Width = (int)(rect.Height * ratio);
                            }

                            newRect.X = (rect.Width - newRect.Width) / 2;
                            newRect.Y = (rect.Height - newRect.Height) / 2;

                            g.DrawImage(frame, newRect.X + 1, newRect.Y + 1, newRect.Width - 2, newRect.Height - 2);
                        }
                        else
                        {
                            // draw current frame
                            g.DrawImage(frame, rect.X + 1, rect.Y + 1, rect.Width - 2, rect.Height - 2);
                        }

                        firstFrameNotProcessed = false;
                    }
                    else
                    {
                        // create font and brush
                        SolidBrush drawBrush = new SolidBrush(this.ForeColor);

                        g.DrawString((lastMessage == null) ? "Connecting ..." : lastMessage,
                            this.Font, drawBrush, new PointF(5, 5));

                        drawBrush.Dispose();
                    }
                }

                borderPen.Dispose();
            }
        }