Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@NickDiMucci
Last active June 18, 2017 09:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickDiMucci/836618c0ae022a739235 to your computer and use it in GitHub Desktop.
Save NickDiMucci/836618c0ae022a739235 to your computer and use it in GitHub Desktop.
Get a Texture2D from a Bitmap image.
public static Texture2D BitmapToTexture2D(GraphicsDevice GraphicsDevice, System.Drawing.Bitmap image)
{
// Buffer size is size of color array multiplied by 4 because
// each pixel has four color bytes
int bufferSize = image.Height * image.Width * 4;
// Create new memory stream and save image to stream so
// we don't have to save and read file
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bufferSize);
image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
// Creates a texture from IO.Stream - our memory stream
Texture2D texture = Texture2D.FromStream(GraphicsDevice, memoryStream);
return texture;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment