Im trying to rotate an image but it dosent work good enough:

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chocolade
    New Member
    • Aug 2010
    • 69

    Im trying to rotate an image but it dosent work good enough:

    Im using pictureBox1 and pictureBox2 and a button1
    Im doing image cropped and then i can drag the image around the form with the pictureBox2.

    Now i did that when i click on the button1 in the button1_Click event it will rotate the image by 25 degrees.

    The problem is i want to rotate the original cropped Image and not a new one so this is what i did in the rotation function:

    Code:
    private  Bitmap RotateImageByAngle(Bitmap oldBitmap, float angle)
            {
    
               // var newBitmap = new Bitmap(oldBitmap.Width, oldBitmap.Height);
                var graphics = Graphics.FromImage(oldBitmap);
                graphics.TranslateTransform((float)oldBitmap.Width / 2, (float)oldBitmap.Height / 2);
                graphics.RotateTransform(angle);
                graphics.TranslateTransform(-(float)oldBitmap.Width / 2, -(float)oldBitmap.Height / 2);
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphics.DrawImage(oldBitmap,0,0);
                graphics.ResetTransform();
                return oldBitmap;
            }
    If im using the newBitmap its working but then the new rotated image lose quality and also its rotating inside the pictureBox2 but there is white spaces between the image borders and the pictureBox2 from inside.

    So to avoid quality lost i want to rotate somehow the original image ( oldBitmap ).
    Now as im doing it the oldBitmap rotating but its keep drawing on it self over and over again.

    This is the rotation function when using the newBitmap:

    Code:
     private  Bitmap RotateImageByAngle(Bitmap oldBitmap, float angle)
            {
    
                var newBitmap = new Bitmap(oldBitmap.Width, oldBitmap.Height);
                var graphics = Graphics.FromImage(newBitmap);
                graphics.TranslateTransform((float)oldBitmap.Width / 2, (float)oldBitmap.Height / 2);
                graphics.RotateTransform(angle);
                graphics.TranslateTransform(-(float)oldBitmap.Width / 2, -(float)oldBitmap.Height / 2);
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphics.DrawImage(oldBitmap,0,0);
                graphics.ResetTransform();
                return newBitmap;
            }

    And this is the complete code including the cropped image.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    
    
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            Random _rnd = new Random();
            int g = 0;
            int _x = 0;
            int _y = 0;
            int _x2 = 0;
            int _y2 = 0;
            Bitmap _croppedImage = null;
            private Point _curLoc = new Point(0, 0);
    
    
            public Form1()
            {
                InitializeComponent();
                pictureBox1.Image = new Bitmap(@"d:\2011-05-18 08.34.32.jpg");
                pictureBox2.Image = new Bitmap(@"d:\2011-05-18 08.34.32.jpg");
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
               // GenerateBitmap();
                
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
               // GenerateBitmap();
                g = g + 25;
               pictureBox2.Image =  RotateImageByAngle(_croppedImage, g);
            }
    
            private void GenerateBitmap()
            {
                Bitmap bmp = new Bitmap(this.pictureBox1.ClientSize.Width, this.pictureBox1.ClientSize.Height);
    
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.Clear(Color.Green);
    
                    for (int i = 0; i < 10; i++)
                    {
                        Rectangle r = new Rectangle(_rnd.Next(this.pictureBox1.ClientSize.Width),
                          _rnd.Next(this.pictureBox1.ClientSize.Height),
                          _rnd.Next(this.pictureBox1.ClientSize.Width / 2),
                          _rnd.Next(this.pictureBox1.ClientSize.Height / 2));
    
                        if ((i & 0x01) == 1)
                        {
                            g.FillEllipse(Brushes.Aqua, r);
                        }
                        else
                        {
                            g.FillRectangle(Brushes.DarkRed, r);
                        }
                    }
                }
    
                if (this.pictureBox1.Image != null)
                    this.pictureBox1.Image.Dispose();
    
                this.pictureBox1.Image = bmp;
            }
    
            private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    _x = e.X;
                    _y = e.Y;
                }
                else if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    //if we are inside the drawn rectangle
                    if (new Rectangle(e.X, e.Y, 1, 1).IntersectsWith(new Rectangle(_x, _y, _x2 - _x, _y2 - _y)))
                    {
                        //get the image
                        if (_x > -1 && _y > -1 && _x2 < this.pictureBox1.ClientSize.Width && _y2 < this.pictureBox1.ClientSize.Height)
                            CropImage();
    
                        //setup the Control
                        pictureBox2.ClientSize = new Size(_croppedImage.Width, _croppedImage.Height);
                        pictureBox2.BorderStyle = BorderStyle.FixedSingle;
                        pictureBox2.Image = _croppedImage;
                        pictureBox2.BringToFront();
                        //set initial offset
                        _curLoc = new Point(-24, -24);
                        pictureBox2.Location = new Point(e.X, e.Y);
                        //capture the mouse
                        pictureBox2.Capture = true;
                    }
                }
    
            }
    
            private void CropImage()
            {
                if (pictureBox1.Image != null)
                {
                    if (_croppedImage != null)
                        _croppedImage.Dispose();
    
                    _croppedImage = ((Bitmap)this.pictureBox1.Image).Clone(
                      new Rectangle(_x, _y, _x2 - _x, _y2 - _y),
                      this.pictureBox1.Image.PixelFormat);
                }
            }
    
            private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    _x2 = e.X;
                    _y2 = e.Y;
                    this.pictureBox1.Invalidate();
                }
    
            }
    
            private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
            {
                //release the mouse from pictureBox2
                this.pictureBox2.Capture = false;
    
            }
    
            private void pictureBox1_Paint(object sender, PaintEventArgs e)
            {
                if (_x > -1 && _y > -1 && _x2 < this.pictureBox1.ClientSize.Width && _y2 < this.pictureBox1.ClientSize.Height)
                {
                    e.Graphics.DrawRectangle(Pens.Blue, new Rectangle(_x, _y, _x2 - _x, _y2 - _y));
                }
    
            }
    
            private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
            {
                //set initial values for re-dragging
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                    _curLoc = new Point(-e.X, -e.Y);
    
            }
    
            private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    //move the Control
                    Point mousePos = this.pictureBox1.PointToClient(Control.MousePosition);
                    mousePos.Offset(_curLoc.X, _curLoc.Y);
                    ((PictureBox)sender).Location = mousePos;
                }
    
            }
    
            private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
            {
                //release the mouse from pictureBox2
                this.pictureBox2.Capture = false;
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (_croppedImage != null)
                    _croppedImage.Dispose();
    
                if (pictureBox1.Image != null)
                    pictureBox1.Image.Dispose();
    
            }
    
            
    
            private  Bitmap RotateImageByAngle(Bitmap oldBitmap, float angle)
            {
    
                var newBitmap = new Bitmap(oldBitmap.Width, oldBitmap.Height);
                var graphics = Graphics.FromImage(newBitmap);
                graphics.TranslateTransform((float)oldBitmap.Width / 2, (float)oldBitmap.Height / 2);
                graphics.RotateTransform(angle);
                graphics.TranslateTransform(-(float)oldBitmap.Width / 2, -(float)oldBitmap.Height / 2);
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphics.DrawImage(oldBitmap,0,0);
                graphics.ResetTransform();
                return newBitmap;
            }
    
    
    
    
    
        }
    }

    Please if you can show me how to do it according to this code i did.

    Thanks a lot.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Rotations in C# are confusing me. I learned matrix transforms in OpenGL and I feel like these are different. I tried experimenting with it but I can't figure it out... normally rotation works from the image origin so the general idea is to translate it so that the image center is the origin, rotate it, then translate it back, but the translations are... funny. I'd need to spend more time on this to understand it.

    That said, google has come to the rescue! :D I found the article you were obviously referencing, but I also found another...

    The home for technical questions and answers at Microsoft. Get started asking, answering, and browsing questions about products like .Net, Azure, or Teams.


    I tried this one and had a lot more success. The other benefit is that it rotates matrix and uses that to get the bounds of a rectangle, so your rectangle will always contain your rotated image. Kinda nice :)

    Comment

    Working...