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 byte array to SKBitmap in SkiaSharp? #416
Comments
I was just trying to solve the same problem, it worked for me by using |
@xPaw does retrieving the bitmap pointer by using bitmap.GetPixels() is working properly to copy the byte array to bitmap ? |
It worked for me. |
You will always have to do some marshaling as the bitmap lives in unmanaged/native memory and the byte array is in managed code. But, you may be able to do something like this: // the pixel array of uint 32-bit colors
var pixelArray = new uint[] {
0xFFFF0000, 0xFF00FF00,
0xFF0000FF, 0xFFFFFF00
};
// create an empty bitmap
bitmap = new SKBitmap();
// pin the managed array so that the GC doesn't move it
var gcHandle = GCHandle.Alloc(pixelArray, GCHandleType.Pinned);
// install the pixels with the color type of the pixel data
var info = new SKImageInfo(2, 2, SKImageInfo.PlatformColorType, SKAlphaType.Unpremul);
bitmap.InstallPixels(info, gcHandle.AddrOfPinnedObject(), info.RowBytes, null, delegate { gcHandle.Free(); }, null); This pins the managed memory and passes the pointer to the bitmap. This way, both are accessing the same memory data and there is no need to actually do any conversions (or copying). (It is essential that the pinned memory be unpinned after usage so that the memory can be freed by the GC.) |
SKBitmap.Bytes is read only, any suggestion on how to Marshal.Copy a byte array to the SKBitmap? I am using using below code snippet but it not working.
Code snippet:
The text was updated successfully, but these errors were encountered: