Main Menu

Rotation Functionality

Started by mohamed.salahaldi, November 26, 2014, 08:17:26 AM

Previous topic - Next topic

mohamed.salahaldi

Dear Cyotek Team,

Actually you are doing an impressive work with the controls :)

In addition to the cool and simple features currently implemented in the ImageBox, i wonder how i can implement the rotate functionality??

Best regards,

Richard Moss

Hello,

Thanks for the compliment! In regards to rotation, here's a rough and ready example which allows rotation. It probably needs more work - the destination viewport calculate doesn't take into consideration the new size due to rotation so it'll probably end up getting clipped. Should start as a good example to take further though!

using System;
using System.ComponentModel;
using System.Drawing;
using Cyotek.Windows.Forms;

namespace WindowsFormsApplication1
{
  internal class RotateImageBox : ImageBox
  {
    #region Instance Fields

    private float _angle;

    #endregion

    #region Events

    /// <summary>
    /// Occurs when the Angle property value changes
    /// </summary>
    [Category("Property Changed")]
    public event EventHandler AngleChanged;

    #endregion

    #region Overridden Methods

    protected override void DrawImage(Graphics g)
    {
      Rectangle viewport;
      float halfWidth;
      float halfHeight;

      // derived from https://social.msdn.microsoft.com/Forums/vstudio/en-US/9a2a65a4-74c2-4f8b-aa6d-ad04b39a9692/c-draw-a-rotated-image-at-its-center?forum=csharpgeneral

      viewport = this.GetImageViewPort();
      halfWidth = viewport.Width / 2f;
      halfHeight = viewport.Height / 2f;

      //translate center
      g.TranslateTransform(viewport.Left + halfWidth, viewport.Top + halfHeight);

      //rotate
      g.RotateTransform(this.Angle);

      //re-translate
      g.TranslateTransform(-halfWidth, -halfHeight);

      // draw the image
      g.DrawImage(this.Image, new Rectangle(Point.Empty, viewport.Size), this.GetSourceImageRegion(), GraphicsUnit.Pixel);
    }

    #endregion

    #region Public Properties

    [Category("Appearance")]
    [DefaultValue(0)]
    public virtual float Angle
    {
      get { return _angle; }
      set
      {
        if (this.Angle != value)
        {
          _angle = value;

          this.OnAngleChanged(EventArgs.Empty);
        }
      }
    }

    #endregion

    #region Protected Members

    /// <summary>
    /// Raises the <see cref="AngleChanged" /> event.
    /// </summary>
    /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
    protected virtual void OnAngleChanged(EventArgs e)
    {
      EventHandler handler;

      this.Invalidate();

      handler = this.AngleChanged;

      if (handler != null)
      {
        handler(this, e);
      }
    }

    #endregion
  }
}


Hope this helps!

Regards;
Richard Moss
Read "Before You Post" before posting (https://forums.cyotek.com/cyotek-webcopy/before-you-post/). Do not send me private messages. Do not expect instant replies.

All responses are hand crafted. No AI involved. Possibly no I either.

HammerIT

Hello Richard,
I am liking your ImageBox control and am investigating using it in a project of mine.
This question is now a little old and things have progressed so I am wondering if the system.drawing RotateFlip would work here.
I am not very good a C# yet and am just assuming that the cyotec image box uses the Picturebox as its base container. If so then the system.display rotateFlip method might be an easy one liner..
belgiquepharmacie.be/acheter-rivotril/
I require users to be able to rotate images 90-180-270 to be able to read scans that were not orientated correctly.

A second functionality I would need is thumbnail support for multipage tiff files but that would be for another thread.

I plan on going through your tutorials in the following week as time permits.

Regards,
Bernard

Richard Moss

Hello,

No, the ImageBox doesn't use PictureBox as a base although it does share some of the functionality.

While you could use the RotateFlip method of a Bitmap object, that means the original image is changed. In the above example, the use of transforms mean it's only how the image is drawn that's different, the image itself isn't changed. If you don't mind the image changing, then you could write your own code to rotate the images and assign them to a standard ImageBox instance.

In regards to thumbnails, I actually use the ImageBox in our Gif Animator software to create thumbnails. However, at least for that product I believe a dedicated component would be more useful and at some point I intend to write one; whenever I get time to revisit that product. But, you could certainly use the ImageBox to create thumbnails too.

As you mentioned tiff, do check out the article on the Cyotek Blog for displaying multi-page tiff files in an ImageBox control.

Regards;
Richard Moss
Read "Before You Post" before posting (https://forums.cyotek.com/cyotek-webcopy/before-you-post/). Do not send me private messages. Do not expect instant replies.

All responses are hand crafted. No AI involved. Possibly no I either.

HammerIT

Hello Richard,
Thanks for the quick response.
Yes, it was that blog that led me to your site. Very good article.
Everything here is great, especially for a newbie like me.

Regards,
HammerIT