When trying to encode an image in Bmp, it always returns null.
I tried using different values for the quality from 0 to 100, knowing that anyway it does not make sense to have a quality in bmp, I actually thought it would just ignore it.
Here's a sample that returns null.
var bitmap = SKBitmap.Decode(File.OpenRead(filename));
var image = SKImage.FromBitmap(bitmap);
var data = image.Encode(SKEncodedImageFormat.Bmp, 100); // null
Any idea how to make it work? I would like to avoid using the Bitmap.Bytes as the goal is to be able to also use other encoding format.
Thanks
Is it possible to share the image? Also, if you save it as a .png or .jpg, what happens?
I tried with several images as input (bmp, png and jpg) the result is always the same.
After looking a bit more, I'm wondering whether the problem is not directly located inside the native skia library, as SkiaSharp is only binding with SkiaApi.sk_image_encode_specific(...)
Any idea?
What platform was this on? Did you try saving as a .png?
I'm working on OSX and Windows, same behaviour for both.
Here's a summary using different input/ouput:
good: * -> jpg, png
bad: * -> bmp
So any input is working well, however the problem is when trying to encode in bmp. Otherwise it's working.
OK, I will check it out. bmp should work, as it is the most basic, but maybe the encoder is missing for some reason.
Would you like me to create a .net solution that reproduces the problem?
(Forgot to mention earlier that I'm working with .Net Core, but it shouldn't matter as it's using the native binding anyway)
OK... Here is the issue, by default, skia compiles with it's own codecs:
https://github.com/mono/skia/blob/xamarin-mobile-bindings/src/images/SkImageEncoder.cpp
Basically JPG, PNG, WEBP.
There is an option to use the Windows/CoreGraphics encoders, but take a look at this:
https://github.com/mono/skia/blob/xamarin-mobile-bindings/src/ports/SkImageEncoder_WIC.cpp#L54-L63
Now they only do PNG and JPG. Really weird since Windows can do way more:
https://msdn.microsoft.com/en-us/library/windows/desktop/ee690311(v=vs.85).aspx I have no idea why they only have a few, so I asked: https://groups.google.com/forum/#!topic/skia-discuss/a_HNOFtuyWg
Maybe they will have an answer...
I just saw the answer from Hal Canary. So I'm gonna use PNG instead.
Even though the problem is not solved for BMP, I have a pretty good solution instead.
Thanks a lot for your inquiry.
I just had issue where I needed to decode Webp image using SkiaSharp and convert it to Bmp format.
Since SkiaSharp Encode is not supported I used BmpSharp as workaround. Maybe someone will find this useful (I almost created new issue for this.)
~~~csharp
if (!(outputImage.BytesPerPixel == 3 || outputImage.BytesPerPixel == 4))
throw new Exception( $"Unsupported Bits per image ({outputImage.BytesPerPixel}) for BmpSharp" );
var bitsPerPixel = outputImage.BytesPerPixel == 4 ? BmpSharp.BitsPerPixelEnum.RGBA32 : BmpSharp.BitsPerPixelEnum.RGB24;
var bmp = new BmpSharp.Bitmap( outputImage.Width, outputImage.Height, outputImage.Bytes, bitsPerPixel );
return bmp.GetBmpBytes(flipped:true);
~~~
Disclaimer : I'm author of BmpSharp
Most helpful comment
I just had issue where I needed to decode Webp image using SkiaSharp and convert it to Bmp format.
Since SkiaSharp Encode is not supported I used BmpSharp as workaround. Maybe someone will find this useful (I almost created new issue for this.)
~~~csharp
if (!(outputImage.BytesPerPixel == 3 || outputImage.BytesPerPixel == 4))
throw new Exception( $"Unsupported Bits per image ({outputImage.BytesPerPixel}) for BmpSharp" );
var bitsPerPixel = outputImage.BytesPerPixel == 4 ? BmpSharp.BitsPerPixelEnum.RGBA32 : BmpSharp.BitsPerPixelEnum.RGB24;
var bmp = new BmpSharp.Bitmap( outputImage.Width, outputImage.Height, outputImage.Bytes, bitsPerPixel );
return bmp.GetBmpBytes(flipped:true);
~~~
Disclaimer : I'm author of BmpSharp