Forum Libraries, Components, and Plugins


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Fast way to decode SKBitmap from byte[] (JPEG Raw)

RouvenRouven DEMember ✭✭

Hallo Zusammen,

you might guess, i´m interested in decoding raw jpeg data, i got in an byte[], to a SKBitmap.

I currently use the SKBitmap.Decode like :

bitmap = SKBitmap.Decode(buffer);

This does work fine and is gorgeous simple, but i´d like to have more performance. It´s not cirtical for smaller Images. But with larger Images like 800*1400, the decoding needs something between 30 and 100 ms on a faster device like the Samsung S7.
Do you know a better way to decode, when i exactly know the dimensions, colordepth and used jpeg codec ?

thanks in advance

Okay, i checked the SKCodec, but it does not give my any Advantage, when i try to decode a larger Image at once.
I tried something like this:

using (SKData data = new SKData(buffer))
    using (SKCodec codec = SKCodec.Create(data))
        var info = codec.Info;
        bitmap = new SKBitmap(info.Width, info.Height, info.ColorType, info.IsOpaque ? SKAlphaType.Opaque : SKAlphaType.Premul);
        codec.GetPixels(bitmap.Info, bitmap.GetPixels());

I guess, under the hood, it´s pretty much the same like:

bitmap = SKBitmap.Decode(buffer); 

Any ideas, what i can do, to optimize decoding ?

Best Answer


  • RouvenRouven DEMember ✭✭

    Thank you, this is giving another option, to check out.

  • I'm having the same problem with slow loading.
    SKCodec.GetPixels seems to be the one causing the long load time.

            animatedImage tempAnimatedImage = new animatedImage();
            Assembly assembly = GetType().GetTypeInfo().Assembly;
                using (Stream stream = assembly.GetManifestResourceStream(gifName))
                using (SKManagedStream skStream = new SKManagedStream(stream))
                using (SKCodec codec = SKCodec.Create(skStream))
                    tempAnimatedImage.totalFrames = codec.FrameCount;
                    tempAnimatedImage.animatedImages = new SKBitmap[tempAnimatedImage.totalFrames];
                    tempAnimatedImage.frameSpeed = new int[tempAnimatedImage.totalFrames];
                    tempAnimatedImage.numOfRepeats = codec.RepetitionCount;
                    for (int frame = 0; frame < tempAnimatedImage.totalFrames; frame++)
                        tempAnimatedImage.frameSpeed[frame] = codec.FrameInfo[frame].Duration;
                        SKImageInfo imageInfo = new SKImageInfo(codec.Info.Width, codec.Info.Height);
                        tempAnimatedImage.animatedImages[frame] = new SKBitmap(imageInfo);
                          IntPtr pointer = tempAnimatedImage.animatedImages[frame].GetPixels();
                        SKCodecOptions codecOptions = new SKCodecOptions(frame);
                        codec.GetPixels(imageInfo, pointer, codecOptions);
                return tempAnimatedImage;
            catch { }
            return GIFerror;
Sign In or Register to comment.