Skip to content

Instantly share code, notes, and snippets.

@Azerothian
Created January 20, 2012 04:03
Show Gist options
  • Save Azerothian/1645029 to your computer and use it in GitHub Desktop.
Save Azerothian/1645029 to your computer and use it in GitHub Desktop.
Bitmap To texture
public unsafe static void BitmapToTexture(Bitmap bitmap, Texture texture)
{
var bmp = bitmap.Clone(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.PixelFormat.Format32bppArgb);
var bitmapData = bmp.LockBits(
new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadOnly,
bmp.PixelFormat);
HardwarePixelBuffer buffer = texture.GetBuffer();
buffer.Lock(Axiom.Graphics.BufferLocking.Discard);
PixelBox pixelBox = buffer.CurrentLock;
UInt32* texData = (UInt32*)pixelBox.Data;
UInt32* mapData = (UInt32*)bitmapData.Scan0;
var height = Math.Min(pixelBox.Height, bmp.Height);
var width = Math.Min(pixelBox.Width, bmp.Width);
for (var y = 0; y < height; ++y)
for (var x = 0; x < width; ++x)
texData[pixelBox.RowPitch * y + x] = mapData[bitmapData.Stride / 4 * y + x];
buffer.Unlock();
bmp.UnlockBits(bitmapData);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment