2017-12-29 18 views
0

KBitmap.Bytes ist schreibgeschützt, jeder Vorschlag, wie Marshal.Copy ein Byte-Array auf die SKBitmap? Ich benutze die Verwendung des Code-Snippets, aber es funktioniert nicht.Wie konvertiert man Byte-Array zu SKBitmap in SkiaSharp?

Code-Snippet:

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(); 

Antwort

0

Sie werden immer etwas Serialisieren als Bitmap Leben in unmanaged/native Speicher und das Byte-Array ist in verwaltetem Code zu tun haben. Aber können Sie in der Lage sein, so etwas zu tun:

// 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); 

Diese Stifte den verwalteten Speicher und übergibt den Zeiger auf die Bitmap. Auf diese Weise greifen beide auf die gleichen Speicherdaten zu und es ist nicht notwendig, tatsächlich Konvertierungen (oder Kopieren) durchzuführen. (Es ist wichtig, dass der festgelegte Speicher nach Gebrauch nicht fixiert sein, so dass der Speicher kann durch den GC befreit werden.)

Auch hier: https://github.com/mono/SkiaSharp/issues/416

Verwandte Themen