I am trying to use BMPBitMapDecoder to read .bmp files in a directory. But, no matter what I try, I get “codec cannot use the type of stream/URI provided” exception.
Things I have tried so far:
1) Following Documentation Of Microsoft To Use URI: I have followed the uri example and wrote something like this:
myPath = "C:\MyImages\";
imagesInDir = Directory.GetFiles(myPath, "*.bmp", SearchOption.TopDirectoryOnly).ToList();
Uri imgURI = new Uri(imagesInDir[0]);
BmpBitmapDecoder bmpImgDec = new BmpBitmapDecoder(imgURI, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bmpSource = bmpImgDec.Frames[0];
Result: I got the “codec cannot use the type of URI provided” exception.
2) Following Documentation Of Microsoft To Use File Stream: I have followed the file stream example and wrote something like this:
myPath = "C:\MyImages\";
imagesInDir = Directory.GetFiles(myPath, "*.bmp", SearchOption.TopDirectoryOnly).ToList();
FileStream imgFS = new FileStream(imagesInDir[0], FileMode.Open, FileAccess.Read, FileShare.Read);
BmpBitmapDecoder bmpImgDec = new BmpBitmapDecoder(imgFS, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bmpSource = bmpImgDec.Frames[0];
Result: I got the “codec cannot use the type of stream provided” exception.
3) Using Block To Use Stream: I wrote something like this:
myPath = "C:\MyImages\";
imagesInDir = Directory.GetFiles(myPath, "*.bmp", SearchOption.TopDirectoryOnly).ToList();
using (Stream imgFS = new FileStream(imagesInDir[0], FileMode.Open, FileAccess.Read, FileShare.Read)){
BmpBitmapDecoder bmpImgDec = new BmpBitmapDecoder(imgFS, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bmpSource = bmpImgDec.Frames[0];
}
Result: I got the “codec cannot use the type of stream provided” exception.
4) Using BitmapDecoder Instead Of BmpBitmapDecoder: I have tried that both when I did follow Microsoft’s documentation and when I wrote a using block. Instead of using constructor of BmpBitMapDecoder, I used Create method of BitmapDecoder.
Result: This time, first, it complained about not being able to implicitly convert between BmpBitMapDecoder and BitMapDecoder. So I used type casting: “(BmpBitmapDecoder)”. When I added casting however, it gave me another exception about not being able to convert between PngBitmapDecoder and BmpBitmapDecoder.