在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
效果如图:
工具代码: 1 using System.Drawing; 2 using System.Drawing.Imaging; 3 4 class ImageCutterConfig 5 { 6 /// <summary> 7 /// MIME类型,如果用“image/jpeg”切割后的图片会模糊,目前还不知道为啥 8 /// </summary> 9 public const string mimeType = @"image/png"; //@"image/jpeg";// 10 } 11 12 /// <summary> 13 /// 图形切割配置 14 /// </summary> 15 public class GridData 16 { 17 /// <summary> 18 /// 原始图片的文件 19 /// </summary> 20 public string SrcFileName = @"D:\0_lugsProjects\MapCutter\SourceMap.jpg"; 21 /// <summary> 22 /// 切割后的图片输出目录 23 /// </summary> 24 public string OutDirectory = @"D:\0_lugsProjects\MapCutter\Res\"; 25 /// <summary> 26 /// 图片宽度 27 /// </summary> 28 public int ImageWidth = 3000; 29 /// <summary> 30 /// 图片高度 31 /// </summary> 32 public int ImageHeight = 1900; 33 /// <summary> 34 /// 格子宽度 35 /// </summary> 36 public int GridWidth = 256; 37 /// <summary> 38 /// 格子高度 39 /// </summary> 40 public int GridHeight = 256; 41 /// <summary> 42 /// 切片文件前缀 43 /// </summary> 44 public string titlePrefixName = "WorldMap_"; 45 /// <summary> 46 /// 切片文件扩展名 47 /// </summary> 48 public string expandedName = @".jpg"; 49 } 50 51 /// <summary> 52 /// 图片切割工具参数 53 /// </summary> 54 class ToolParam 55 { 56 public Image srcImage; 57 public GridData imageData; 58 public ImageCodecInfo codecInfo; 59 public EncoderParameters encoderParams; 60 public int columnNum; 61 public int rowNum; 62 63 public int columnIndex; 64 public int rowIndex; 65 66 public ToolParam(Image srcImage, GridData imageData, ImageCodecInfo codecInfo, EncoderParameters encoderParams, int columnNum, int rowNum) 67 { 68 this.srcImage = srcImage; 69 this.imageData = imageData; 70 this.codecInfo = codecInfo; 71 this.encoderParams = encoderParams; 72 this.columnNum = columnNum; 73 this.rowNum = rowNum; 74 RefreshIndex(0, 0); 75 } 76 77 public void RefreshIndex(int columnIndex, int rowIndex) 78 { 79 this.columnIndex = columnIndex; 80 this.rowIndex = rowIndex; 81 } 82 } 83 84 class ImageCutterTool 85 { 86 /// <summary> 87 /// 切割图片并保存 88 /// </summary> 89 /// <param name="data"></param> 90 public static void SaveSlice(ToolParam data) 91 { 92 int index = data.rowIndex * data.columnNum + data.columnIndex + 1; 93 int w = data.imageData.GridWidth; 94 int h = data.imageData.GridHeight; 95 int x = data.columnIndex * data.imageData.GridWidth; 96 int y = data.rowIndex * data.imageData.GridHeight; 97 if ((data.columnIndex + 1) * data.imageData.GridWidth > data.srcImage.Width) 98 { 99 w = data.srcImage.Width - data.columnIndex * data.imageData.GridWidth; 100 } 101 if ((data.rowIndex + 1) * data.imageData.GridHeight > data.srcImage.Height) 102 { 103 h = data.srcImage.Height - data.rowIndex * data.imageData.GridHeight; 104 } 105 106 Bitmap destBitMap = new Bitmap(w, h, PixelFormat.Format24bppRgb); 107 using (Graphics g = Graphics.FromImage(destBitMap)) 108 { 109 g.Clear(Color.Transparent); 110 g.DrawImage(data.srcImage, new Rectangle(0, 0, w, h), x, y, w, h, GraphicsUnit.Pixel); 111 string name = data.imageData.OutDirectory + data.imageData.titlePrefixName + index.ToString("D2") + data.imageData.expandedName; 112 destBitMap.Save(name, data.codecInfo, data.encoderParams); 113 } 114 } 115 116 /// <summary> 117 /// 获取图像编解码器 118 /// </summary> 119 /// <param name="mimeType">MIME类型</param> 120 /// <returns></returns> 121 public static ImageCodecInfo GetEncodeInfo(string mimeType) 122 { 123 ImageCodecInfo[] infos = ImageCodecInfo.GetImageEncoders(); 124 for (int i = 0, iMax = infos.Length; i < iMax; ++i) 125 { 126 ImageCodecInfo info = infos[i]; 127 if (mimeType == info.MimeType) 128 { 129 return info; 130 } 131 } 132 return null; 133 } 134 }
测试代码: 1 using System; 2 using System.Drawing; 3 using System.Drawing.Imaging; 4 using System.Windows; 5 6 class lgs 7 { 8 static void Main() 9 { 10 GridData data = new GridData(); 11 Image srcImage = Image.FromFile(data.SrcFileName); 12 int width = srcImage.Width; 13 int height = srcImage.Height; 14 //列数 15 int columnNum = (int)Math.Ceiling(width * 1.0f / data.GridWidth); 16 //行数 17 int rowNum = (int)Math.Ceiling(height * 1.0f / data.GridHeight); 18 19 ImageCodecInfo codecInfo = ImageCutterTool.GetEncodeInfo(ImageCutterConfig.mimeType); 20 EncoderParameters encoderParams = new EncoderParameters(2); 21 encoderParams.Param[0] = new EncoderParameter(Encoder.ColorDepth, 1L); 22 encoderParams.Param[1] = new EncoderParameter(Encoder.Quality, 1L); 23 24 ToolParam toolParam = new ToolParam(srcImage, data, codecInfo, encoderParams, columnNum, rowNum); 25 for (int i = 0; i < rowNum; ++i) 26 { 27 for (int j = 0; j < columnNum; ++j) 28 { 29 toolParam.RefreshIndex(j, i); 30 ImageCutterTool.SaveSlice(toolParam); 31 } 32 } 33 34 MessageBox.Show("图片切割完毕"); 35 } 36 } |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论