PresentationCore.dll System.Windows.Media.Imaging BitmapSource.create () returns CachedBitmap

This code works fine with the output of a file called output2.tif when I pass the BitmapSource read from the file.

FileInfo fi = new FileInfo(@"C:\Users\Chris\Downloads\PdfVerificationTests.can_use_image_approval_mode.approved.tiff"); FileStream stream = fi.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None); TiffBitmapDecoder decoder = new TiffBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.None); EncodeImageFromBitmapSource(decoder.Frames[0]); private void EncodeImageFromBitmapSource(BitmapSource theSource) { //Taken from MSDN //http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapencoder(v=vs.110).aspx FileStream stream = new FileStream("output2.tif", FileMode.Create); TiffBitmapEncoder encoder = new TiffBitmapEncoder(); TextBlock myTextBlock = new TextBlock(); myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString(); encoder.Frames.Add((BitmapFrame)theSource); encoder.Save(stream); } 

This code is causing an error

 private void EncodeImageFromArray(byte[] theSource) { //Taken from Stack Overflow. Purported way to create a BitmapSource from a byte array //http://stackoverflow.com/questions/15274699/create-a-bitmapimage-from-a-byte-array/15290190?noredirect=1#comment36991252_15290190 byte[] buffer = new byte[120000]; //changed from "..." Buffer.BlockCopy(theSource, 0, buffer, 0, 120000); //added to populate the array from the parameter var width = 400; // for example 100 changed to 400 var height = 300; // for example 100 changed to 300 (400*300*1 = 120000) var dpiX = 96d; var dpiY = 96d; System.Windows.Media.PixelFormat pixelFormat = System.Windows.Media.PixelFormats.Gray8; // grayscale bitmap ambiguous PixelFormat reference changed var bytesPerPixel = (pixelFormat.BitsPerPixel + 7) / 8; // == 1 in this example var stride = bytesPerPixel * width; // == width in this example BitmapSource bitmap = BitmapSource.Create(width, height, dpiX, dpiY, pixelFormat, null, buffer, stride); //At this point, bitmap should be a BitmapSource?? EncodeImageFromBitmapSource(bitmap); } 

The result of the second function is a runtime error in the encoder.Frames.Add EncodeImageFromBitmapSource line:

 System.InvalidCastException was unhandled HResult=-2147467262 Message=Unable to cast object of type 'System.Windows.Media.Imaging.CachedBitmap' to type 'System.Windows.Media.Imaging.BitmapFrame'. 

So, when is a BitmapSource not a BitmapSource ? When it is created using BitmapSource.create() , obviously. The main object created by BitmapSource.create() is of type CachedBitmap , and the return is passed to BitmapSource . When I use BitmapSource in the encoder.Frames.Add() function, it throws an error. What am I missing?

This is another question, but why does BitmapSource.create() return something that is different from how it is used between the two examples?

0
source share
1 answer
 encoder.Frames.Add((BitmapFrame)theSource); 

the bitmapsource type doesn't seem to just convert to the bitmapFrame type. (BitmapFrame) theSource seems to throw an exception. Try

 BitmapFrame.Create(theSource) 

Link: http://msdn.microsoft.com/en-us/library/ms615993 (v = vs .110) .aspx

+1
source

Source: https://habr.com/ru/post/911666/


All Articles