private static Texture2D CreateTexture2DFromBitmap(Bitmap bitmap, TextureFormat format, bool generateMipmaps, TextureTarget textureTarget) { using (WritePixelBuffer pixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream, BitmapAlgorithms.SizeOfPixelsInBytes(bitmap))) { pixelBuffer.CopyFromBitmap(bitmap); Texture2DDescription description = new Texture2DDescription(bitmap.Width, bitmap.Height, format, generateMipmaps); Texture2D texture = new Texture2DGL3x(description, textureTarget); texture.CopyFromBuffer(pixelBuffer, TextureUtility.ImagingPixelFormatToImageFormat(bitmap.PixelFormat), TextureUtility.ImagingPixelFormatToDatatype(bitmap.PixelFormat)); return(texture); } }
public void WritePixelBufferBitmap() { using (Bitmap bitmap = new Bitmap(1, 1)) using (GraphicsWindow window = Device.CreateWindow(1, 1)) using (WritePixelBuffer pixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream, BitmapAlgorithms.SizeOfPixelsInBytes(bitmap))) { Color color = Color.FromArgb(0, 1, 2, 3); bitmap.SetPixel(0, 0, color); pixelBuffer.CopyFromBitmap(bitmap); // // Verify read back - comes back BGRA // BlittableBGRA[] readBackPixel = pixelBuffer.CopyToSystemMemory <BlittableBGRA>(); Assert.AreEqual(color.R, readBackPixel[0].R); Assert.AreEqual(color.G, readBackPixel[0].G); Assert.AreEqual(color.B, readBackPixel[0].B); Assert.AreEqual(color.A, readBackPixel[0].A); // // Verify read back into bitmap // using (Bitmap readBackBitmap = pixelBuffer.CopyToBitmap(1, 1, PixelFormat.Format32bppArgb)) { Assert.AreEqual(color, readBackBitmap.GetPixel(0, 0)); } } }