1
Answer

rotate image - windows forms

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
    {
        /// <summary>
        /// Erforderliche Designervariable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        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;
    }
}


Answers (1)