Last active
June 18, 2017 09:01
-
-
Save NickDiMucci/836618c0ae022a739235 to your computer and use it in GitHub Desktop.
Get a Texture2D from a Bitmap image.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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