I want to use a Texture2d as a system.drawing.bitmap

后端 未结 2 1878
伪装坚强ぢ
伪装坚强ぢ 2020-12-20 01:29

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

相关标签:
2条回答
  • 2020-12-20 01:53

    You can use two methods that allow you to create a Bitmap, 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 );
    
    0 讨论(0)
  • 2020-12-20 01:56

    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;
    }
    
    0 讨论(0)
提交回复
热议问题