Faisal Ansari

Faisal Ansari

  • NA
  • 382
  • 599.3k

Panning Image ???

Sep 3 2012 11:50 PM
I have a PictureBox control which i am genrating on runtime which can zoom in using MouseWheel event. Now I want to add a panning feature to it. I mean when PictureBox is in zoomed state, if user left clicks and holds the click then move the mouse, the image would pan within the picturebox.

Here is my 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.IO;
using System.Data.SqlClient;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Drawing.Imaging;

namespace WorkOnImage
{
    public partial class ImageForm : Form
    {
        public System.Drawing.Image newImage { get; set; }
        public System.Drawing.Image imagePrint { get; set; }
        public string imagePath { get; set; }
        public string formName { get; set; }
        
        MyPictureBox obj = new MyPictureBox();

        public ImageForm()
        {
            InitializeComponent();
        }

        private void ImageForm_Load(object sender, EventArgs e)
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(imagePath);
            this.imagePrint = img;
            this.Left = 0;
            this.Top = 0;
            int w = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width-24;
            int h = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Height-80;
            this.Size = new Size(w, h);
            obj.Size = this.Size;
            obj.SizeMode = PictureBoxSizeMode.Zoom;
            obj.Image = img;
            obj.Top = 40;
            this.Controls.Add(obj);
            this.ActiveControl = obj;
            this.WindowState = FormWindowState.Maximized;
            this.Text = formName.ToString();
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            ShowImages frmShowImg = new ShowImages();
            frmShowImg.Show();
            this.Hide();
        }

        private void btnRotate_Click(object sender, EventArgs e)
        {
            try
            {
                this.Controls.Add(obj);
                this.ActiveControl = obj;
                System.Drawing.Image img = obj.Image;
                img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                obj.Image = img;
            }
            catch
            {
                MessageBox.Show("Invalid Statements are running !");
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            PrintDocument doc = new PrintDocument();
            doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
            PrintDialog dlgSettings = new PrintDialog();
            dlgSettings.Document = doc;
            if (dlgSettings.ShowDialog() == DialogResult.OK)
            {
                doc.Print();
            }
        }

        void doc_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(imagePrint, e.Graphics.VisibleClipBounds.Location.X,
                e.Graphics.VisibleClipBounds.Y, e.Graphics.VisibleClipBounds.Size.Width,
                e.Graphics.VisibleClipBounds.Size.Height);
        }

        private void btnAntiClock_Click(object sender, EventArgs e)
        {
            try
            {
                this.Controls.Add(obj);
                this.ActiveControl = obj;
                System.Drawing.Image img = obj.Image;
                img.RotateFlip(RotateFlipType.Rotate270FlipNone);
                obj.Image = img;
            }
            catch
            {
                MessageBox.Show("Invalid Statements are running !");
            }
        }

        private void btnRotate180_Click(object sender, EventArgs e)
        {
            try
            {
                this.Controls.Add(obj);
                this.ActiveControl = obj;
                System.Drawing.Image img = obj.Image;
                img.RotateFlip(RotateFlipType.Rotate180FlipNone);
                obj.Image = img;
            }
            catch
            {
                MessageBox.Show("Invalid Statements are running !");
            }
        }

        private void btnFitDisplay_Click(object sender, EventArgs e)
        {
            obj.SizeMode = PictureBoxSizeMode.StretchImage;
            obj.Refresh();
        }
    }

    public class MyPictureBox : PictureBox
    {
        private int zoomLevel = 1;
        private Point zoomPoint;

        public MyPictureBox()
        {
            this.MouseWheel += new MouseEventHandler(MyPictureBox_MouseWheel);
        }

        void MyPictureBox_MouseWheel(object sender, MouseEventArgs e)
        {
            if (this.zoomLevel == 1)
            {
                this.zoomPoint = new Point(e.X, e.Y);
            }
            if (e.Delta > 0)
            {
                if (zoomLevel < 20)
                {
                    zoomLevel += 1;
                }
            }
            else
            {
                if (e.Delta < 1)
                {
                    if (zoomLevel > 1)
                    {
                        zoomLevel -= 1;
                    }
                }
            }
            this.Invalidate();
        }

        new public System.Drawing.Image Image
        {
            get
            {
                return base.Image;
            }
            set
            {
                zoomLevel = 1;
                base.Image = value;
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
           // base.OnPaint(pe);

             if (this.Image != null)
             {
                 Point loc;
                 Size sz;

                 if (zoomLevel != 1)
                 {
                     sz = new Size(this.Image.Width / zoomLevel, this.Image.Height / zoomLevel);

                     loc = new Point((int)(this.Image.Width * (zoomPoint.X / (double)this.ClientRectangle.Width)) - sz.Width / 2,
                         (int)(this.Image.Height * (zoomPoint.Y / (double)this.ClientRectangle.Height)) - sz.Height / 2);
                 }
                 else
                 {
                     loc = new Point(0, 0);
                     sz = this.Image.Size;
                 }
                 System.Drawing.Rectangle rectSrc = new System.Drawing.Rectangle(loc, sz);
                 pe.Graphics.DrawImage(this.Image, this.ClientRectangle, rectSrc, GraphicsUnit.Pixel);
             }
        }
    }
}
    

Reply Soon its urgent


Thanks.

Answers (2)