ImageSpliter.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //************************************************************************
  2. // https://github.com/yuzhengyang
  3. // author: yuzhengyang
  4. // date: 2017.9.1 - 2017.9.1
  5. // desc: 图片分块工具
  6. // Copyright (c) yuzhengyang. All rights reserved.
  7. //************************************************************************
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Drawing;
  11. using System.Linq;
  12. using System.Text;
  13. namespace Azylee.Core.IOUtils.ImageUtils
  14. {
  15. public class ImageSpliter
  16. {
  17. private int Width = 0;
  18. private int Height = 0;
  19. private Image Image;
  20. public ImageSpliter(string file, int width, int height)
  21. {
  22. Image = Image.FromFile(file);
  23. Width = width;
  24. Height = height;
  25. }
  26. public ImageSpliter(Image src, int width, int height)
  27. {
  28. Image = src;
  29. Width = width;
  30. Height = height;
  31. }
  32. public Bitmap Get(int x, int y)
  33. {
  34. if (Image != null && Image.Width >= x * Width && Image.Height >= y * Height)
  35. {
  36. //创建新图位图
  37. Bitmap bitmap = new Bitmap(Width, Height);
  38. //创建作图区域
  39. using (Graphics graphic = Graphics.FromImage(bitmap))
  40. {
  41. //截取原图相应区域写入作图区
  42. graphic.DrawImage(Image, 0, 0, new Rectangle((x - 1) * Width, (y - 1) * Height, Width, Height), GraphicsUnit.Point);
  43. }
  44. }
  45. return null;
  46. }
  47. public static Bitmap Get(Image img, int height, int width, int x, int y)
  48. {
  49. try
  50. {
  51. if (img != null && img.Width >= x * width && img.Height >= y * height)
  52. {
  53. //创建新图位图
  54. Bitmap rs = new Bitmap(width, height);
  55. //创建作图区域
  56. Graphics graphic = Graphics.FromImage(rs);
  57. //截取原图相应区域写入作图区
  58. graphic.DrawImage(img, 0, 0, new Rectangle((x - 1) * width, (y - 1) * height, width, height), GraphicsUnit.Pixel);
  59. graphic.Dispose();
  60. img.Dispose();
  61. return rs;
  62. }
  63. }
  64. catch { }
  65. return null;
  66. }
  67. }
  68. }