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.