2017-12-29 58 views
0

KBitmap.Bytes는 Marshal에 대한 제안 사항입니다. 바이트 배열을 SKBitmap에 복사 하시겠습니까? 아래 코드를 사용하고 있지만 작동하지 않습니다.SkiaSharp에서 바이트 배열을 SKBitmap으로 변환하는 방법은 무엇입니까?

코드 스 니펫 : 당신은 항상 비트 맵 관리되지 않는/기본 메모리의 삶과 바이트 배열이 관리되는 코드에서와 같이 일부 마샬링을해야 할 것

SKBitmap bitmap = new SKBitmap((int)Width, (int)Height); 
    bitmap.LockPixels(); 
    byte[] array = new byte[bitmap.RowBytes * bitmap.Height]; 
    for (int i = 0; i < pixelArray.Length; i++) 
    { 
     SKColor color = new SKColor((uint)pixelArray[i]); 
     int num = i % (int)Width; 
     int num2 = i/(int)Width; 
     array[bitmap.RowBytes * num2 + 4 * num] = color.Blue; 
     array[bitmap.RowBytes * num2 + 4 * num + 1] = color.Green; 
     array[bitmap.RowBytes * num2 + 4 * num + 2] = color.Red; 
     array[bitmap.RowBytes * num2 + 4 * num + 3] = color.Alpha; 
    } 
    Marshal.Copy(array, 0, bitmap.Handle, array.Length); 
    bitmap.UnlockPixels(); 

답변

0

. 그러나, 당신은 같은 것을 할 수 있습니다 :

// 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에 의해 해제 될 수 있도록 고정 된 메모리가 사용 후 고정 해제하는 것이 필수적이다.)

또한 여기 : https://github.com/mono/SkiaSharp/issues/416