private Document OnLoadImpl(Stream input) { WmpBitmapDecoder wbd = new WmpBitmapDecoder(input, BitmapCreateOptions.None, BitmapCacheOption.None); BitmapFrame frame0 = wbd.Frames[0]; Document output = new Document(frame0.PixelWidth, frame0.PixelHeight); output.DpuUnit = MeasurementUnit.Inch; output.DpuX = frame0.DpiX; output.DpuY = frame0.DpiY; BitmapLayer layer = Layer.CreateBackgroundLayer(output.Width, output.Height); MemoryBlock memoryBlock = layer.Surface.Scan0; IntPtr scan0 = memoryBlock.Pointer; FormatConvertedBitmap fcb = new FormatConvertedBitmap(frame0, System.Windows.Media.PixelFormats.Bgra32, null, 0); fcb.CopyPixels(Int32Rect.Empty, scan0, (int)memoryBlock.Length, layer.Surface.Stride); output.Layers.Add(layer); BitmapMetadata hdMetadata = (BitmapMetadata)frame0.Metadata; CopyMetadataTo(output.Metadata, hdMetadata); // WPF doesn't give us an IDisposable implementation on its types Utility.GCFullCollect(); return(output); }
public static BitmapDecoder CreateBitmapDecoder(BitmapEncodingMode mode, Stream fs, BitmapCreateOptions createOpt, BitmapCacheOption cacheOpt) { BitmapDecoder e = null; switch (mode) { case BitmapEncodingMode.Bmp: e = new BmpBitmapDecoder(fs, createOpt, cacheOpt); break; case BitmapEncodingMode.Gif: e = new GifBitmapDecoder(fs, createOpt, cacheOpt); break; case BitmapEncodingMode.Jpeg: e = new JpegBitmapDecoder(fs, BitmapCreateOptions.None, BitmapCacheOption.Default); break; case BitmapEncodingMode.Png: e = new PngBitmapDecoder(fs, createOpt, cacheOpt); break; case BitmapEncodingMode.Tiff: e = new TiffBitmapDecoder(fs, createOpt, cacheOpt); break; case BitmapEncodingMode.Wmp: e = new WmpBitmapDecoder(fs, createOpt, cacheOpt); break; } return(e); }
/// <summary> /// Method, used as function reference to create a WDP /// </summary> /// <param name="input">Input memory stream</param> /// <returns>Output memory stream of a BitmapImage</returns> private static MemoryStream GetWdp(MemoryStream input) { MemoryStream ms = new MemoryStream(); WmpBitmapDecoder wmp = new WmpBitmapDecoder(input, BitmapCreateOptions.None, BitmapCacheOption.Default); BitmapEncoder encoder = new BmpBitmapEncoder(); encoder.Frames.Add(wmp.Frames[0]); encoder.Save(ms); ms.Flush(); ms.Position = 0; return(ms); }
public static void JxrToBmp(string source, string target) { Stream imageStreamSource = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read); WmpBitmapDecoder decoder = new WmpBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); BitmapSource bitmapSource = decoder.Frames[0]; var encoder = new BmpBitmapEncoder();; encoder.Frames.Add(BitmapFrame.Create(bitmapSource)); using (var stream = new FileStream(target, FileMode.Create)) { encoder.Save(stream); } }
private void FileDialogButton_Click(object sender, RoutedEventArgs e) { var fileDialog = new OpenFileDialog { Filter = "Images |*.png;*.jpg;*.bmp;*.gif;*.tif;*.wmp;*.ico", CheckFileExists = true, CheckPathExists = true, Multiselect = false, Title = "Odaberi sliku", }; fileDialogButton.BorderBrush = buttonFrameBrushColor; // Kad se selektuje fajl if (fileDialog.ShowDialog() == true) { var bco = BitmapCreateOptions.PreservePixelFormat; var bcco = BitmapCacheOption.Default; string extension = fileDialog.SafeFileName.Split('.').Last(); switch (extension) { case "bmp": var bmpDecoder = new BmpBitmapDecoder(new Uri(fileDialog.FileName), bco, bcco); bitmapSource = bmpDecoder.Frames[0]; imageHolder.Source = bitmapSource; break; case "gif": var gifDecoder = new GifBitmapDecoder(new Uri(fileDialog.FileName), bco, bcco); bitmapSource = gifDecoder.Frames[0]; imageHolder.Source = bitmapSource; break; case "ico": var icoDecoder = new IconBitmapDecoder(new Uri(fileDialog.FileName), bco, bcco); bitmapSource = icoDecoder.Frames[0]; imageHolder.Source = bitmapSource; break; case "jpg": var jpgDecoder = new JpegBitmapDecoder(new Uri(fileDialog.FileName), bco, bcco); bitmapSource = jpgDecoder.Frames[0]; imageHolder.Source = bitmapSource; break; case "png": var pngDecoder = new PngBitmapDecoder(new Uri(fileDialog.FileName), bco, bcco); bitmapSource = pngDecoder.Frames[0]; imageHolder.Source = bitmapSource; break; case "tif": var tiffDecoder = new TiffBitmapDecoder(new Uri(fileDialog.FileName), bco, bcco); bitmapSource = tiffDecoder.Frames[0]; imageHolder.Source = bitmapSource; break; case "wmp": var wmpDecoder = new WmpBitmapDecoder(new Uri(fileDialog.FileName), bco, bcco); bitmapSource = wmpDecoder.Frames[0]; imageHolder.Source = bitmapSource; break; default: MessageBox.Show("Image format not suported", "Extension Error", MessageBoxButton.OK, MessageBoxImage.Error); break; } } }
private void CreateAndShowMainWindow() { // Create the application's main window mainWindow = new Window(); mainWindow.Title = "WDP Imaging Sample"; ScrollViewer mySV = new ScrollViewer(); //<Snippet4> int width = 128; int height = width; int stride = width / 8; byte[] pixels = new byte[height * stride]; // Define the image palette BitmapPalette myPalette = BitmapPalettes.WebPalette; // Creates a new empty image with the pre-defined palette //<Snippet2> BitmapSource image = BitmapSource.Create( width, height, 96, 96, PixelFormats.Indexed1, myPalette, pixels, stride); //</Snippet2> //<Snippet3> FileStream stream = new FileStream("new.wdp", FileMode.Create); WmpBitmapEncoder encoder = new WmpBitmapEncoder(); TextBlock myTextBlock = new TextBlock(); myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString(); encoder.Frames.Add(BitmapFrame.Create(image)); encoder.Save(stream); //</Snippet3> //</Snippet4> //<Snippet1> // Open a Stream and decode a WDP image Stream imageStreamSource = new FileStream("tulipfarm.wdp", FileMode.Open, FileAccess.Read, FileShare.Read); WmpBitmapDecoder decoder = new WmpBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); BitmapSource bitmapSource = decoder.Frames[0]; // Draw the Image Image myImage = new Image(); myImage.Source = bitmapSource; myImage.Stretch = Stretch.None; myImage.Margin = new Thickness(20); //</Snippet1> //<Snippet5> // Open a Uri and decode a WDP image Uri myUri = new Uri("tulipfarm.wdp", UriKind.RelativeOrAbsolute); WmpBitmapDecoder decoder3 = new WmpBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); BitmapSource bitmapSource3 = decoder3.Frames[0]; // Draw the Image Image myImage2 = new Image(); myImage2.Source = bitmapSource3; myImage2.Stretch = Stretch.None; myImage2.Margin = new Thickness(20); //</Snippet5> //<Snippet6> FileStream stream2 = new FileStream("tulipfarm.jpg", FileMode.Open); JpegBitmapDecoder decoder2 = new JpegBitmapDecoder(stream2, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); BitmapSource bitmapSource2 = decoder2.Frames[0]; FileStream stream3 = new FileStream("new2.wdp", FileMode.Create); WmpBitmapEncoder encoder2 = new WmpBitmapEncoder(); encoder2.Frames.Add(BitmapFrame.Create(bitmapSource2)); encoder2.Save(stream3); //</Snippet6> // Define a StackPanel to host the decoded WDP images StackPanel myStackPanel = new StackPanel(); myStackPanel.Orientation = Orientation.Vertical; myStackPanel.VerticalAlignment = VerticalAlignment.Stretch; myStackPanel.HorizontalAlignment = HorizontalAlignment.Stretch; // Add the Image and TextBlock to the parent Grid myStackPanel.Children.Add(myImage); myStackPanel.Children.Add(myImage2); myStackPanel.Children.Add(myTextBlock); // Add the StackPanel as the Content of the Parent Window Object mySV.Content = myStackPanel; mainWindow.Content = mySV; mainWindow.Show(); }
private void CreateAndShowMainWindow() { // Create the application's main window _mainWindow = new Window { Title = "WDP Imaging Sample" }; var mySv = new ScrollViewer(); var width = 128; var height = width; var stride = width / 8; var pixels = new byte[height * stride]; // Define the image palette var myPalette = BitmapPalettes.WebPalette; // Creates a new empty image with the pre-defined palette var image = BitmapSource.Create( width, height, 96, 96, PixelFormats.Indexed1, myPalette, pixels, stride); var stream = new FileStream("new.wdp", FileMode.Create); var encoder = new WmpBitmapEncoder(); var myTextBlock = new TextBlock { Text = "Codec Author is: " + encoder.CodecInfo.Author }; encoder.Frames.Add(BitmapFrame.Create(image)); encoder.Save(stream); // Open a Stream and decode a WDP image Stream imageStreamSource = new FileStream("tulipfarm.wdp", FileMode.Open, FileAccess.Read, FileShare.Read); var decoder = new WmpBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); BitmapSource bitmapSource = decoder.Frames[0]; // Draw the Image var myImage = new Image { Source = bitmapSource, Stretch = Stretch.None, Margin = new Thickness(20) }; // Open a Uri and decode a WDP image var myUri = new Uri("tulipfarm.wdp", UriKind.RelativeOrAbsolute); var decoder3 = new WmpBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); BitmapSource bitmapSource3 = decoder3.Frames[0]; // Draw the Image var myImage2 = new Image { Source = bitmapSource3, Stretch = Stretch.None, Margin = new Thickness(20) }; var stream2 = new FileStream("tulipfarm.jpg", FileMode.Open); var decoder2 = new JpegBitmapDecoder(stream2, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); BitmapSource bitmapSource2 = decoder2.Frames[0]; var stream3 = new FileStream("new2.wdp", FileMode.Create); var encoder2 = new WmpBitmapEncoder(); encoder2.Frames.Add(BitmapFrame.Create(bitmapSource2)); encoder2.Save(stream3); // Define a StackPanel to host the decoded WDP images var myStackPanel = new StackPanel { Orientation = Orientation.Vertical, VerticalAlignment = VerticalAlignment.Stretch, HorizontalAlignment = HorizontalAlignment.Stretch }; // Add the Image and TextBlock to the parent Grid myStackPanel.Children.Add(myImage); myStackPanel.Children.Add(myImage2); myStackPanel.Children.Add(myTextBlock); // Add the StackPanel as the Content of the Parent Window Object mySv.Content = myStackPanel; _mainWindow.Content = mySv; _mainWindow.Show(); }
private void OnSaveImpl( Document input, Stream output, SaveConfigToken token, Surface scratchSurface, ProgressEventHandler callback) { HDPhotoSaveConfigToken hdToken = token as HDPhotoSaveConfigToken; WmpBitmapEncoder wbe = new WmpBitmapEncoder(); using (RenderArgs ra = new RenderArgs(scratchSurface)) { input.Render(ra, true); } MemoryBlock block = scratchSurface.Scan0; IntPtr scan0 = block.Pointer; double dpiX; double dpiY; switch (input.DpuUnit) { case MeasurementUnit.Centimeter: dpiX = Document.DotsPerCmToDotsPerInch(input.DpuX); dpiY = Document.DotsPerCmToDotsPerInch(input.DpuY); break; case MeasurementUnit.Inch: dpiX = input.DpuX; dpiY = input.DpuY; break; case MeasurementUnit.Pixel: dpiX = Document.GetDefaultDpu(MeasurementUnit.Inch); dpiY = Document.GetDefaultDpu(MeasurementUnit.Inch); break; default: throw new InvalidEnumArgumentException(); } BitmapSource bitmapSource = BitmapFrame.Create( scratchSurface.Width, scratchSurface.Height, dpiX, dpiY, System.Windows.Media.PixelFormats.Bgra32, null, scan0, (int)block.Length, // TODO: does not support >2GB images scratchSurface.Stride); FormatConvertedBitmap fcBitmap = new FormatConvertedBitmap( bitmapSource, hdToken.BitDepth == 24 ? PixelFormats.Bgr24 : PixelFormats.Bgra32, null, 0); BitmapFrame outputFrame0 = BitmapFrame.Create(fcBitmap); wbe.Frames.Add(outputFrame0); wbe.ImageQualityLevel = (float)hdToken.Quality / 100.0f; string tempFileName = FileSystem.GetTempFileName(); FileStream tempFileOut = new FileStream(tempFileName, FileMode.Create, FileAccess.Write, FileShare.Read); wbe.Save(tempFileOut); tempFileOut.Close(); tempFileOut = null; FileStream tempFileIn = new FileStream(tempFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read); WmpBitmapDecoder wbd = new WmpBitmapDecoder(tempFileIn, BitmapCreateOptions.None, BitmapCacheOption.None); BitmapFrame ioFrame0 = wbd.Frames[0]; InPlaceBitmapMetadataWriter metadata2 = ioFrame0.CreateInPlaceBitmapMetadataWriter(); CopyMetadataTo(metadata2, input.Metadata); tempFileIn.Close(); tempFileIn = null; FileStream tempFileIn2 = new FileStream(tempFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read); Utility.CopyStream(tempFileIn2, output); tempFileIn2.Close(); tempFileIn2 = null; try { File.Delete(tempFileName); } catch (Exception) { } // WPF doesn't give us an IDisposable implementation on its types Utility.GCFullCollect(); }
public static BitmapSource ImageSourceReturn(string uri) { try { Uri myUri = new Uri(uri); switch (myUri.Segments.Last().Substring(myUri.Segments.Last().Count() - 3, 3)) { case "jpg": { JpegBitmapDecoder dec = new JpegBitmapDecoder(myUri, BitmapCreateOptions.None, BitmapCacheOption.Default); BitmapSource bs = dec.Frames[0]; return(bs); } case "peg": { JpegBitmapDecoder dec = new JpegBitmapDecoder(myUri, BitmapCreateOptions.None, BitmapCacheOption.Default); BitmapSource bs = dec.Frames[0]; return(bs); } case "bmp": { BmpBitmapDecoder dec = new BmpBitmapDecoder(myUri, BitmapCreateOptions.None, BitmapCacheOption.Default); BitmapSource bs = dec.Frames[0]; return(bs); } case "png": { PngBitmapDecoder dec = new PngBitmapDecoder(myUri, BitmapCreateOptions.None, BitmapCacheOption.Default); BitmapSource bs = dec.Frames[0]; return(bs); } case "gif": { GifBitmapDecoder dec = new GifBitmapDecoder(myUri, BitmapCreateOptions.None, BitmapCacheOption.Default); BitmapSource bs = dec.Frames[0]; return(bs); } case "iff": { TiffBitmapDecoder dec = new TiffBitmapDecoder(myUri, BitmapCreateOptions.None, BitmapCacheOption.Default); BitmapSource bs = dec.Frames[0]; return(bs); } case "wmp": { WmpBitmapDecoder dec = new WmpBitmapDecoder(myUri, BitmapCreateOptions.None, BitmapCacheOption.Default); BitmapSource bs = dec.Frames[0]; return(bs); } case "JPG": { JpegBitmapDecoder dec = new JpegBitmapDecoder(myUri, BitmapCreateOptions.None, BitmapCacheOption.Default); BitmapSource bs = dec.Frames[0]; return(bs); } case "PEG": { JpegBitmapDecoder dec = new JpegBitmapDecoder(myUri, BitmapCreateOptions.None, BitmapCacheOption.Default); int f = dec.Frames.Count; BitmapSource bs = new BitmapImage(myUri); return(bs); } case "BMP": { BmpBitmapDecoder dec = new BmpBitmapDecoder(myUri, BitmapCreateOptions.None, BitmapCacheOption.Default); BitmapSource bs = dec.Frames[0]; return(bs); } case "PNG": { PngBitmapDecoder dec = new PngBitmapDecoder(myUri, BitmapCreateOptions.None, BitmapCacheOption.Default); BitmapSource bs = dec.Frames[0]; return(bs); } case "GIF": { GifBitmapDecoder dec = new GifBitmapDecoder(myUri, BitmapCreateOptions.None, BitmapCacheOption.Default); BitmapSource bs = dec.Frames[0]; return(bs); } case "IFF": { TiffBitmapDecoder dec = new TiffBitmapDecoder(myUri, BitmapCreateOptions.None, BitmapCacheOption.Default); BitmapSource bs = dec.Frames[0]; return(bs); } case "WMP": { WmpBitmapDecoder dec = new WmpBitmapDecoder(myUri, BitmapCreateOptions.None, BitmapCacheOption.Default); BitmapSource bs = dec.Frames[0]; return(bs); } default: { return(null); } } } catch (Exception e) { return(null); } }
protected override ImpImage Load(string path, out ImpError error) { error = null; var myUri = new Uri(path, UriKind.RelativeOrAbsolute); var type = GetType(path); BitmapDecoder decoder = null; try { switch (type) { case ImageType.Jpg: try { decoder = new JpegBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); } catch (Exception) { var impJpgDecoder = new ImpJpgDecoder(myUri); return(new ImpImage(impJpgDecoder.Source)); } break; case ImageType.Png: try { decoder = new PngBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); } catch (Exception) { var impPngDecoder = new ImpPngDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); return(new ImpImage(impPngDecoder.Source)); } break; case ImageType.Gif: decoder = new GifBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); if (decoder.Frames.Count > 1) { using (var stream = new FileStream(myUri.AbsolutePath, FileMode.Open)) { var gifFile = GifFile.ReadGifFile(stream, true); return(new ImpImage((GifBitmapDecoder)decoder, gifFile)); } } break; case ImageType.Bmp: decoder = new BmpBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); break; case ImageType.Tiff: decoder = new TiffBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); break; case ImageType.Tga: error = new ImpError(ErrorType.NotSupportedFile); break; case ImageType.Icon: decoder = new IconBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); break; case ImageType.WindowsMediaPhoto: decoder = new WmpBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); break; default: error = new ImpError(ErrorType.UnknownFileType); break; } } catch (Exception) { error = new ImpError(ErrorType.FailedToOpenFile); } if (error != null) { return(null); } return(new ImpImage(decoder)); }
private static BitmapSource Load(object obj, BitmapEncoding enc, BitmapCreateOptions create, BitmapCacheOption cache, out BitmapMetadata data) { BitmapDecoder dec = null; if (obj is Stream) { Stream stream = obj as Stream; switch (enc) { case BitmapEncoding.Bmp: dec = new BmpBitmapDecoder(stream, create, cache); break; case BitmapEncoding.Png: dec = new PngBitmapDecoder(stream, create, cache); break; case BitmapEncoding.Jpg: dec = new JpegBitmapDecoder(stream, create, cache); break; case BitmapEncoding.Tiff: dec = new TiffBitmapDecoder(stream, create, cache); break; case BitmapEncoding.Gif: dec = new GifBitmapDecoder(stream, create, cache); break; case BitmapEncoding.Wmp: dec = new WmpBitmapDecoder(stream, create, cache); break; case BitmapEncoding.Icon: dec = new IconBitmapDecoder(stream, create, cache); break; default: throw new NotImplementedException(); } } else if (obj is Uri) { Uri stream = obj as Uri; switch (enc) { case BitmapEncoding.Bmp: dec = new BmpBitmapDecoder(stream, create, cache); break; case BitmapEncoding.Png: dec = new PngBitmapDecoder(stream, create, cache); break; case BitmapEncoding.Jpg: dec = new JpegBitmapDecoder(stream, create, cache); break; case BitmapEncoding.Tiff: dec = new TiffBitmapDecoder(stream, create, cache); break; case BitmapEncoding.Gif: dec = new GifBitmapDecoder(stream, create, cache); break; case BitmapEncoding.Wmp: dec = new WmpBitmapDecoder(stream, create, cache); break; case BitmapEncoding.Icon: dec = new IconBitmapDecoder(stream, create, cache); break; default: throw new NotImplementedException(); } } else { throw new ArgumentException(); } data = dec.Metadata; return(dec.Frames[0]); }