Home c# The rotation of the image in the PictureBox c #

The rotation of the image in the PictureBox c # [duplicate]

Author

Date

Category

There was a problem with the image rotation pictureBox. I have an image and I rotate it in the form (the angle of rotation is set using trackbara), I want this rotation is performed in pictureBox, but unfortunately all my attempts are futile, and the picture does not want to rotate, tell me how this rotation is done in the PictureBox, will very grateful

public Image img;
  public int RotationAngle;
  public Form1 ()
  {
    Initializecomponent ();
    this.DoubleBuffered = true; // remove flicker
  }
  private void Form1_Load (object sender, EventArgs e)
  {
    img = Image.FromFile (Application.StartupPath.ToString () + @ "\ image \ Rocket.png");
  }
  private void Form1_Paint (object sender, PaintEventArgs e)
  {
    Bitmap bitmap = new Bitmap (img, img.Width, img.Height); // create a new bitmap
    e.Graphics.TranslateTransform (bitmap.Width * 5, bitmap.Height + 200); // move to form
    e.Graphics.RotateTransform (Convert.ToInt32 (RotationAngle / 1.5)); // rotation angle
    e.Graphics.TranslateTransform (-bitmap.Width / 2, -bitmap.Height - 200); // Specify a point around which the rotation
    e.Graphics.DrawImage (bitmap, bitmap.Width / 6, bitmap.Height / 6); // projecting image
  }
  private void Form1_Resize (object sender, EventArgs e)
  {
    Invalidate ();
  }
  private void trackBar1_ValueChanged (object sender, EventArgs e)
  {
    RotationAngle = trackBar1.Value;
    Invalidate ();
  }

Answer 1

The code is as follows:

private void Form1_Load (object sender, EventArgs e)
  {
    img = Image.FromFile (Application.StartupPath.ToString () + @ "\ image \ Rocket.png");
   // pictureBox1.BackgroundImage = Image.FromFile (Application.StartupPath.ToString () + @ "\ image \ 1.gif");
    aTimer = new System.Timers.Timer (10);
    aTimer.Elapsed + = OnTimedEvent;
    aTimer.AutoReset = true;
    aTimer.Enabled = true;
  }
  public void OnTimedEvent (Object source, ElapsedEventArgs e)
  {
     Test ();
  }
  public void Test ()
  {
   Bitmap bitmap = new Bitmap (pictureBox1.Width, pictureBox1.Height); // create a new bitmap
   this.Invoke (new Action (() = & gt; bitmap.SetResolution (img.HorizontalResolution, img.VerticalResolution)));
    using (Graphics gfx = Graphics.FromImage (bitmap))
    {
      gfx.TranslateTransform (pictureBox1.Width / 2, pictureBox1.Height / 2 400); // move to picturebox
      gfx.RotateTransform (Convert.ToInt32 (RotationAngle / 1.5)); // rotation angle (Variable RotationAngle)
      gfx.TranslateTransform (-pictureBox1.Width / 2, -pictureBox1.Height / -600 2); // Specify a point around which the rotation
      gfx.DrawImage (img, pictureBox1.Width / 4, pictureBox1.Height / 4); // projecting image
    }
    pictureBox1.Image = bitmap;
  }


So if I do

bitmap.Dispose;

picturebox that looks like this

public void Test ()
  {
   Bitmap bitmap = new Bitmap (pictureBox1.Width, pictureBox1.Height); // create a new bitmap
   this.Invoke (new Action (() = & gt; bitmap.SetResolution (img.HorizontalResolution, img.VerticalResolution))); 
Using (Graphics GFX = Graphics.FromImage (Bitmap))
    {
      GFX.TranslateTransform (PictureBox1.Width / 2, PictureBox1.Height / 2 +400); // Move to PictureBox
      GFX.rotateTransform (Convert.TOINT32 (Rotationangle / 1.5)); // Rotation Angle (Rotationalgle Variable)
      gfx.translateTransform (-pictureBox1.width / 2, -pictureBox1.Height / 2 -600); // Set the point around which rotated
      GFX.DRAWIMAGE (img, picturebox1.width / 4, picturebox1.height / 4); // Project the image
    }
    pictureBox1.image = Bitmap;
    bitmap.dispose ();
  }

Answer 2

Here is another example code

Public Void Rotate (Object Sender, Painteventargs E)
  {
    // E.Graphics.clear (Color.teal);
      this.invoke (New Action (() = & GT; PictureBox1.Refresh ()));
      Bitmap Bitmap = New Bitmap (img, img.width, img.height); // Create a new bitmap
      e.graphics.translateTransform (Bitmap.width / 2, Bitmap.Height + 200); // Move to PictureBox
     E.Graphics.rotateTransform (Convert.TOINT32 (Rotationangle / 1.5)); // Rotation Angle (Rotationalgle Variable)
      E.Graphics.translateTransform (-bitmap.width / 2, -bitmap.height - 200); // Set the point around which rotated
      e.graphics.drawimage (Bitmap, Bitmap.Width / 6, Bitmap.Height / 6); // Project the image
     bitmap.dispose ();
  }

Answer 3

I already said 100 times about inheritance! Why didn’t you even try to inherit from class PictureBox ?

everything is done literally per minute:

Public Sealed Class RotatablePictureBox: PictureBox
{
  Private Float _angle;
  Public RotatablePictureBox ()
  {
    DoubleBuffered = True;
  }
  Public Float Rotationangle
  {
    get {Return _angle; }
    SET.
    {
      _Angle = Value;
      Invalidate ();
    }
  }
  PROTECTED OVERRIDE VOID ONPAINT (Painteventargs E)
  {
    If (Image is NULL)
    {
      Base.ONPAINT (E);
      Return;
    }
    E.Graphics.clear (Backcolor);
    E.Graphics.translateTransform (Width / 2.0F, Height / 2.0f + 400);
    e.graphics.rotatetransform (Rotationangle / 1.5f);
    e.graphics.translateTransform (-width / 2.0f, -Height / 2.0f - 600.0f);
    e.graphics.drawimage (Image, Width / 4, Height / 4);
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions