Add working Scaling (at least as a proof of concept)

This commit is contained in:
EinEtw4s 2022-11-30 22:31:50 +01:00
parent 4856adf3d7
commit 6caf74a154
3 changed files with 64 additions and 25 deletions

View file

@ -1,4 +1,6 @@
namespace Game_of_Life; using System.Media;
namespace Game_of_Life;
partial class MainForm1 partial class MainForm1
{ {
@ -29,33 +31,52 @@ partial class MainForm1
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.pictureBox1 = new PictureBoxWithInterpolationMode(); this.pictureBox1 = new Game_of_Life.PictureBoxWithInterpolationMode();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
// pictureBox1 // pictureBox1
// //
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
this.pictureBox1.Location = new System.Drawing.Point(0, 0); this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(800, 450); this.pictureBox1.Size = new System.Drawing.Size(800, 450);
this.pictureBox1.TabIndex = 0; this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false; this.pictureBox1.TabStop = false;
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel);
//
// label1
//
this.label1.AutoSize = true;
this.label1.BackColor = System.Drawing.Color.Transparent;
this.label1.ForeColor = System.Drawing.Color.AliceBlue;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(27, 15);
this.label1.TabIndex = 1;
this.label1.Text = "";
// //
// MainForm1 // MainForm1
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Black;
this.ClientSize = new System.Drawing.Size(800, 450); this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.label1);
this.Controls.Add(this.pictureBox1); this.Controls.Add(this.pictureBox1);
this.Name = "MainForm1"; this.Name = "MainForm1";
this.Text = "MainForm1"; this.Text = "MainForm1";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout();
} }
#endregion #endregion
private PictureBoxWithInterpolationMode pictureBox1; private PictureBoxWithInterpolationMode pictureBox1;
private Label label1;
} }

View file

@ -6,30 +6,13 @@ namespace Game_of_Life;
public partial class MainForm1 : Form public partial class MainForm1 : Form
{ {
private int scalingFactor = 10;
public MainForm1() public MainForm1()
{ {
Pixelmap pixelmap;
int scalingFactor = 10;
InitializeComponent(); InitializeComponent();
int height = pictureBox1.Size.Height; label1.Text = "Scaling Factor: " + scalingFactor.ToString() + $"\nDimensions: {pictureBox1.Size.Height.ToString()}x{pictureBox1.Size.Width.ToString()}";
int width = pictureBox1.Size.Width;
Graphics graphics = pictureBox1.CreateGraphics();
pixelmap = new Pixelmap(height, width, scalingFactor);
Random rand = new Random();
for (int i = 0; i < 20; i++)
{
pixelmap.setPixel(rand.Next(width), rand.Next(height), rand.Next(3));
}
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.InterpolationMode = InterpolationMode.NearestNeighbor;
pixelmap.paintOnGraphics(graphics);
/* this.Shown += (s, e) => /* this.Shown += (s, e) =>
{ {
@ -45,4 +28,39 @@ public partial class MainForm1 : Form
} }
};*/ };*/
} }
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Pixelmap pixelmap;
int height = pictureBox1.Size.Height;
int width = pictureBox1.Size.Width;
Graphics graphics = e.Graphics;
pixelmap = new Pixelmap(height, width, scalingFactor);
Random rand = new Random();
for (int i = 0; i < 20; i++)
{
pixelmap.setPixel(rand.Next(width / scalingFactor), rand.Next(height / scalingFactor), rand.Next(3));
}
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.InterpolationMode = InterpolationMode.NearestNeighbor;
pixelmap.paintOnGraphics(graphics);
// Thread.Sleep(1000);
}
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
scalingFactor += e.Delta/32;
if (scalingFactor < 1)
{
scalingFactor = 1;
}
label1.Text = "Scaling Factor: " + scalingFactor.ToString() + $"\nDimensions: {pictureBox1.Size.Height.ToString()}x{pictureBox1.Size.Width.ToString()}";
pictureBox1.Refresh();
}
} }

View file

@ -9,8 +9,8 @@ public class Pixelmap
public Pixelmap(int height, int width, int scalingFactor) public Pixelmap(int height, int width, int scalingFactor)
{ {
this.height = height; this.height = height / scalingFactor;
this.width = width; this.width = width / scalingFactor;
map = new Color[width, height]; map = new Color[width, height];
for (int w = 0; w < width; w++) for (int w = 0; w < width; w++)
{ {
@ -60,8 +60,8 @@ public class Pixelmap
Color pixelColor = map[w, h]; Color pixelColor = map[w, h];
if (!pixelColor.Equals(Color.Black)) if (!pixelColor.Equals(Color.Black))
{ {
Pen pixelBrush = new Pen(pixelColor); Brush pixelBrush = new SolidBrush(pixelColor);
g.DrawRectangle(pixelBrush, new Rectangle((w/scalingFactor), (h/scalingFactor), scalingFactor, scalingFactor)); g.FillRectangle(pixelBrush, new Rectangle((w*scalingFactor), (h * scalingFactor), scalingFactor, scalingFactor));
} }
} }
} }