/// <summary> /// fileNameで指定されるファイルを読み出し、bufferの指定位置に嵌め込む。 /// bufferがnullの場合にはアロケートして返す。 /// </summary> /// <param name="fileName">ファイル名</param> /// <param name="buffer">バッファ(null指定時はアロケートして返す)</param> /// <param name="x">X位置</param> /// <param name="y">Y位置</param> /// <returns>バッファが返る。</returns> private ImageBuffer CombineImage(string fileName, ImageBuffer buffer, int x, int y) { using (Image image = ReadImage(fileName)) { ImageBuffer srcImage = ImageBuffer.CreateFrom(image); if (buffer == null) { int width = srcImage.Width * setting.HorizontalCount; int height = srcImage.Height * setting.VerticalCount; buffer = ImageBuffer.Create(width, height); } int regionWidth = buffer.Width / setting.HorizontalCount; int regionHeight = buffer.Height / setting.VerticalCount; int dstXOffs = regionWidth * x; int dstYOffs = regionHeight * y; int srcXOffs = (srcImage.Width - regionWidth) / 2; int srcYOffs = (srcImage.Height - regionHeight) / 2; buffer.WriteImage(srcImage, srcXOffs, srcYOffs, dstXOffs, dstYOffs, regionWidth, regionHeight); } return(buffer); }
/// <summary> /// 処理する。 /// </summary> /// <param name="filePaths">ファイルパス</param> public void Process(string[] filePaths) { foreach (string filePath in filePaths) { ImageBuffer srcBuffer; ImageFormat imageFormat; // データを読み出す。 // usingブロックをすぐに閉じるのは、 // こうしないとファイルをオープンしっぱなしになるため。 using (Image srcImage = ReadImage(filePath)) { srcBuffer = ImageBuffer.CreateFrom(srcImage); imageFormat = srcImage.RawFormat; } ImageBuffer dstBuffer = Process(srcBuffer); using (Image dstImage = dstBuffer.GetImage()) { string fileName = System.IO.Path.GetFileName(filePath); string dir = GetOutputDirectory(); if (string.IsNullOrEmpty(dir)) { dir = System.IO.Directory.GetCurrentDirectory(); } string dstPath = System.IO.Path.Combine(dir, fileName); dstImage.Save(dstPath, imageFormat); } } }
/// <summary> /// 画像を読み込む。 /// </summary> /// <param name="paths">パス</param> /// <returns>読み込んだ画像の配列</returns> private static ImageBuffer[] LoadImages(string[] paths) { List <ImageBuffer> images = new List <ImageBuffer>(); foreach (string path in paths) { using (Image image = ReadImage(path)) { images.Add(ImageBuffer.CreateFrom(image)); } } return(images.ToArray()); }
/// <summary> /// エクスポート処理 /// </summary> private void ExportProc() { var lastFileName = Properties.Settings.Default.LastExportPath; if (System.IO.File.Exists(lastFileName)) { saveFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(lastFileName); saveFileDialog.FileName = System.IO.Path.GetDirectoryName(lastFileName); } saveFileDialog.Filter = Properties.Resources.FILEFILTER_IMAGE; saveFileDialog.FilterIndex = 0; if (saveFileDialog.ShowDialog(this) != DialogResult.OK) { return; } Properties.Settings.Default.LastExportPath = saveFileDialog.FileName; var exportFileName = saveFileDialog.FileName; // サクッと生成する。 ImageBuffer imageBuffer = ImageBuffer.Create(faceImageEntrySet.ExportWidth, faceImageEntrySet.ExportHeight); for (int i = 0; i < faceImageEntrySet.EntryCount; i++) { var entry = faceImageEntrySet.GetEntry(i); if (!string.IsNullOrEmpty(entry.FileName)) { using (var srcImage = Image.FromFile(entry.FileName)) { var writeImage = ImageBuffer.CreateFrom(srcImage); int dstXOffs = entry.Width * (i % faceImageEntrySet.HorizontalEntryCount); int dstYOffs = entry.Height * (i / faceImageEntrySet.HorizontalEntryCount); imageBuffer.WriteImage(writeImage, entry.X, entry.Y, dstXOffs, dstYOffs, entry.Width, entry.Height); } } } using (var image = imageBuffer.GetImage()) { image.Save(exportFileName); } }
/// <summary> /// 処理する。 /// </summary> /// <param name="paths">ファイルパス</param> public void Process(string[] paths) { foreach (string filePath in paths) { using (Image srcImage = ReadImage(filePath)) { ImageBuffer srcBuffer = ImageBuffer.CreateFrom(srcImage); ImageBuffer dstBuffer = SequentialProcess(srcBuffer); using (Image dstImage = dstBuffer.GetImage()) { string fileName = System.IO.Path.GetFileName(filePath); string dir = setting.OutputDirectory; if (string.IsNullOrEmpty(dir)) { dir = System.IO.Directory.GetCurrentDirectory(); } string dstPath = System.IO.Path.Combine(dir, fileName); dstImage.Save(dstPath, srcImage.RawFormat); } } } }