Windows Forms using PaintEvent leads to paint artifacts

I’m trying to make a function that highlights certain area’s of a picture.
In practice this means that this function darkens the entire picture except for the desired area’s. The positions and size of these area’s are to be given as arguments.

I’ve made a proof of concept, which works but has some odd artifacts which I’m unable to get rid of. And I’ve got no idea why they are there. See picture below:

I’ve added two transparent area’s, those are the area’s you can see on the test picture.
But the artifacts to the left of the picture was not made by me (at least intentionally).

All of the code for this is in its own .CS file. (Except for the few lines that call the class of course…)

Designer:

partial class cComponentWindow
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
     }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.btnOkay = new System.Windows.Forms.Button();
        this.StaticLabel = new System.Windows.Forms.Label();
        this.DynamicLabel = new System.Windows.Forms.Label();
        this.PCBPictureBox = new System.Windows.Forms.PictureBox();
        ((System.ComponentModel.ISupportInitialize)(this.PCBPictureBox)).BeginInit();
        this.SuspendLayout();
        // 
        // btnOkay
        // 
        this.btnOkay.Location = new System.Drawing.Point(12, 480);
        this.btnOkay.Name = "btnOkay";
        this.btnOkay.Size = new System.Drawing.Size(820, 23);
        this.btnOkay.TabIndex = 1;
        this.btnOkay.Text = "Okay";
        this.btnOkay.UseVisualStyleBackColor = true;
        this.btnOkay.Click += new System.EventHandler(this.btnOkay_Click);
        // 
        // StaticLabel
        // 
        this.StaticLabel.AutoSize = true;
        this.StaticLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.StaticLabel.Location = new System.Drawing.Point(8, 2);
        this.StaticLabel.Name = "StaticLabel";
        this.StaticLabel.Size = new System.Drawing.Size(130, 20);
        this.StaticLabel.TabIndex = 3;
        this.StaticLabel.Text = "Some Static Text";
        // 
        // DynamicLabel
        // 
        this.DynamicLabel.AutoSize = true;
        this.DynamicLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.DynamicLabel.Location = new System.Drawing.Point(12, 25);
        this.DynamicLabel.Name = "DynamicLabel";
        this.DynamicLabel.Size = new System.Drawing.Size(104, 20);
        this.DynamicLabel.TabIndex = 2;
        this.DynamicLabel.Text = "Dynamic Text";
        // 
        // PCBPictureBox
        // 
        this.PCBPictureBox.InitialImage = null;
        this.PCBPictureBox.Location = new System.Drawing.Point(25, 25);
        this.PCBPictureBox.Name = "PCBPictureBox";
        this.PCBPictureBox.Size = new System.Drawing.Size(790, 450);
        this.PCBPictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
        this.PCBPictureBox.TabIndex = 4;
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form_Paint);
        this.PCBPictureBox.TabStop = false;
        // 
        // cComponentWindow
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(844, 511);
        this.Controls.Add(this.DynamicLabel);
        this.Controls.Add(this.StaticLabel);
        this.Controls.Add(this.btnOkay);
        this.Controls.Add(this.PCBPictureBox);
        this.Name = "cComponentWindow";
        this.Text = "Title";
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form_Paint); // I also tried commenting this auto generated one out. That didn't change anything.
        ((System.ComponentModel.ISupportInitialize)(this.PCBPictureBox)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();
    }

    #endregion
    private System.Windows.Forms.Button btnOkay;
    private System.Windows.Forms.PictureBox PCBPictureBox;
    private System.Windows.Forms.Label StaticLabel;
    private System.Windows.Forms.Label DynamicLabel;
}

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
public partial class cComponentWindow
{
    // Variables:
    private UInt16 SizeX;
    private UInt16 SizeY;
    private UInt16 OffsetX;
    private UInt16 OffsetY;
    private bool[,] Matrix;
    private PaintEventArgs e;
    private Pen DarkPen = new Pen(Color.FromArgb(127, 0, 0, 0));

    // Methodes:
    public cComponentWindow(UInt16 OffsetX, UInt16 OffsetY, UInt16 SizeX, UInt16 SizeY, String CHK, Image Picture)
    {
        InitializeComponent();
        this.OffsetX = OffsetX;
        this.OffsetY = OffsetY;
        this.SizeX = SizeX;
        this.SizeY = SizeY;
        this.DynamicLabel.Text = CHK;
        this.PCBPictureBox.Image = Picture;
        this.Matrix = new bool[this.SizeX, this.SizeY];
    }

    private void btnOkay_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.OK;
        Close();
    }

    private void Form_Paint(object sender, PaintEventArgs e)
    {
        this.e = e;
        this.Draw();
    }

    public void Highlight(UInt16 OffsetX, UInt16 OffsetY, UInt16 SizeX, UInt16 SizeY)
    {
        for (UInt16 x = 0; x < SizeX; x++)
        {
            for (UInt16 y = 0; y < SizeY; y++)
            {
                this.Matrix[OffsetX + x, OffsetY + y] = true;
            }
        }
    }

    private void Draw() // OG time: 2m20s after some optimisations 0.5s (-99.6%)
    {
        //e.Graphics.FillRectangle(new SolidBrush(Color.Green), 0, 27, 790, 395 + 27);
        // Go through the entire matrix.
        for (UInt16 y = 0; y < this.SizeY; y++)
        {
            UInt16 StartBlock = 0;
            bool PreviousValue = false;
            for (UInt16 x = 0; x < this.SizeX; x++)
            {
                bool CurrentValue = Matrix[x, y];
                if (CurrentValue && !PreviousValue)
                {   // Draw the line to a highlight.
                    e.Graphics.DrawLine(DarkPen, this.OffsetX + StartBlock, this.OffsetY + y, this.OffsetX + x - 1, this.OffsetY + y);
                }
                if (!CurrentValue && this.SizeX - 1 == x)
                {   // Draw the line to the edge of the screen.
                   e.Graphics.DrawLine(DarkPen, this.OffsetX + StartBlock, this.OffsetY + y, this.OffsetX + x, this.OffsetY + y);
                }
                if (PreviousValue && !CurrentValue)
                {   // Mark the start of the line.
                    StartBlock = x;
                }
                PreviousValue = CurrentValue;
            }
        }
    }
}

Which gets called by:

cComponentWindow Window = new cComponentWindow(0, 27, 790, 395, "Text to be dynamically displayed", global::G458.Properties.Resources.Test_Picture);
Window.Highlight(10, 10, 250, 150);
Window.Highlight(500, 300, 100, 50);
Window.ShowDialog();

In the first line of the Draw() function there is a fill commented out. This FillRectangle() command generates a similar artifact as the darken artifact. If you are trying it for yourself I suggest using that line of code once.

What I’ve tried doing:

  • Moving / resizing the darken area > Artifact changes shape but still exists.
  • Moving the PaintEvent to the window instead of the picture box. > Gave the exact same result.
  • Commenting out the PaintEvent on the window so only the PictureXox has the PaintEvent. > Gave the exact same result.

I don’t know how I could fix this and would appreciate any fixes or suggestions 🙂
If you require some more information, please ask. I’ll try to answer it as best as I can.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật