C#图片查看器

来源:互联网 发布:开心农场源码 编辑:程序博客网 时间:2024/05/01 15:37

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Text;
    4. using System.Windows.Forms;
    5. using System.Drawing;
    6. using System.IO;
    7. using System.Drawing.Design;
    8. namespace 图片查看
    9. {
    10.      #region 展示大图
    11.      class ShowPicture
    12.      {      
    13.         #region 缩放图片尺寸
    14.         /// <summary>
    15.          /// 按原始比例缩放图片的高和宽,并且使高小于等于MAX_Y,宽小于等于MAX_X
    16.         /// </summary>
    17.         /// <param name="x">图片的宽</param>
    18.         /// <param name="y">图片的高</param>
    19.          /// <param name="MAX_X">PictureBox的高</param>
    20.         /// <param name="MAX_Y">PictureBox的宽</param>
    21.          /// <returns></returns>
    22.          private static Size Zoom(double x, double y,int MAX_X,int MAX_Y)
    23.         {
    24.             double SCALE = x / y;
    25.             if (x > MAX_X || y > MAX_Y) //修正原来的与运算符
    26.             {
    27.                 if (x > y)
    28.                 {
    29.                     for (x = MAX_X; ; )
    30.                     {
    31.                          y = x / SCALE;
    32.                          if (y <= MAX_Y)
    33.                              break;
    34.                          else
    35.                              x--;
    36.                      }
    37.                  }
    38.                  else
    39.                  {
    40.                      for (y = MAX_Y; ; )
    41.                      {
    42.                          x = SCALE * y;
    43.                          if (x <= MAX_X)
    44.                              break;
    45.                          else
    46.                              y--;
    47.                      }
    48.                  }
    49.              }
    50.              return new Size((int)x, (int)y);
    51.          }
    52.          #endregion
    53.         
    54.          /// <summary>
    55.          /// 在新窗体中展示大图
    56.          /// 2008-10-26修改
    57.          /// </summary>       
    58.          /// <param name="image">图片</param>
    59.          public static void Show(Image OriginalImage)
    60.          {
    61.              if (OriginalImage != null)
    62.              {
    63.                  Form showPicture = new Form();
    64.                  showPicture.BackColor = Color.Snow;
    65.                  showPicture.Text = "图片查看器";               
    66.                  showPicture.WindowState = FormWindowState.Maximized;
    67.                  PictureBox 图片_show_pictureBox = new PictureBox();
    68.                  
    69.                  图片_show_pictureBox.BackColor = Color.Black;
    70.                  图片_show_pictureBox.BorderStyle = BorderStyle.FixedSingle;
    71.                  图片_show_pictureBox.Size = Zoom(OriginalImage.Width, OriginalImage.Height,900,675);
    72.                  图片_show_pictureBox.Image = OriginalImage;
    73.                  图片_show_pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
    74.                  图片_show_pictureBox.Location = new Point((1024 - 图片_show_pictureBox.Size.Width )/ 2, (675 - 图片_show_pictureBox.Size.Height) / 2);
    75.                  showPicture.Controls.Add(图片_show_pictureBox);
    76.                  //图片_show_pictureBox.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;//自动随窗体大小变化
    77.                  showPicture.ShowDialog();
    78.                  GC.Collect();
    79.              }
    80.          }
    81.          #endregion
    82.      }
    83.      #endregion
    84.  }