SKImage.Encode in Bmp returns null

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
dsorondacommented, Feb 4, 2020

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.)

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

1reaction
ThmXcommented, Jun 27, 2017

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SkiaSharp SkData is null after Encoding Image
ico the Encoded method returns a null SkData which naturally results in skData. AsStream() exploding in a NullReferenceException. var data = ...
Read more >
SKImage.Encode Method (SkiaSharp)
Returns the SKData wrapping the encoded image. Remarks. The quality is a suggestion, and not all formats (for example, PNG) respect or support...
Read more >
SkImage Class Reference - Skia
If the image does not already has its contents in encoded form, return NULL. Note: to force the image to return its contents...
Read more >
SkImage Class Reference
Returns original SkImage if decoded in raster bitmap, or if encoded in a stream. Returns nullptr if backed by GPU texture and copy...
Read more >
include/core/SkImage.h - skia
SkImage is returned if bitmap is valid. Valid SkBitmap parameters include: ... static sk_sp<SkImage> DecodeToRaster(const void* encoded, size_t length,.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found