Hello, I want to design an image rotating program shorter. Here is the code I use. What I want to do better I have written in the comments. Especially I want to minimize use of the following line in the Form1.cs:
itsme = new Bitmap(@"..\..\..\..\..\content\pics\Seifenblase.JPG");Thank you for your help.
Form1.Designer.cs
namespace DrawingBasicsImageRotating
{
partial class Form1
{
///
/// Erforderliche Designervariable.
///
private System.ComponentModel.IContainer components = null;
///
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
///
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.itsme = new System.Drawing.Bitmap(@"..\..\..\..\..\content\pics\Seifenblase.JPG");
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Location = new System.Drawing.Point(20, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(400, 300);
this.panel1.TabIndex = 0;
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(24, 16);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(358, 278);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.Image = itsme;
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabStop = false;
//I want the following line integrate to the button-control
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.FillRectangle1);
//
// button1
//
this.button1.Location = new System.Drawing.Point(476, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "rotate1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(476, 41);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "rotate2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(560, 340);
this.Controls.Add(this.panel1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Drawing.Bitmap itsme;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
Form1.cs
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace DrawingBasicsImageRotating
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.pictureBox1.Paint -= new System.Windows.Forms.PaintEventHandler(this.FillRectangle1);
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.FillRectangle2);
}
private void button2_Click(object sender, EventArgs e)
{
this.pictureBox1.Paint -= new System.Windows.Forms.PaintEventHandler(this.FillRectangle2);
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.FillRectangle3);
}
private void FillRectangle1(object sender, PaintEventArgs e)
{
offset = new Size(0, 0);
//I don’t want the following line because it’s a repetation
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
//I don’t want the following line because it’s a repetation
pictureBox1.Image = itsme;
}
private void FillRectangle2(object sender, PaintEventArgs e)
{
//I don’t want the following line because it’s a repetation
itsme = new Bitmap(@"..\..\..\..\..\content\pics\Seifenblase.JPG");
itsme.RotateFlip(RotateFlipType.Rotate90FlipX);
pictureBox1.Image = itsme;
}
private void FillRectangle3(object sender, PaintEventArgs e)
{
//I don’t want the following line because it’s a repetation
itsme = new Bitmap(@"..\..\..\..\..\content\pics\Seifenblase.JPG");
itsme.RotateFlip(RotateFlipType.RotateNoneFlipX);
pictureBox1.Image = itsme;
}
private Size offset;
}
}
Pankajkumar PatelPosted May 8, 2026, 5:58 AM
Hi Andreas Jung,
@"..\..\..\..\..\content\pics\Seifenblase.JPG" is kept at 3 places. Instead of that create one function GetBitmap() with defined logic and call it. This is the one simple way to minimize use of the mentioned line in the Form1.cs.
Form1.Designer.cs
Form1.cs
Glad to help! Please mark this answer as the accepted answer if it helped you out!
Andreas JungPosted May 7, 2026, 5:29 PM
when I remove the two last lines of:
in Form1.cs the image flickers and is unstable, so it's not good this way.