I want to use Texture2d as system.drawing.bitmap

XNA.Texture2D to System.Drawing.Bitmap I'm sure this answered my question, but linked an external site and is no longer available.

I use the window shape in xna game. I want to use a background image for one of my panels. It is easy enough to download from a file, but when the game is deployed to another system, it is obvious that the file location will be different.

Bitmap bmp = new Bitmap(@"c:\myImage.png"); 

In the above question, someone suggested usign Texture2d.saveToPng, then open the bitmap from the memory stream. It sounds great if someone can direct me in that direction. Any other ideas?

+2
source share
2 answers

You can use two methods that allow you to create bitmap images, use Texture2D.SaveAsJpeg() or Texture2D.SaveAsPng() .

 MemoryStream memoryStream = new MemoryStream(); Texture2D texture = Content.Load<Texture2D>( "Images\\test" ); texture.SaveAsJpeg( memoryStream, texture.Width, texture.Height ); //Or SaveAsPng( memoryStream, texture.Width, texture.Height ) System.Drawing.Bitmap bmp = new System.Drawing.Bitmap( memoryStream ); 
+1
source

This works for me. If there is a problem with this, please let me know.

 public static Image Texture2Image(Texture2D texture) { Image img; using (MemoryStream MS = new MemoryStream()) { texture.SaveAsPng(MS, texture.Width, texture.Height); //Go To the beginning of the stream. MS.Seek(0, SeekOrigin.Begin); //Create the image based on the stream. img = Bitmap.FromStream(MS); } return img; } 
+4
source

Source: https://habr.com/ru/post/1442867/


All Articles