Click here to Skip to main content
15,895,740 members
Articles / Multimedia / GDI+

Image Magnifier Control

Rate me:
Please Sign up or sign in to vote.
4.45/5 (5 votes)
11 Oct 2006CPOL2 min read 58.4K   2.4K   22  
ImageMagnifier - a simple control for image zooming.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ImageMagnifier 
{
    public partial class ImageMagnifier : Control
    {
        public int[,] pixels;

        private Image imageToMagnify;

        public Image ImageToMagnify
        {
            get { return imageToMagnify; }
            set 
            {
                if (value != null)
                {
                    imageToMagnify = value;
                    Bitmap b = new Bitmap(this.ImageToMagnify);

                    pixels = new int[this.ImageToMagnify.Width, this.ImageToMagnify.Height];

                    for (int i = 0; i < this.ImageToMagnify.Width; i++)
                        for (int j = 0; j < this.ImageToMagnify.Height; j++)
                        {
                            pixels[i, j] = b.GetPixel(i, j).ToArgb();
                        }
                    this.Invalidate();
                }
            }
        }
        public const int MaxMagnificationCoefficient=64;
        public const int MinMagnificationCoefficient=1;

        private int magnificationCoefficient=1;

        public int MagnificationCoefficient
        {
            get { return magnificationCoefficient; }
            set 
            {
                if (value < MinMagnificationCoefficient)
                    magnificationCoefficient = MinMagnificationCoefficient;
                else
                    magnificationCoefficient = value > MaxMagnificationCoefficient ? MaxMagnificationCoefficient : value;
                this.Invalidate();
            }
        }

        public ImageMagnifier()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
        }

        public ImageMagnifier(Image ImageToMagnify)
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            this.ImageToMagnify = ImageToMagnify;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            if (imageToMagnify != null)
            {
                this.Width = imageToMagnify.Width * magnificationCoefficient;
                this.Height = imageToMagnify.Height * magnificationCoefficient;

                int currentTop = 0;
                int currentLeft = 0;
                for (int i = 0; i < this.ImageToMagnify.Width; i++)
                {
                    currentTop = i * magnificationCoefficient;
                    for (int j = 0; j < this.ImageToMagnify.Height; j++)
                    {
                        currentLeft = j * magnificationCoefficient;
                        Brush b = new SolidBrush(Color.FromArgb(pixels[i, j]));
                        pe.Graphics.FillRectangle(b, currentTop, currentLeft, magnificationCoefficient, magnificationCoefficient);
                    }
                }
            }
        }
    }
}

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)
United Kingdom United Kingdom
Work: HSBC (http://www.hsbc.co.uk/).
Regalia: PhD in CS, MCAD, MCPD: Web Developer, MCTS: .Net Framework 2.0., 3.5.
Interests: Programming, artificial intelligence, C#, .NET, HTML5, ASP.NET, SQL, LINQ.
Marital Status: Married, daughter
Blog: http://www.magomedov.co.uk

Comments and Discussions