C#图片查看器

来源:互联网 发布:淘宝新品标签多久显示 编辑:程序博客网 时间:2024/05/01 20:27
  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. #region 展示大图 
  10. class ShowPicture 
  11. #region 缩放图片尺寸 
  12. /// <summary> 
  13. /// 按原始比例缩放图片的高和宽,并且使高小于等于MAX_Y,宽小于等于MAX_X 
  14. /// </summary> 
  15. /// <param name="x">图片的宽</param> 
  16. /// <param name="y">图片的高</param> 
  17. /// <param name="MAX_X">PictureBox的高</param> 
  18. /// <param name="MAX_Y">PictureBox的宽</param> 
  19. /// <returns></returns> 
  20. private static Size Zoom(double x, double y,int MAX_X,int MAX_Y) 
  21. double SCALE = x / y; 
  22. if (x > MAX_X && y > MAX_Y) 
  23. if (x > y) 
  24. for (x = MAX_X; ; ) 
  25. y = x / SCALE; 
  26. if (y <= MAX_Y) 
  27. break
  28. else 
  29. x--; 
  30. else 
  31. for (y = MAX_Y; ; ) 
  32. x = SCALE * y; 
  33. if (x <= MAX_X) 
  34. break
  35. else 
  36. y--; 
  37. return new Size((int)x, (int)y); 
  38. #endregion 
  39. /// <summary> 
  40. /// 在新窗体中展示大图 
  41. /// 2008-10-26修改 
  42. /// </summary> 
  43. /// <param name="image">图片</param> 
  44. public static void Show(Image OriginalImage) 
  45. if (OriginalImage != null
  46. Form showPicture = new Form(); 
  47. showPicture.BackColor = Color.Snow; 
  48. showPicture.Text = "图片查看器"
  49. showPicture.WindowState = FormWindowState.Maximized; 
  50. PictureBox 图片_show_pictureBox = new PictureBox(); 
  51. 图片_show_pictureBox.BackColor = Color.Black; 
  52. 图片_show_pictureBox.BorderStyle = BorderStyle.FixedSingle; 
  53. 图片_show_pictureBox.Size = Zoom(OriginalImage.Width, OriginalImage.Height,900,675); 
  54. 图片_show_pictureBox.Image = OriginalImage; 
  55. 图片_show_pictureBox.SizeMode = PictureBoxSizeMode.StretchImage; 
  56. 图片_show_pictureBox.Location = new Point((1024 - 图片_show_pictureBox.Size.Width )/ 2, (675 - 图片_show_pictureBox.Size.Height) / 2); 
  57. showPicture.Controls.Add(图片_show_pictureBox); 
  58. //图片_show_pictureBox.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;//自动随窗体大小变化 
  59. showPicture.ShowDialog(); 
  60. GC.Collect(); 
  61. #endregion 
  62. #endregion 
  63. }
原创粉丝点击