如何将一个字节阵列转换为Skiasharp中的SkbitMap? 仅读取有关如何元帅的任何建议。我正在使用以下代码段,但不起作用。 代码段: skbitmap bitmap = new SkbitMap((...

问题描述 投票:0回答:2

您将始终必须进行一些编组,因为位图生活在不受管理的/本机内存中,并且字节阵列在托管代码中。但是,您也许可以做这样的事情:

// 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);
这将托管内存固定并将指针传递给位图。这样,两者都在访问相同的内存数据,并且无需实际进行任何转换(或复制)。 (在使用后不限制固定的内存是至关重要的,以便可以通过GC释放内存。)
.net-core asp.net-core-mvc 2d .net-standard skiasharp
2个回答
14
投票
Https://github.com/mono/skiasharp/issues/416


private SKBitmap CreateImage(byte[] imageBuffer, int width, int height)

{ //从编码的条形码缓冲区创建SKBITMAP skbitmap bitmap = new skbitmap(宽度,高度,skcolortype.rgba88888,skalphatype.premul); // Lock the bitmap's pixels IntPtr pixels = bitmap.GetPixels(); int buffSize = bitmap.Height * bitmap.RowBytes; byte[] pixelBuffer = new byte[buffSize]; int i = 0; int x = 0; int padding = bitmap.RowBytes - (4 * width); // Copy native pixel buffer into managed buffer, one scan line at a time for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { pixelBuffer[x++] = imageBuffer[i++]; pixelBuffer[x++] = imageBuffer[i++]; pixelBuffer[x++] = imageBuffer[i++]; pixelBuffer[x++] = 255; } x += padding; } // Copy the managed buffer to the bitmap System.Runtime.InteropServices.Marshal.Copy(pixelBuffer, 0, pixels, buffSize);


0
投票
	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.