Is there a fast alternative to creating a Texture2D from a Bitmap object in XNA?

前端 未结 4 1739
终归单人心
终归单人心 2021-02-03 10:36

I\'ve looked around a lot and the only methods I\'ve found for creating a Texture2D from a Bitmap are:

using  (MemoryStream s = new  MemoryStream())
{
   bmp.Sav         


        
4条回答
  •  旧巷少年郎
    2021-02-03 10:59

    I found I had to specify the PixelFormat as .Format32bppArgb when using LockBits as you suggest to grab webcam images.

            BitmapData bmd = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
                System.Drawing.Imaging.ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            int bufferSize = bmd.Height * bmd.Stride;
            //create data buffer 
            byte[] bytes = new byte[bufferSize];
            // copy bitmap data into buffer
            Marshal.Copy(bmd.Scan0, bytes, 0, bytes.Length);
    
            // copy our buffer to the texture
            Texture2D t2d = new Texture2D(_graphics.GraphicsDevice, bmp.Width, bmp.Height, 1, TextureUsage.None, SurfaceFormat.Color);
            t2d.SetData(bytes);
            // unlock the bitmap data
            bmp.UnlockBits(bmd);
            return t2d;
    

提交回复
热议问题