天天看点

Winform窗体图片的拖动,放大,缩小,复位,打印预览,鼠标滑轮的放大缩小

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.Threading;
using System.IO;
using System.Xml.Serialization;
using DevComponents.DotNetBar;
using System.Drawing.Printing;

namespace ThreadingTest
{
    public partial class PicView : Office2007Form
    {
        private Point pt;
        private const double step = 0.1;

        public PicView()
        {
            InitializeComponent();	   pictureBox2.MouseWheel += new MouseEventHandler(pictureBox2_MouseWheel);
            pbMain.Image = Image.FromFile(@"D:\界面布局.bmp");
            oldwith = pbMain.Width = pbMain.Image.Width;
            oldHeith = pbMain.Height = pbMain.Image.Height;
            oldPoint = pbMain.Location = new Point((panelContainer.Width - pbMain.Width) / 2, (panelContainer.Height - pbMain.Height) / 2);

        }
        /// <summary>
        /// 这个是封装dll的方法,直接改为类库,就可以调用了
        /// </summary>
        /// <param name="path"></param>
        /// <param name="name"></param>
        public void SetPath(string path, string name)
        {            pictureBox2.MouseWheel += new MouseEventHandler(pictureBox2_MouseWheel);
            this.Name = name;
            if (!File.Exists(path))
            {
                return;
            }
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            Image image = Image.FromStream(fs);
            fs.Close();
            fs.Dispose();
            pbMain.Image = image;
            oldwith = pbMain.Width = pbMain.Image.Width;
            oldHeith = pbMain.Height = pbMain.Image.Height;
            oldPoint = pbMain.Location = new Point((panelContainer.Width - pbMain.Width) / 2, (panelContainer.Height - pbMain.Height) / 2);
        }
        #region Event

        /// <summary>
        /// 放大
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEnlarge_Click(object sender, EventArgs e)
        {
            try
            {
                pbMain.Width = pbMain.Width + int.Parse(Math.Ceiling(pbMain.Width * step).ToString());
                pbMain.Height = pbMain.Height + int.Parse(Math.Ceiling(pbMain.Height * step).ToString());

                pbMain.Location = new Point((panelContainer.Width - pbMain.Width) / 2, (panelContainer.Height - pbMain.Height) / 2);
            }
            catch { }
        }
        /// <summary>
        /// 缩小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReduce_Click(object sender, EventArgs e)
        {
            pbMain.Width = pbMain.Width - int.Parse(Math.Ceiling(pbMain.Width * step).ToString());
            pbMain.Height = pbMain.Height - int.Parse(Math.Ceiling(pbMain.Height * step).ToString());

            pbMain.Location = new Point((panelContainer.Width - pbMain.Width) / 2, (panelContainer.Height - pbMain.Height) / 2);
        }

        private void pbMain_MouseDown(object sender, MouseEventArgs e)
        {
            pt = Cursor.Position;
            pbMain.Cursor = Cursors.SizeAll;
        }

        private void pbMain_MouseUp(object sender, MouseEventArgs e)
        {
            pbMain.Cursor = Cursors.Default;
        }

        private void pbMain_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int px = Cursor.Position.X - pt.X;
                int py = Cursor.Position.Y - pt.Y;
                pbMain.Location = new Point(pbMain.Location.X + px, pbMain.Location.Y + py);

                pt = Cursor.Position;
            }
        }
        /// <summary>
        /// 打印预览图片效果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        Image image = null;
        private void btnPrint_Click(object sender, EventArgs e)
        {
            image = new Bitmap(this.panelContainer.Bounds.Width, this.panelContainer.Bounds.Height);
            Graphics gps = Graphics.FromImage(image);
            gps.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            gps.CopyFromScreen(this.panelContainer.PointToScreen(new Point(0, 0)).X, this.panelContainer.PointToScreen(new Point(0, 0)).Y, 0, 0, new Size(this.panelContainer.Bounds.Width, this.panelContainer.Bounds.Height));
            ppd.Document = pdMain;
            System.Drawing.Printing.PageSettings df = new System.Drawing.Printing.PageSettings();
            df.PaperSize = new PaperSize("new size", image.Width, image.Height);
            pdMain.DefaultPageSettings = df;
            ppd.PrintPreviewControl.Zoom = 0.5;
            ppd.ShowDialog();
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "jpeg|*.jpg|bitmap|*.bmp|gif|*.gif|tiff|*.tif";
            saveFileDialog.FilterIndex = 1;
            saveFileDialog.RestoreDirectory = true;

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                pbMain.Image.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Tiff);
            }
        }
        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pdMain_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(image, new PointF(0,0));
        }

        #endregion
        private int oldwith = 0;
        private int oldHeith = 0;
        private Point oldPoint = new Point();
        /// <summary>
        /// 复位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            pbMain.Width = oldwith;
            pbMain.Height = oldHeith;

            pbMain.Location = oldPoint;

        }	     //鼠标滑轮放大缩小	      void pictureBox2_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                if (pictureBox2.Width < 12000 && pictureBox2.Height < 7000)
                {
                    pictureBox2.Width = pictureBox2.Width + int.Parse(Math.Ceiling(pictureBox2.Width * step).ToString());
                    pictureBox2.Height = pictureBox2.Height + int.Parse(Math.Ceiling(pictureBox2.Height * step).ToString());
                    pictureBox2.Location = new Point((panelEx2.Width - pictureBox2.Width) / 2, (panelEx2.Height - pictureBox2.Height) / 2);
                }
            }
            else
            {
                if (pictureBox2.Width > 70 && pictureBox2.Height > 40)
                {
                    pictureBox2.Width = pictureBox2.Width - int.Parse(Math.Ceiling(pictureBox2.Width * step).ToString());
                    pictureBox2.Height = pictureBox2.Height - int.Parse(Math.Ceiling(pictureBox2.Height * step).ToString());
                    pictureBox2.Location = new Point((panelEx2.Width - pictureBox2.Width) / 2, (panelEx2.Height - pictureBox2.Height) / 2);
                }
            }
        }
    }
}