Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

c# - I want to use a Texture2d as a system.drawing.bitmap

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

I am using a windows form in an xna game. I want to use a background image for one of my panels. It is easy enough to do the loading from file, but when the game is deployed to another system obviously the file location will be different.

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

In the above mentioned question, someone had suggested usign Texture2d.saveToPng then open the bitmap from the memory stream. This sounds great, if someone could steer me in that direction. Any other ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This works for me. If there are problems with it 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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...