Skip to content

Instantly share code, notes, and snippets.

@Azerothian
Created January 16, 2012 07:17
Show Gist options
  • Save Azerothian/1619547 to your computer and use it in GitHub Desktop.
Save Azerothian/1619547 to your computer and use it in GitHub Desktop.
Bitmap To texture
/// <summary>
/// Bitmaps to texture.
/// </summary>
/// <param name="bmp">The BMP.</param>
/// <param name="texture">The texture.</param>
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);
}
/*
* System::Void JointPositionOverlay::copyBitmapToTexture()
{
// Lock the texture buffer so we can write to it
Ogre::HardwarePixelBufferSharedPtr buffer = mTexture->getBuffer ();
buffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
const Ogre::PixelBox &pb = buffer->getCurrentLock();
Ogre::uint32 *texData = static_cast<Ogre::uint32*>(pb.data);
// Lock the bitmap buffer so we can read from it
System::Drawing::Imaging::BitmapData^ bmd =
mBitmap->LockBits(
System::Drawing::Rectangle(0, 0, mBitmap->Width, mBitmap->Height),
System::Drawing::Imaging::ImageLockMode::ReadOnly,
System::Drawing::Imaging::PixelFormat::Format32bppArgb);
Ogre::uint32* mapData = static_cast<Ogre::uint32*>(bmd->Scan0.ToPointer());
// Now copy the data between buffers
size_t height = std::min (pb.getHeight(), (size_t)bmd->Height);
size_t width = std::min(pb.getWidth(), (size_t)bmd->Width);
for(size_t y=0; y<height; ++y)
for(size_t x=0; x<width; ++x)
texData[pb.rowPitch*y + x] = mapData[bmd->Stride/4 * y + x];
// Unlock the buffers again
mBitmap->UnlockBits(bmd);
buffer->unlock();
}
*
*
*
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment