/// <summary> /// /// </summary> /// <param name="compression"></param> public void Save(Stream fs, TiffCompressOption compression) { TiffBitmapEncoder encoder = new TiffBitmapEncoder { Compression = compression }; foreach (var bitmap in _bitmapFrames) { encoder.Frames.Add(bitmap); } encoder.Save(fs); }
public static void SaveToTiff(this BitmapSource image, string fileName, TiffCompressOption tiffCompressOption = TiffCompressOption.Default) { var ec = new TiffBitmapEncoder { Compression = tiffCompressOption }; ec.Frames.Add(BitmapFrame.Create(image)); using (var fileStream = new FileStream(fileName, FileMode.Create)) { ec.Save(fileStream); } }
private void SaveBitmapFrameAsTiffImage(BitmapFrame bitmapFrame, TiffCompressOption compressOption, string saveDirectory, string fileName) { TiffBitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Frames.Add(bitmapFrame); encoder.Compression = compressOption; string saveFilePath = Path.Combine(saveDirectory, fileName); using (FileStream fileStream = new FileStream(saveFilePath, FileMode.Create, FileAccess.Write)) { encoder.Save(fileStream); } }
private static BitmapEncoder CreateBitmapEncoder(bool convertToJpeg, TiffCompressOption compression) { if (convertToJpeg == true) { return(new JpegBitmapEncoder()); } else { TiffBitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Compression = compression; return(encoder); } }
/// <summary> /// TIFF形式画像を出力する /// </summary> /// <param name="outputImg"></param> /// <param name="fileName">ファイル名</param> /// <param name="compress">圧縮の種類</param> public static void SaveAsTiff(this Image outputImg, string fileName, TiffCompressOption compress) { // TiffEncoderを作成する TiffBitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Compression = compress; // ページに追加する string tempFileName = System.IO.Path.GetTempFileName(); outputImg.Save(tempFileName); BitmapFrame bmpFrame = BitmapFrame.Create(new Uri(tempFileName), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); encoder.Frames.Add(bmpFrame); using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { encoder.Save(fs); } encoder.Dispatcher.InvokeShutdown(); System.IO.File.Delete(tempFileName); }
/// <summary> /// TIFF形式画像を出力する /// </summary> /// <param name="outputImg"></param> /// <param name="fileName">ファイル名</param> /// <param name="compress">圧縮の種類</param> public static void SaveAsTiff2(this Image outputImg, string fileName, TiffCompressOption compress) { // TiffEncoderを作成する TiffBitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Compression = compress; // ページに追加する string tempFileName = System.IO.Path.GetTempFileName(); using (FileStream imgStream = new FileStream(tempFileName, FileMode.Create, FileAccess.ReadWrite)) { outputImg.Save(imgStream, ImageFormat.Bmp); } using (FileStream stream = new FileStream(tempFileName, FileMode.Open, FileAccess.Read)) { BitmapFrame bmpFrame = BitmapFrame.Create(stream); encoder.Frames.Add(bmpFrame); using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { encoder.Save(fs); } } }
private void saveImage(int imageWidth, int imageHeight, string fileNameRaw, byte[] image1, byte[] image2, byte[] image3, byte[] image4, byte[] OD) { //String fileNameRaw = fileName; //fileName.Replace(@"\Pictures\", @"\Pictures\RAW\"); String fileName; long ms = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; //Console.WriteLine(Convert.ToString(DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - ms) + "ms elapsed (1) ..."); #region saveOD // Saving OD : //if (showOD) //{ // Bitmap bmpOD = new Bitmap(imageWidth, imageHeight, PixelFormat.Format48bppRgb); // Rectangle dim = new Rectangle(0, 0, bmpOD.Width, bmpOD.Height); // try // { // BitmapData picdata = bmpOD.LockBits(dim, ImageLockMode.ReadWrite, bmpOD.PixelFormat); // Marshal.Copy(OD, 0, picdata.Scan0, OD.Length); // bmpOD.UnlockBits(picdata); // } // catch (Exception e) // { // MessageBox.Show("Error while trying to create optical density:/n" + e.ToString()); // return; // } // try // { // bmpOD.Save(fileNameRaw.Replace(".tif", "_OD.tif")); // } // catch // { // MessageBox.Show("Error while saving picture " + shotID); // } //} // Save raw images //if (showRaw) //{ // //} #endregion if (tiff) { //Save as TIFF try { int stride = (imageWidth * 16 + 7) / 8; //Stride = difference in array pos between two image rows: (ImageWidth * bpp + 7)/8 TiffCompressOption compr = TiffCompressOption.None; if (tiffCompress) { compr = TiffCompressOption.Zip; } fileName = fileNameRaw.Replace(".tif", "_atoms.tif"); if (saveThisImage && System.IO.File.Exists(fileName)) { Console.WriteLine("\n\n\n !! Warning !!\n\n Filename " + fileName + " already exists!"); while (System.IO.File.Exists(fileName)) { fileName = fileName + ".bck"; } Console.WriteLine(" Saving as " + fileName + "\n\n Likely the program is corrupted, please restart.\n\n\n"); } BitmapSource bmp1Source = BitmapSource.Create(imageWidth, imageHeight, 96, 96, System.Windows.Media.PixelFormats.Gray16, null, image1, stride); FileStream bmp1stream = new FileStream(fileName, FileMode.Create); TiffBitmapEncoder encoder1 = new TiffBitmapEncoder(); encoder1.Compression = compr; //TiffCompressOption.None; encoder1.Frames.Add(BitmapFrame.Create(bmp1Source)); encoder1.Save(bmp1stream); fileName = fileNameRaw.Replace(".tif", "_noatoms.tif"); if (saveThisImage && System.IO.File.Exists(fileName)) { Console.WriteLine("\n\n\n !! Warning !!\n\n Filename " + fileName + " already exists!"); while (System.IO.File.Exists(fileName)) { fileName = fileName + ".bck"; } Console.WriteLine(" Saving as " + fileName + "\n\n Likely the program is corrupted, please restart.\n\n\n"); } BitmapSource bmp2Source = BitmapSource.Create(imageWidth, imageHeight, 96, 96, System.Windows.Media.PixelFormats.Gray16, null, image2, stride); FileStream bmp2stream = new FileStream(fileName, FileMode.Create); TiffBitmapEncoder encoder2 = new TiffBitmapEncoder(); encoder2.Compression = compr; //TiffCompressOption.None; encoder2.Frames.Add(BitmapFrame.Create(bmp2Source)); encoder2.Save(bmp2stream); if (saveDark) { fileName = fileNameRaw.Replace(".tif", "_dark1.tif"); if (saveThisImage && System.IO.File.Exists(fileName)) { Console.WriteLine("\n\n\n !! Warning !!\n\n Filename " + fileName + " already exists!"); while (System.IO.File.Exists(fileName)) { fileName = fileName + ".bck"; } Console.WriteLine(" Saving as " + fileName + "\n\n Likely the program is corrupted, please restart.\n\n\n"); } BitmapSource bmp3Source = BitmapSource.Create(imageWidth, imageHeight, 96, 96, System.Windows.Media.PixelFormats.Gray16, null, image3, stride); FileStream bmp3stream = new FileStream(fileName, FileMode.Create); TiffBitmapEncoder encoder3 = new TiffBitmapEncoder(); encoder3.Compression = compr; //TiffCompressOption.None; encoder3.Frames.Add(BitmapFrame.Create(bmp3Source)); encoder3.Save(bmp3stream); fileName = fileNameRaw.Replace(".tif", "_dark2.tif"); if (saveThisImage && System.IO.File.Exists(fileName)) { Console.WriteLine("\n\n\n !! Warning !!\n\n Filename " + fileName + " already exists!"); while (System.IO.File.Exists(fileName)) { fileName = fileName + ".bck"; } Console.WriteLine(" Saving as " + fileName + "\n\n Likely the program is corrupted, please restart.\n\n\n"); } BitmapSource bmp4Source = BitmapSource.Create(imageWidth, imageHeight, 96, 96, System.Windows.Media.PixelFormats.Gray16, null, image4, stride); FileStream bmp4stream = new FileStream(fileName, FileMode.Create); TiffBitmapEncoder encoder4 = new TiffBitmapEncoder(); encoder4.Compression = compr; //TiffCompressOption.None; encoder4.Frames.Add(BitmapFrame.Create(bmp4Source)); encoder4.Save(bmp4stream); bmp3stream.Close(); bmp4stream.Close(); bmp3Source.Dispatcher.InvokeShutdown(); bmp4Source.Dispatcher.InvokeShutdown(); } bmp1stream.Close(); bmp2stream.Close(); bmp1Source.Dispatcher.InvokeShutdown(); bmp2Source.Dispatcher.InvokeShutdown(); } catch (Exception e) { Console.WriteLine("Error in saveImage while saving 48bitRGB TIFF"); System.IO.File.WriteAllText(Path.GetDirectoryName(Application.ExecutablePath) + @"\" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + "saving.txt", "TIFF: " + e.ToString()); } //Console.WriteLine(Convert.ToString(DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - ms) + "ms elapsed (3) ..."); } else if (png) { // Save as PNG try { int stride = (imageWidth * 16 + 7) / 8; //Stride = difference in array pos between two image rows: (ImageWidth * bpp + 7)/8 fileName = fileNameRaw.Replace(".png", "_atoms.png"); if (saveThisImage && System.IO.File.Exists(fileName)) { Console.WriteLine("\n\n\n !! Warning !!\n\n Filename " + fileName + " already exists!"); while (System.IO.File.Exists(fileName)) { fileName = fileName + ".bck"; } Console.WriteLine(" Saving as " + fileName + "\n\n Likely the program is corrupted, please restart.\n\n\n"); } BitmapSource bmp1Source = BitmapSource.Create(imageWidth, imageHeight, 96, 96, System.Windows.Media.PixelFormats.Gray16, null, image1, stride); FileStream bmp1stream = new FileStream(fileName, FileMode.Create); PngBitmapEncoder encoder1 = new PngBitmapEncoder(); encoder1.Frames.Add(BitmapFrame.Create(bmp1Source)); encoder1.Save(bmp1stream); fileName = fileNameRaw.Replace(".png", "_noatoms.png"); if (saveThisImage && System.IO.File.Exists(fileName)) { Console.WriteLine("\n\n\n !! Warning !!\n\n Filename " + fileName + " already exists!"); while (System.IO.File.Exists(fileName)) { fileName = fileName + ".bck"; } Console.WriteLine(" Saving as " + fileName + "\n\n Likely the program is corrupted, please restart.\n\n\n"); } BitmapSource bmp2Source = BitmapSource.Create(imageWidth, imageHeight, 96, 96, System.Windows.Media.PixelFormats.Gray16, null, image2, stride); FileStream bmp2stream = new FileStream(fileName, FileMode.Create); PngBitmapEncoder encoder2 = new PngBitmapEncoder(); encoder2.Frames.Add(BitmapFrame.Create(bmp2Source)); encoder2.Save(bmp2stream); if (saveDark) { fileName = fileNameRaw.Replace(".png", "_dark1.png"); if (saveThisImage && System.IO.File.Exists(fileName)) { Console.WriteLine("\n\n\n !! Warning !!\n\n Filename " + fileName + " already exists!"); while (System.IO.File.Exists(fileName)) { fileName = fileName + ".bck"; } Console.WriteLine(" Saving as " + fileName + "\n\n Likely the program is corrupted, please restart.\n\n\n"); } BitmapSource bmp3Source = BitmapSource.Create(imageWidth, imageHeight, 96, 96, System.Windows.Media.PixelFormats.Gray16, null, image3, stride); FileStream bmp3stream = new FileStream(fileName, FileMode.Create); PngBitmapEncoder encoder3 = new PngBitmapEncoder(); encoder3.Frames.Add(BitmapFrame.Create(bmp3Source)); encoder3.Save(bmp3stream); fileName = fileNameRaw.Replace(".png", "_dark2.png"); if (saveThisImage && System.IO.File.Exists(fileName)) { Console.WriteLine("\n\n\n !! Warning !!\n\n Filename " + fileName + " already exists!"); while (System.IO.File.Exists(fileName)) { fileName = fileName + ".bck"; } Console.WriteLine(" Saving as " + fileName + "\n\n Likely the program is corrupted, please restart.\n\n\n"); } BitmapSource bmp4Source = BitmapSource.Create(imageWidth, imageHeight, 96, 96, System.Windows.Media.PixelFormats.Gray16, null, image4, stride); FileStream bmp4stream = new FileStream(fileName, FileMode.Create); PngBitmapEncoder encoder4 = new PngBitmapEncoder(); encoder4.Frames.Add(BitmapFrame.Create(bmp4Source)); encoder4.Save(bmp4stream); bmp3stream.Close(); bmp4stream.Close(); bmp3Source.Dispatcher.InvokeShutdown(); bmp4Source.Dispatcher.InvokeShutdown(); } bmp1stream.Close(); bmp2stream.Close(); bmp1Source.Dispatcher.InvokeShutdown(); bmp2Source.Dispatcher.InvokeShutdown(); } catch (Exception e) { Console.WriteLine("Error in saveImage while saving 48bitRGB PNG"); System.IO.File.WriteAllText(Path.GetDirectoryName(Application.ExecutablePath) + @"\" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + "saving.txt", "PNG: " + e.ToString()); } //Console.WriteLine(Convert.ToString(DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - ms) + "ms elapsed (2) ..."); } Console.WriteLine("\r (Note: Saving done.)\n"); }
/// <summary>Creates a converted copy of the image (tiff formatted).</summary> public static byte[] ConvertTo_TiffByteArray(this BitmapSource input, TiffCompressOption compressOption = TiffCompressOption.None) { var encoder = new TiffBitmapEncoder {Compression = compressOption}; return input.ConvertTo_UsingEncoder(encoder); }
/// <summary> /// /// </summary> /// <param name="compression"></param> public void Save(TiffCompressOption compression) { using (FileStream fs = new FileStream(this.FileName, FileMode.Create)) this.Save(fs, compression); }
public ImageOptions(int jpegQualityLevel, TiffCompressOption tiffCompression) { JpegQualityLevel = jpegQualityLevel; TiffCompression = tiffCompression; }
public static void SaveTiff(BitmapSource source, string fileName, int dpi, TiffCompressOption compression) { if (source == null) throw new ArgumentNullException("source"); if (String.IsNullOrEmpty(fileName)) throw new ArgumentNullException("fileName"); //if (File.Exists(fileName)) File.Delete(fileName); BitmapMetadata metadata = GetBitmapMetadata("tiff"); using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { TiffBitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Compression = compression; encoder.Frames.Add(BitmapFrame.Create(source, null, metadata, null)); encoder.Save(fs); fs.Close(); } }
public static void SaveMergeTiff(string[] fileNames, string outputFileName, int dpi, TiffCompressOption compresion) { if (fileNames == null || fileNames.Count() == 0) return; if (String.IsNullOrEmpty(outputFileName)) new ArgumentNullException("outputFileName"); if (File.Exists(outputFileName)) File.Delete(outputFileName); using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.Write)) { TiffBitmapEncoder encoder = new TiffBitmapEncoder(); foreach (var fileName in fileNames) { BitmapSource source = Load(fileName); int bytesPerPixel = (source.Format.BitsPerPixel + 7) / 8; int stride = source.PixelWidth * bytesPerPixel; byte[] bitmapArray = new byte[source.PixelHeight * stride]; source.CopyPixels(bitmapArray, stride, 0); BitmapSource output = BitmapSource.Create(source.PixelWidth, source.PixelHeight, dpi, dpi, source.Format, source.Palette, bitmapArray, stride); encoder.Compression = compresion; encoder.Frames.Add(BitmapFrame.Create(output)); } encoder.Save(fs); fs.Close(); } }
public static Task SaveToTiffAsync(this BitmapSource image, string fileName, TiffCompressOption tiffCompressOption = TiffCompressOption.Default) { return(Task.Run(() => SaveToTiff(image, fileName, tiffCompressOption))); }
public ImageOptions(int jpegQualityLevel, TiffCompressOption tiffCompression) { JpegQualityLevel = jpegQualityLevel; TiffCompression = tiffCompression; }