I have developed a full-trust browser hosted application to display images contained in a specific folder. This application uses only classes from System.Windows.Media.Imaging namespace.
I have some big images (A1/A0 scanned at 600 DPI) stored in JPEG or uncompressed TIFF format. For those images the following code behaves very strange.
Stream imageStreamSource = null; try { FileInfo fi = (FileInfo)lbFiles.SelectedItem; BitmapDecoder decoder = null; imageStreamSource = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read); if (fi.Name.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase)) decoder = new JpegBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None); else if (fi.Name.EndsWith(".tif", StringComparison.InvariantCultureIgnoreCase)) decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None); else if (fi.Name.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase)) decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None); else if (fi.Name.EndsWith(".bmp", StringComparison.InvariantCultureIgnoreCase)) decoder = new BmpBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None); ImageSource = decoder.Frames[0]; ImageSource.Freeze(); } catch (Exception ex) { MessageBox.Show(ex.Message, "WpfBrowserApplication1", MessageBoxButton.OK, MessageBoxImage.Error); return; } MessageBox.Show("Image loaded!", "WpfBrowserApplication1", MessageBoxButton.OK, MessageBoxImage.Information);
The image is loaded but the BitmapSource object does not contain any pixel data. I get information about the image resolution, dimensions and color format, but no pixels.
If I try to convert the image to another format, lets say from JPEG to BMP, the file that is generated has only 1 KB. The code used for conversion is as following:
FileInfo fi = (FileInfo)lbFiles.SelectedItem; string sDestFormat = (string)((ComboBoxItem)cmbDestinationFormat.SelectedValue).Content; String outputFile = String.Format("{0}\\{1}{2}", fi.Directory.FullName, fi.Name, sDestFormat); try { BitmapEncoder encoder = null; using (FileStream outputStream = new FileStream(outputFile, FileMode.Create)) { switch (sDestFormat) { case ".TIF": encoder = new TiffBitmapEncoder(); ((TiffBitmapEncoder)encoder).Compression = TiffCompressOption.Default; break; case ".JPG": encoder = new JpegBitmapEncoder(); break; case ".PNG": encoder = new PngBitmapEncoder(); break; case ".BMP": encoder = new BmpBitmapEncoder(); break; } encoder.Frames.Add(BitmapFrame.Create(ImageSource)); encoder.Save(outputStream); outputStream.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "WpfBrowserApplication1", MessageBoxButton.OK, MessageBoxImage.Error); return; } MessageBox.Show(String.Format("Image saved into file '{0}'", outputFile), "WpfBrowserApplication1", MessageBoxButton.OK, MessageBoxImage.Information);
I have uploaded the complete source code for this application here. Just download the archive WpfBrowserApplication1.zip.
I have uploaded some images causing this problem here.
Cosmin Petrenciuc