static public void Split(string fileName, string outDir, int countPerLine) { if (!Directory.Exists(outDir)) { Directory.CreateDirectory(outDir); } var bmp = Bmp.FromFile(fileName); var width = bmp.bmih.biWidth; var height = Math.Abs(bmp.bmih.biHeight); // 縦横ピクセル数同じ限定 var sx = width / countPerLine; var sy = sx; var index = 0; for (var y = 0; y < height; y += sy) { for (var x = 0; x < width; x += sx) { var outfile = string.Empty; if (x + sx == width) { // 右端の画像はインデックス用 outfile = Path.Combine(outDir, index.ToString() + "-index.bmp"); } else { index++; outfile = Path.Combine(outDir, index.ToString() + ".bmp"); } var copiedBitmap = bmp.Copy(x, y, sx, sy); copiedBitmap.ToFile(outfile); Console.WriteLine("wrote {0} x={1} y={2}", outfile, x, y); } } }
static public void Mux(string[] fileNames, string outFile, int countPerLine) { List <Bmp> bmps = new List <Bmp>(); var lines = (fileNames.Length + countPerLine - 1) / countPerLine; var index = 0; for (var y = 0; y < lines; ++y) { for (var x = 0; x < countPerLine; ++x) { bmps.Add(Bmp.FromFile(fileNames[index])); index++; } } bool palettable = IsPalettable(bmps); List <RGBQUAD> colors = null; if (palettable) { // パレット統合 colors = CombinePalettes(bmps); if (colors.Count > 256) { palettable = false; } } if (palettable) { ushort biBitCount = 8; if (colors.Count <= 2) { biBitCount = 1; } if (colors.Count <= 16) { biBitCount = 4; } else { biBitCount = 8; } Bmp bmp = new Bmp(); bmp.bmih = bmps[0].bmih; var width = bmps[0].bmih.biWidth; var height = Math.Abs(bmps[0].bmih.biHeight); bmp.bmih.biWidth = width * countPerLine; bmp.bmih.biHeight = height * lines; bmp.bmih.biBitCount = biBitCount; if (bmp.bmih.biClrUsed == 0) { bmp.colorTable = new RGBQUAD[1 << biBitCount]; } else { bmp.bmih.biClrUsed = (uint)(colors.Count); bmp.colorTable = new RGBQUAD[bmp.bmih.biClrUsed]; } for (int i = 0; i < colors.Count; ++i) { bmp.colorTable[i] = colors[i]; } int lineStride = bmp.GetLineStride(); bmp.imageData = new byte[Math.Abs(lineStride * bmp.bmih.biHeight)]; index = 0; for (var y = 0; y < lines; ++y) { for (var x = 0; x < countPerLine; ++x) { bmp.Paste(x * width, y * height, bmps[index]); index++; } } bmp.ToFile(outFile); } else { throw new NotImplementedException(); } }