我想使用Texture2d作为system.drawing.bitmap

Gen*_*rey 2 c# xna

XNA.Texture2D到System.Drawing.Bitmap我确信这回答了我的问题,但它链接了一个外部网站,不再可用.

我在xna游戏中使用Windows窗体.我想为我的一个面板使用背景图像.从文件加载很容易,但是当游戏部署到另一个系统时,文件位置显然会有所不同.

Bitmap bmp = new Bitmap(@"c:\myImage.png");
Run Code Online (Sandbox Code Playgroud)

在上面提到的问题中,有人建议使用Texture2d.saveToPng,然后从内存流中打开位图.这听起来不错,如果有人可以引导我朝这个方向发展.还有其他想法吗?

Gen*_*rey 5

这适合我.如果有问题请告诉我.

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;
}
Run Code Online (Sandbox Code Playgroud)