winform 圖片放大縮小

  1. using System.Collections.Generic;  
  2. using System.ComponentModel;  
  3. using System.Data;  
  4. using System.Drawing;  
  5. using System.Linq;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.Threading;  
  9. using System.IO;  
  10. using System.Xml.Serialization;  
  11. using DevComponents.DotNetBar;  
  12. using System.Drawing.Printing;  
  13.   
  14. namespace ThreadingTest  
  15. {  
  16.     public partial class PicView : Office2007Form  
  17.     {  
  18.         private Point pt;  
  19.         private const double step = 0.1;  
  20.   
  21.         public PicView()  
  22.         {  
  23.             InitializeComponent();     pictureBox2.MouseWheel += new MouseEventHandler(pictureBox2_MouseWheel);  
  24.             pbMain.Image = Image.FromFile(@"D:\界面佈局.bmp");  
  25.             oldwith = pbMain.Width = pbMain.Image.Width;  
  26.             oldHeith = pbMain.Height = pbMain.Image.Height;  
  27.             oldPoint = pbMain.Location = new Point((panelContainer.Width - pbMain.Width) / 2, (panelContainer.Height - pbMain.Height) / 2);  
  28.   
  29.         }  
  30.         /// <summary>  
  31.         /// 這個是封裝dll的方法,直接改爲類庫,就可以調用了  
  32.         /// </summary>
  33.         public void SetPath(string path, string name)  
  34.         {            pictureBox2.MouseWheel += new MouseEventHandler(pictureBox2_MouseWheel);  
  35.             this.Name = name;  
  36.             if (!File.Exists(path))  
  37.             {  
  38.                 return;  
  39.             }  
  40.             FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);  
  41.             Image image = Image.FromStream(fs);  
  42.             fs.Close();  
  43.             fs.Dispose();  
  44.             pbMain.Image = image;  
  45.             oldwith = pbMain.Width = pbMain.Image.Width;  
  46.             oldHeith = pbMain.Height = pbMain.Image.Height;  
  47.             oldPoint = pbMain.Location = new Point((panelContainer.Width - pbMain.Width) / 2, (panelContainer.Height - pbMain.Height) / 2);  
  48.         }  
  49.         #region Event  
  50.   
  51.         /// <summary>  
  52.         /// 放大  
  53.         /// </summary>  
  54.         private void btnEnlarge_Click(object sender, EventArgs e)  
  55.         {  
  56.             try  
  57.             {  
  58.                 pbMain.Width = pbMain.Width + int.Parse(Math.Ceiling(pbMain.Width * step).ToString());  
  59.                 pbMain.Height = pbMain.Height + int.Parse(Math.Ceiling(pbMain.Height * step).ToString());  
  60.   
  61.                 pbMain.Location = new Point((panelContainer.Width - pbMain.Width) / 2, (panelContainer.Height - pbMain.Height) / 2);  
  62.             }  
  63.             catch { }  
  64.         }  
  65.         /// <summary>  
  66.         /// 縮小  
  67.         /// </summary>  
  68.         private void btnReduce_Click(object sender, EventArgs e)  
  69.         {  
  70.             pbMain.Width = pbMain.Width - int.Parse(Math.Ceiling(pbMain.Width * step).ToString());  
  71.             pbMain.Height = pbMain.Height - int.Parse(Math.Ceiling(pbMain.Height * step).ToString());  
  72.   
  73.             pbMain.Location = new Point((panelContainer.Width - pbMain.Width) / 2, (panelContainer.Height - pbMain.Height) / 2);  
  74.         }  
  75.   
  76.         private void pbMain_MouseDown(object sender, MouseEventArgs e)  
  77.         {  
  78.             pt = Cursor.Position;  
  79.             pbMain.Cursor = Cursors.SizeAll;  
  80.         }  
  81.   
  82.         private void pbMain_MouseUp(object sender, MouseEventArgs e)  
  83.         {  
  84.             pbMain.Cursor = Cursors.Default;  
  85.         }  
  86.   
  87.         private void pbMain_MouseMove(object sender, MouseEventArgs e)  
  88.         {  
  89.             if (e.Button == MouseButtons.Left)  
  90.             {  
  91.                 int px = Cursor.Position.X - pt.X;  
  92.                 int py = Cursor.Position.Y - pt.Y;  
  93.                 pbMain.Location = new Point(pbMain.Location.X + px, pbMain.Location.Y + py);  
  94.   
  95.                 pt = Cursor.Position;  
  96.             }  
  97.         }  
  98.         /// <summary>  
  99.         /// 打印預覽圖片效果  
  100.         /// </summary>  
  101.         Image image = null;  
  102.         private void btnPrint_Click(object sender, EventArgs e)  
  103.         {  
  104.             image = new Bitmap(this.panelContainer.Bounds.Width, this.panelContainer.Bounds.Height);  
  105.             Graphics gps = Graphics.FromImage(image);  
  106.             gps.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;  
  107.             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));  
  108.             ppd.Document = pdMain;  
  109.             System.Drawing.Printing.PageSettings df = new System.Drawing.Printing.PageSettings();  
  110.             df.PaperSize = new PaperSize("new size", image.Width, image.Height);  
  111.             pdMain.DefaultPageSettings = df;  
  112.             ppd.PrintPreviewControl.Zoom = 0.5;  
  113.             ppd.ShowDialog();  
  114.         }  
  115.         /// <summary>  
  116.         /// 導出  
  117.         /// </summary> 
  118.         private void btnExport_Click(object sender, EventArgs e)  
  119.         {  
  120.             SaveFileDialog saveFileDialog = new SaveFileDialog();  
  121.             saveFileDialog.Filter = "jpeg|*.jpg|bitmap|*.bmp|gif|*.gif|tiff|*.tif";  
  122.             saveFileDialog.FilterIndex = 1;  
  123.             saveFileDialog.RestoreDirectory = true;  
  124.   
  125.             if (saveFileDialog.ShowDialog() == DialogResult.OK)  
  126.             {  
  127.                 pbMain.Image.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Tiff);  
  128.             }  
  129.         }  
  130.         /// <summary>  
  131.         /// 打印  
  132.         /// </summary>  
  133.         /// <param name="sender"></param>  
  134.         /// <param name="e"></param>  
  135.         private void pdMain_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)  
  136.         {  
  137.             e.Graphics.DrawImage(image, new PointF(0,0));  
  138.         }  
  139.  
  140.         #endregion  
  141.         private int oldwith = 0;  
  142.         private int oldHeith = 0;  
  143.         private Point oldPoint = new Point();  
  144.         /// <summary>  
  145.         /// 復位  
  146.         /// </summary>  
  147.         /// <param name="sender"></param>  
  148.         /// <param name="e"></param>  
  149.         private void button1_Click(object sender, EventArgs e)  
  150.         {  
  151.             pbMain.Width = oldwith;  
  152.             pbMain.Height = oldHeith;  
  153.   
  154.             pbMain.Location = oldPoint;  
  155.   
  156.         }

  157. //鼠標滑輪放大縮小
  158.         private void pictureBox2_MouseWheel(object sender, MouseEventArgs e)  
  159.         {  
  160.             if (e.Delta > 0)  
  161.             {  
  162.                 if (pictureBox2.Width < 12000 && pictureBox2.Height < 7000)  
  163.                 {  
  164.                     pictureBox2.Width = pictureBox2.Width + int.Parse(Math.Ceiling(pictureBox2.Width * step).ToString());  
  165.                     pictureBox2.Height = pictureBox2.Height + int.Parse(Math.Ceiling(pictureBox2.Height * step).ToString());  
  166.                     pictureBox2.Location = new Point((panelEx2.Width - pictureBox2.Width) / 2, (panelEx2.Height - pictureBox2.Height) / 2);  
  167.                 }  
  168.             }  
  169.             else  
  170.             {  
  171.                 if (pictureBox2.Width > 70 && pictureBox2.Height > 40)  
  172.                 {  
  173.                     pictureBox2.Width = pictureBox2.Width - int.Parse(Math.Ceiling(pictureBox2.Width * step).ToString());  
  174.                     pictureBox2.Height = pictureBox2.Height - int.Parse(Math.Ceiling(pictureBox2.Height * step).ToString());  
  175.                     pictureBox2.Location = new Point((panelEx2.Width - pictureBox2.Width) / 2, (panelEx2.Height - pictureBox2.Height) / 2);  
  176.                 }  
  177.             }  
  178.         }  
  179.     }  
  180. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章