Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to convert ImageData to SkiaSharp.SkiBitmap #123

Open
SamGuoMsft opened this issue Sep 14, 2022 · 1 comment
Open

How to convert ImageData to SkiaSharp.SkiBitmap #123

SamGuoMsft opened this issue Sep 14, 2022 · 1 comment

Comments

@SamGuoMsft
Copy link

Try to convert How to convert ImageData to SkiaSharp.SkiBitmap by using SKBitmap.Decode(imageData.Data, new SKImageInfo { Height = imageData.ImageSize.Height, Width = imageData.ImageSize.Width }), but it keep failing.

Does anyone know how to convert ImageData to SkiBitmap?

@hey-red
Copy link
Contributor

hey-red commented Sep 14, 2022

Here is example. It's is not optimized, but should work.

using System.Runtime.InteropServices;

using FFMediaToolkit.Decoding;
using FFMediaToolkit.Graphics;

using SkiaSharp;

var file = MediaFile.Open(@"/path/to/file.mp4", new MediaOptions
{
    VideoPixelFormat = ImagePixelFormat.Rgba32 // Set skia supported pix format
});

if (file.Video.TryGetNextFrame(out var imageData))
{
    var gcHandle = GCHandle.Alloc(imageData.Data.ToArray(), GCHandleType.Pinned);

    using var bitmap = new SKBitmap();
    var imageInfo = new SKImageInfo(imageData.ImageSize.Width, imageData.ImageSize.Height, SKColorType.Rgba8888, SKAlphaType.Unpremul);

    bitmap.InstallPixels(imageInfo, gcHandle.AddrOfPinnedObject(), imageInfo.RowBytes, delegate { gcHandle.Free(); });

    // Save to file
    using var fs = File.OpenWrite(@"/path/to/image.png");
    bitmap.Encode(fs, SKEncodedImageFormat.Png, 100);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants