Click here to Skip to main content
15,896,726 members
Articles / Programming Languages / C#

Fast Image Rotation For .NET Compact Framework

Rate me:
Please Sign up or sign in to vote.
4.86/5 (23 votes)
26 Nov 2007CPOL7 min read 117.9K   1.4K   35  
An article describing how to do fast image rotation on the .NET Compact Framework
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Bornander.UI.Imaging.Test
{
    public partial class ImagePanel : Panel
    {
        public ImagePanel()
        {
            InitializeComponent();
        }

        private void LayoutPictureBox()
        {
            SuspendLayout();
            if (pictureBox.Image != null)
            {
                Image image = pictureBox.Image;
                float aspectRatio = (float)image.Width / (float)image.Height;

                int wa = this.Width;
                int ha = (int)(wa / aspectRatio);
                if (ha > this.Height)
                {
                    pictureBox.Size = new Size(
                        (int)(this.Height * aspectRatio), 
                        this.Height);
                }
                else
                {
                    pictureBox.Size = new Size(wa, ha);
                }

                pictureBox.Location = new Point(
                    (this.Width - pictureBox.Width) / 2,
                    (this.Height - pictureBox.Height) / 2);

                pictureBox.Visible = true;
            }
            else
            {
                pictureBox.Visible = false;
            }
            ResumeLayout();
        }

        protected override void OnResize(EventArgs eventargs)
        {
            base.OnResize(eventargs);
            LayoutPictureBox();
        }

        public Image Image
        {
            get { return pictureBox.Image; }
            set 
            { 
                pictureBox.Image = value;
                LayoutPictureBox();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Sweden Sweden
Article videos
Oakmead Apps Android Games

21 Feb 2014: Best VB.NET Article of January 2014 - Second Prize
18 Oct 2013: Best VB.NET article of September 2013
23 Jun 2012: Best C++ article of May 2012
20 Apr 2012: Best VB.NET article of March 2012
22 Feb 2010: Best overall article of January 2010
22 Feb 2010: Best C# article of January 2010

Comments and Discussions