private void SaveImage(BitmapSource source) { source = new FormatConvertedBitmap(source, (PixelFormat)ComboBoxSavePixelFormat.SelectedItem, null, 0); var saveFileDialog = new Microsoft.Win32.SaveFileDialog(); saveFileDialog.Filter = "*.png|*.png|*.bmp|*.bmp|*.tiff|*.tiff"; saveFileDialog.AddExtension = true; saveFileDialog.FileName = "HSVRect"; if (saveFileDialog.ShowDialog() == true) { BitmapEncoder encoder = new BmpBitmapEncoder(); if (saveFileDialog.FilterIndex == 1) { encoder = new PngBitmapEncoder(); } else if (saveFileDialog.FilterIndex == 2) { encoder = new BmpBitmapEncoder(); } else if (saveFileDialog.FilterIndex == 3) { encoder = new TiffBitmapEncoder(); } encoder.Frames.Add(BitmapFrame.Create(source)); using (var fs = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write)) { encoder.Save(fs); } } }
private void InitializeBitmapEncoder() { switch (EncodingFormat) { case BitmapEncoding.BMP: Encoder = new BmpBitmapEncoder(); break; case BitmapEncoding.GIF: Encoder = new GifBitmapEncoder(); break; case BitmapEncoding.JPEG: Encoder = new JpegBitmapEncoder(); break; case BitmapEncoding.PNG: Encoder = new PngBitmapEncoder(); break; case BitmapEncoding.TIFF: Encoder = new TiffBitmapEncoder(); break; case BitmapEncoding.WMP: Encoder = new WmpBitmapEncoder(); break; } }
/// <summary> /// Loads the PNG XMP meta data using a dummy TIFF. /// </summary> /// <param name="xmp">The XMP string to load.</param> /// <returns>The loaded XMP block, or null.</returns> private static BitmapMetadata LoadPNGMetadata(string xmp) { BitmapMetadata xmpData = null; using (MemoryStream stream = new MemoryStream()) { // PNG stores the XMP meta-data in an iTXt chunk as an UTF8 encoded string, // so we have to save it to a dummy tiff and grab the XMP meta-data on load. BitmapMetadata tiffMetadata = new BitmapMetadata("tiff"); tiffMetadata.SetQuery("/ifd/xmp", new BitmapMetadata("xmp")); tiffMetadata.SetQuery("/ifd/xmp", Encoding.UTF8.GetBytes(xmp)); BitmapSource source = BitmapSource.Create(1, 1, 96.0, 96.0, PixelFormats.Gray8, null, new byte[] { 255 }, 1); TiffBitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(source, null, tiffMetadata, null)); encoder.Save(stream); TiffBitmapDecoder dec = new TiffBitmapDecoder(stream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None); if (dec.Frames.Count == 1) { BitmapMetadata meta = dec.Frames[0].Metadata as BitmapMetadata; if (meta != null) { xmpData = meta.GetQuery("/ifd/xmp") as BitmapMetadata; } } } return(xmpData); }
private static void SaveToGrayscaleTiff(string filename, uint[,] imageArray) { if (File.Exists(filename)) { File.Delete(filename); } int width = imageArray.GetLength(0); int height = imageArray.GetLength(1); var array = new ushort[width * height]; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { array[j * width + i] = (ushort)imageArray[i, j]; } } var bitmapSrc = BitmapSource.Create(width, height, 96, 96, PixelFormats.Gray16, null, array, 4 * ((width * 2 + 3) / 4)); using (var fs = File.OpenWrite(filename)) { TiffBitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Compression = TiffCompressOption.None; encoder.Frames.Add(BitmapFrame.Create(bitmapSrc)); encoder.Save(fs); fs.Close(); } }
public void SaveToTIF(string fileName) { XpsDocument xd = new XpsDocument(fileName, System.IO.FileAccess.Read); FixedDocumentSequence fds = xd.GetFixedDocumentSequence(); DocumentPaginator paginator = fds.DocumentPaginator; System.Windows.Media.Visual visual = paginator.GetPage(0).Visual; System.Windows.FrameworkElement fe = (System.Windows.FrameworkElement)visual; int multiplyFactor = 4; string outputPath = fileName.Replace(".xps", ".tif"); RenderTargetBitmap bmp = new RenderTargetBitmap( (int)fe.ActualWidth * multiplyFactor, (int)fe.ActualHeight * multiplyFactor, 96d * multiplyFactor, 96d * multiplyFactor, System.Windows.Media.PixelFormats.Default); bmp.Render(fe); TiffBitmapEncoder tff = new TiffBitmapEncoder(); tff.Frames.Add(BitmapFrame.Create(bmp)); using (Stream stream = File.Create(outputPath)) { tff.Save(stream); } }
private static byte[] ExtractXMPPacket(BitmapMetadata xmp) { BitmapMetadata tiffMetaData = new BitmapMetadata("tiff"); tiffMetaData.SetQuery("/ifd/xmp", new BitmapMetadata("xmp")); foreach (string tag in xmp) { object value = xmp.GetQuery(tag); if (value is BitmapMetadata xmpSub) { CopySubIFDRecursive(ref tiffMetaData, xmpSub, "/ifd/xmp" + tag); } else { tiffMetaData.SetQuery("/ifd/xmp" + tag, value); } } byte[] xmpBytes = null; using (MemoryStream stream = new MemoryStream()) { // Create a dummy tiff to extract the XMP packet from. BitmapSource source = BitmapSource.Create(1, 1, 96.0, 96.0, PixelFormats.Gray8, null, new byte[] { 255 }, 1); TiffBitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(source, null, tiffMetaData, null)); encoder.Save(stream); xmpBytes = TiffReader.ExtractXMP(stream); } return(xmpBytes); }
public override void Save(Stream stream, ImageFormat format) { BitmapEncoder encoder; if (format.Equals(ImageFormat.Bmp)) { encoder = new BmpBitmapEncoder(); } else if (format.Equals(ImageFormat.Gif)) { encoder = new GifBitmapEncoder(); } else if (format.Equals(ImageFormat.Jpeg)) { encoder = new JpegBitmapEncoder(); } else if (format.Equals(ImageFormat.Png)) { encoder = new PngBitmapEncoder(); } else if (format.Equals(ImageFormat.Tiff)) { encoder = new TiffBitmapEncoder(); } else { throw new ArgumentOutOfRangeException("format", format, "Unsupported bitmap encoding format"); } encoder.Frames.Add(BitmapFrame.Create((BitmapSource)this)); encoder.Save(stream); }
private void button_Click(object sender, RoutedEventArgs e) { System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog(); sfd.Filter = "图片文件|*.bmp"; if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK) { return; } InkCanvas ink = this.inkCanvas; string path = sfd.FileName; FileStream fs = new FileStream(path, FileMode.Create); //文件流对象 //RenderTargetBitmap用来创建一副位图对象 RenderTargetBitmap rtb = new RenderTargetBitmap((int)ink.ActualWidth, (int)ink.ActualHeight, 1 / 100, 1 / 100, PixelFormats.Default); rtb.Render(ink); //呈现位图对象 //BitmapEncoder用来保存BitmapFrame对象,并保存为指定的文件 //BitmapFrame是图像数据 BitmapEncoder be = new TiffBitmapEncoder(); //指定格式 be.Frames.Add(BitmapFrame.Create(rtb)); be.Save(fs); fs.Close(); }
/// <summary> /// Saves the RenderTargetBitmap passed to the outputStream supplied /// in TIFF format. /// </summary> /// <param name="src">A RenderTargetBitmap containing the image data to save.</param> /// <param name="outputStream">A Stream used as the output stream to /// write the image data to.</param> public static void saveAsBmp(RenderTargetBitmap src, Stream outputStream) { TiffBitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(src)); encoder.Save(outputStream); }
/// <summary> /// 将指定位图保存到指定路径中 /// </summary> /// <param name="bitmap">要保存的位图</param> /// <param name="path">要保存到的路径(包含文件名和扩展卡名)</param> public static void Save(BitmapSource bitmap, string path) { BitmapEncoder encoder = null; string fileExtension = System.IO.Path.GetExtension(path).ToUpper(); //选取编码器 switch (fileExtension) { case ".BMP": encoder = new BmpBitmapEncoder(); break; case ".GIF": encoder = new GifBitmapEncoder(); break; case ".JPEG": encoder = new JpegBitmapEncoder(); break; case ".PNG": encoder = new PngBitmapEncoder(); break; case ".TIFF": encoder = new TiffBitmapEncoder(); break; default: throw new Exception("无法识别的图像格式!"); } encoder.Frames.Add(BitmapFrame.Create(bitmap)); using (var file = File.Create(path)) encoder.Save(file); }
private static BitmapSource?ChooseOrEncodeWindowIcon(BitmapSource?smallIcon, BitmapSource?largeIcon) { BitmapSource?bitmapSource = null; if (largeIcon != null) { if (smallIcon != null) { BitmapFrame bitmapFrame; var tiffBitmapEncoder = new TiffBitmapEncoder(); tiffBitmapEncoder.Frames.Add(BitmapFrame.Create(smallIcon)); tiffBitmapEncoder.Frames.Add(BitmapFrame.Create(largeIcon)); using (var memoryStream = new MemoryStream()) { tiffBitmapEncoder.Save(memoryStream); bitmapFrame = BitmapFrame.Create(memoryStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); } FreezeImage(bitmapFrame); bitmapSource = bitmapFrame; } else { bitmapSource = largeIcon; } } else if (smallIcon != null) { bitmapSource = smallIcon; } return(bitmapSource); }
/// <summary> /// Exports the PDF pages as Tiff Images. /// </summary> /// <param name="fileName">The PDF file name</param> private void ExportPDFtoTiff(string fileName) { PdfDocumentView pdfViewer = new PdfDocumentView(); //Load the input PDF file pdfViewer.Load(fileName); //Export the images From the input PDF file at the page range of 0 to 1 . BitmapSource[] image = pdfViewer.ExportAsImage(0, pdfViewer.PageCount - 1); if (image != null) { for (int i = 0; i < image.Length; i++) { //Initialize the new Tiff bitmap encoder TiffBitmapEncoder encoder = new TiffBitmapEncoder(); //Set the compression to zip to reduce the file size. encoder.Compression = TiffCompressOption.Zip; //Create the bitmap frame using the bitmap source and add it to the encoder. encoder.Frames.Add(BitmapFrame.Create(image[i])); //Create the file stream for the output in the desired image format. using (FileStream stream = new FileStream("Image_" + i.ToString() + ".Tiff", FileMode.Create)) { //Save the stream, so that the image will be generated in the output location. encoder.Save(stream); } } } }
public void SaveAs16BitBitmap(string path) { var bmp = To16BitBitmap(); var rect = new Rectangle(0, 0, bmp.Width, bmp.Height); var bitmapData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat); var source = BitmapSource.Create(bmp.Width, bmp.Height, bmp.HorizontalResolution, bmp.VerticalResolution, PixelFormats.Gray16, null, bitmapData.Scan0, bitmapData.Stride * bmp.Height, bitmapData.Stride); bmp.UnlockBits(bitmapData); using (var stream = new FileStream(path, FileMode.Create)) { var encoder = new TiffBitmapEncoder(); encoder.Compression = TiffCompressOption.None; encoder.Frames.Add(BitmapFrame.Create(source)); encoder.Save(stream); stream.Close(); } }
public static void SaveImageRaster3DAsTIFF3D2(string save_path, IImageRaster <IRaster3DInteger, uint> image) { Stream destination_stream = new FileStream(save_path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); TiffBitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Compression = TiffCompressOption.None; for (int index_z = 0; index_z < image.Raster.Size2; index_z++) { Bitmap bitmap = ConvertToBitmapUInt16(image, index_z); BitmapSource bitmap_source = ToolsRendering.CreateBitmapSourceFromBitmap16Bit(bitmap); encoder.Frames.Add(BitmapFrame.Create(bitmap_source)); } encoder.Save(destination_stream); //TiffBitmapEncoder encoder = new TiffBitmapEncoder(destination_stream_, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); //Stream imageStreamSource = new FileStream(load_path, FileMode.Open, FileAccess.Read, FileShare.Read); //TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); //BitmapSource test_frame = decoder.Frames[0]; //int size_x = test_frame.PixelWidth; //int size_y = test_frame.PixelHeight; //int size_z = decoder.Frames.Count; //int bits_per_pixel = test_frame.Format.BitsPerPixel; //for (int index_z = 0; index_z < size_z; index_z++) //{ // // save each frame to a bytestream // BitmapSource frame = decoder.Frames[index_z]; // // img.CopyPixels(pixels, stride, 0) // MemoryStream byte_stream = new MemoryStream(); // // bitmap.Save(byte_stream, ImageFormat.Tiff); // // and then create a new Image from it // System.Drawing.Image image = System.Drawing.Image.FromStream(byte_stream); // // d //} }
/// <summary>画像データを縮小する</summary> /// <param name="refStream">画像データ</param> /// <param name="imageSize">画像の最大サイズ</param> private static void ShrinkImageData(ref MemoryStream refStream, int imageSize = 128) { long streamPosition = refStream.Position; var srcImage = BitmapFrame.Create(refStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); if (srcImage.PixelWidth <= imageSize && srcImage.PixelHeight <= imageSize) { refStream.Position = streamPosition; return; } double maxScale = Math.Min( imageSize / (double)srcImage.PixelWidth, imageSize / (double)srcImage.PixelHeight); var destImage = ResizeImage(srcImage, maxScale); var imageStream = new MemoryStream(); System.Diagnostics.Debug.WriteLine($"[{nameof(ProfileImageCache)}] Image resized. width: {destImage.PixelWidth}, height: {destImage.PixelHeight}"); var encoder = new TiffBitmapEncoder { Compression = TiffCompressOption.Default, Frames = new BitmapFrame[] { BitmapFrame.Create(destImage) }, }; encoder.Save(imageStream); refStream.Dispose(); imageStream.Position = 0; refStream = imageStream; }
public ScreenshotTakenEventArgs(BitmapSource bitmap, ImageFormat format = null) { if (format == null) { format = ImageFormat.Png; } BitmapEncoder encoder = new PngBitmapEncoder(); if (format.Equals(ImageFormat.Bmp)) { encoder = new BmpBitmapEncoder(); } if (format.Equals(ImageFormat.Gif)) { encoder = new GifBitmapEncoder(); } if (format.Equals(ImageFormat.Jpeg)) { encoder = new JpegBitmapEncoder(); } if (format.Equals(ImageFormat.Tiff)) { encoder = new TiffBitmapEncoder(); } encoder.Frames.Add(BitmapFrame.Create(bitmap)); var memoryStream = new MemoryStream(); encoder.Save(memoryStream); ImageData = memoryStream; ImageExtension = encoder.CodecInfo.FileExtensions.Split(',')[0]; // First extension. Usually the best. But it doesn't matter much. Yes. }
public static BitmapEncoder CreateBitmapEncoder(BitmapEncodingMode mode) { BitmapEncoder e = null; switch (mode) { case BitmapEncodingMode.Bmp: e = new BmpBitmapEncoder(); break; case BitmapEncodingMode.Gif: e = new GifBitmapEncoder(); break; case BitmapEncodingMode.Jpeg: e = new JpegBitmapEncoder(); break; case BitmapEncodingMode.Png: e = new PngBitmapEncoder(); break; case BitmapEncodingMode.Tiff: e = new TiffBitmapEncoder(); break; case BitmapEncodingMode.Wmp: e = new WmpBitmapEncoder(); break; } return(e); }
public static void Save(string fileName, List <System.Windows.Controls.Image> imageList) { TiffBitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Compression = TiffCompressOption.Ccitt4; foreach (System.Windows.Controls.Image image in imageList) { BitmapSource bitmapSource = (BitmapSource)image.Source; BitmapFrame bitmapFrame = BitmapFrame.Create(bitmapSource); if (bitmapFrame != null) { encoder.Frames.Add(BitmapFrame.Create(bitmapSource)); } } if (encoder.Frames.Count > 0) { FileStream fileStream = new System.IO.FileStream(fileName, FileMode.Create); encoder.Save(fileStream); fileStream.Close(); } else { throw new Exception("No frames in the scanned file encoder."); } }
/// <summary> /// 保存图像 /// </summary> /// <param name="savePath">图像保存路径</param> /// <param name="BS">控件源</param> /// <param name="extName">文件拓展名</param> public static void SaveImage(string savePath, BitmapSource BS, string extName) { BitmapEncoder encoder = null; switch (extName) { case ".png": encoder = new PngBitmapEncoder(); break; case ".jpg": encoder = new JpegBitmapEncoder(); break; case ".bmp": encoder = new BmpBitmapEncoder(); break; case ".gif": encoder = new GifBitmapEncoder(); break; case ".tiff": encoder = new TiffBitmapEncoder(); break; default: throw new InvalidOperationException(); } encoder.Frames.Add(BitmapFrame.Create(BS)); encoder.Save(File.Create(savePath)); }
private static void SaveBmp(Bitmap bmp, string path) { Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bitmapData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat); var pixelFormats = ConvertBmpPixelFormat(bmp.PixelFormat); BitmapSource source = BitmapSource.Create(bmp.Width, bmp.Height, bmp.HorizontalResolution, bmp.VerticalResolution, pixelFormats, null, bitmapData.Scan0, bitmapData.Stride * bmp.Height, bitmapData.Stride); bmp.UnlockBits(bitmapData); FileStream stream = new FileStream(path, FileMode.Create); TiffBitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Compression = TiffCompressOption.Zip; encoder.Frames.Add(BitmapFrame.Create(source)); encoder.Save(stream); stream.Close(); }
public void Scan_NormalPicture_Succeeds(int depth, string mode, string pattern, bool inverted, string outFile) { using (var connection = Connection.At(TestConstants.SaneDaemon)) { using (var device = connection.OpenDevice( TestConstants.UnAuthenticatedDevice)) { var opts = device.AllOptions.ToList(); var pict = opts.First(o => o.Name.Equals("test-picture")); pict.Value = pattern; var cdepth = opts.First(o => o.Name.Equals("depth")); cdepth.Value = depth; var cmode = opts.First(o => o.Name.Equals("mode")); cmode.Value = mode; var inv = opts.First(o => o.Name.Equals("invert-endianess")); if (inv.IsActive) { inv.Value = inverted; } var res = device.Scan(); Assert.That(res.IsError, Is.False, "Error calling scan"); var ours = res.Image.ToBitmapImage(); var theirs = LoadReference(outFile).ToBitmapImage(); bool match = theirs.IsEqual(ours); if (!match) { var failureFile = Path.Combine( TestConstants.FailedTestOutputFolder, outFile) + ".tiff"; var encoder = new TiffBitmapEncoder { Compression = TiffCompressOption.None }; using (var f = File.Create(failureFile)) { encoder.Frames.Add(BitmapFrame.Create(ours)); encoder.Save(f); } } Assert.That(match, Is.True, "Image does not match reference"); } } }
public bool SaveImageToFTPServer(ImageSource imageSource, string imageLocationMemory, string imageLocationDisk) { BitmapEncoder encoder = new TiffBitmapEncoder(); byte[] biteArray = ImageSourceToBytes(encoder, imageSource); // Function returns byte[] csv file using (var client = new Renci.SshNet.SftpClient(Host, Port, Username, Password)) { client.Connect(); if (client.IsConnected) { client.ChangeDirectory(SFTPWorkingDirectory); using (var ms = new MemoryStream(biteArray)) { client.BufferSize = (uint)ms.Length; // bypass Payload error large files client.UploadFile(ms, imageLocationDisk); // imageLocationDisk == openFileDialog.FileName client.RenameFile(client.WorkingDirectory + "/" + imageLocationDisk, client.WorkingDirectory + "/" + imageLocationMemory); return(true); } } else { OutputMessage = "Couldn't connect to SFTP server."; return(false); } } }
private static void SaveToColor8bitTiff(string filename, uint[,,] imageArray) { if (File.Exists(filename)) { File.Delete(filename); } int width = imageArray.GetLength(0); int height = imageArray.GetLength(1); var array = new Byte[width * height * 3]; for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { array[j * width * 3 + i * 3] = (byte)imageArray[i, j, 2]; array[j * width * 3 + i * 3 + 1] = (byte)imageArray[i, j, 1]; array[j * width * 3 + i * 3 + 2] = (byte)imageArray[i, j, 0]; } } var bitmapSrc = BitmapSource.Create(width, height, 96, 96, PixelFormats.Rgb24, null, array, 4 * ((width * 3 + 3) / 4)); using (var fs = File.OpenWrite(filename)) { TiffBitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Compression = TiffCompressOption.None; encoder.Frames.Add(BitmapFrame.Create(bitmapSrc)); encoder.Save(fs); fs.Close(); } }
public async Task <bool> WriteImageSourceAsByteArraySFTP(ImageSource imageSource, string imageLocationMemory) { var taskResult = await Task.Run(() => { BitmapEncoder encoder = new TiffBitmapEncoder(); byte[] biteArray = ImageSourceToBytes(encoder, imageSource); // Function returns byte[] csv file using (var client = new Renci.SshNet.SftpClient(Host, Port, Username, Password)) { client.Connect(); if (client.IsConnected) { client.ChangeDirectory(SFTPWorkingDirectory); using (var ms = new MemoryStream(biteArray)) { client.BufferSize = (uint)ms.Length; // bypass Payload error large files client.Create(SFTPWorkingDirectory + "/" + imageLocationMemory); client.WriteAllBytes(SFTPWorkingDirectory + "/" + imageLocationMemory, biteArray); // imageLocationDisk == openFileDialog.FileName return(true); } } else { OutputMessage = "Couldn't connect to SFTP server."; return(false); } } }); return(taskResult); }
private void GenerateImage(BitmapSource bitmap, ImageFormat format, Stream destStream) { BitmapEncoder encoder = null; switch (format) { case ImageFormat.JPG: encoder = new JpegBitmapEncoder(); break; case ImageFormat.PNG: encoder = new PngBitmapEncoder(); break; case ImageFormat.BMP: encoder = new BmpBitmapEncoder(); break; case ImageFormat.GIF: encoder = new GifBitmapEncoder(); break; case ImageFormat.TIF: encoder = new TiffBitmapEncoder(); break; default: throw new InvalidOperationException(); } encoder.Frames.Add(BitmapFrame.Create(bitmap)); encoder.Save(destStream); }
public void RotateTiffImage(string filePath, double rotation) { BitmapFrame transformed; using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { var bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.StreamSource = fileStream; bitmap.EndInit(); bitmap.Freeze(); TransformedBitmap transformedBitmap = new TransformedBitmap(bitmap, new RotateTransform(rotation)); transformedBitmap.Freeze(); transformed = BitmapFrame.Create(transformedBitmap); transformed.Freeze(); } // ファイルを書き換えないとファイルサイズが増大するので一度削除 FileInfo fi = new FileInfo(filePath); fi.Delete(); using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Write)) { TiffBitmapEncoder tiffEncoder = new TiffBitmapEncoder(); tiffEncoder.Frames.Add(transformed); tiffEncoder.Save(fileStream); } }
public static BitmapEncoder CreateEncoder(BitmapEncoderType encoderType, BitmapSource bitmapSource) { BitmapEncoder bitmapEncoder; switch (encoderType) { case BitmapEncoderType.BMP: bitmapEncoder = new BmpBitmapEncoder(); break; case BitmapEncoderType.GIF: bitmapEncoder = new GifBitmapEncoder(); break; case BitmapEncoderType.PNG: bitmapEncoder = new PngBitmapEncoder(); break; case BitmapEncoderType.TIFF: bitmapEncoder = new TiffBitmapEncoder(); break; case BitmapEncoderType.WMP: bitmapEncoder = new WmpBitmapEncoder(); break; default: bitmapEncoder = new JpegBitmapEncoder(); break; } bitmapEncoder.Frames.Add(BitmapFrame.Create(bitmapSource)); return(bitmapEncoder); }
public static void SaveFixedDocumentAsTiff(FixedDocument document, string outputFileName) { int pages = document.DocumentPaginator.PageCount; TiffBitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Compression = TiffCompressOption.Ccitt4; for (int pageNum = 0; pageNum < pages; pageNum++) { DocumentPage docPage = document.DocumentPaginator.GetPage(pageNum); RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)(docPage.Size.Width * 300 / 96), (int)(docPage.Size.Height * 300 / 96), 300d, // WPF (Avalon) units are 96dpi based 300d, System.Windows.Media.PixelFormats.Default); renderTarget.Render(docPage.Visual); encoder.Frames.Add(BitmapFrame.Create(renderTarget)); } FileStream outputFileStream = new FileStream(outputFileName, FileMode.Create); encoder.Save(outputFileStream); outputFileStream.Close(); }
//画像保存 private void SaveImage(BitmapSource source, string filePath) { var saveFileDialog = new Microsoft.Win32.SaveFileDialog(); saveFileDialog.Filter = "*.png|*.png|*.bmp|*.bmp|*.tiff|*.tiff"; saveFileDialog.AddExtension = true; saveFileDialog.FileName = System.IO.Path.GetFileNameWithoutExtension(filePath) + "_"; saveFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(filePath); if (saveFileDialog.ShowDialog() == true) { BitmapEncoder encoder = new BmpBitmapEncoder(); if (saveFileDialog.FilterIndex == 1) { encoder = new PngBitmapEncoder(); } else if (saveFileDialog.FilterIndex == 2) { encoder = new BmpBitmapEncoder(); } else if (saveFileDialog.FilterIndex == 3) { encoder = new TiffBitmapEncoder(); } encoder.Frames.Add(BitmapFrame.Create(source)); using (var fs = new System.IO.FileStream(saveFileDialog.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write)) { encoder.Save(fs); } } }
/// <summary> /// 获取影像的编码格式 /// <param name="filePath">文件路径</param> /// </summary> //-------------------------------------------------------------------------- //修改历史: //日期 修改人 修改 //2010-8-5 qizhenguo 创建代码 //-------------------------------------------------------------------------- private BitmapEncoder GetEncoder(string filePath) { BitmapEncoder encoder = new JpegBitmapEncoder(); string ext = Path.GetExtension(filePath); switch (ext) { case "jpg": case "jpeg": encoder = new JpegBitmapEncoder(); break; case "bmp": encoder = new BmpBitmapEncoder(); break; case "tiff": case "tif": encoder = new TiffBitmapEncoder(); break; case "png": encoder = new PngBitmapEncoder(); break; case "gif": encoder = new GifBitmapEncoder(); break; default: break; } return(encoder); }