求助,有关byte转Texture2D的问题,求大神指点
答案:2 悬赏:0 手机版
解决时间 2021-02-14 06:23
- 提问者网友:献世佛
- 2021-02-13 12:39
求助,有关byte转Texture2D的问题,求大神指点
最佳答案
- 五星知识达人网友:归鹤鸣
- 2021-02-13 14:16
Texture2D toBitmap 的方法
[csharp] view plaincopy
public static Bitmap FastTextureToBitmap(Texture2D texture)
{
// Setup pointer back to bitmap
Bitmap newBitmap = new Bitmap(texture.Width, texture.Height);
// Get color data from the texture
Microsoft.Xna.Framework.Graphics.Color[ textureColors = GetColorDataFromTexture(texture);
System.Drawing.Imaging.BitmapData bmpData = newBitmap.LockBits(new System.Drawing.Rectangle(0, 0, newBitmap.Width, newBitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
// Loop through pixels and set values
unsafe
{
byte* bmpPointer = (byte*)bmpData.Scan0;
for (int y = 0; y < texture.Height; y++)
{
for (int x = 0; x < texture.Width; x++)
{
bmpPointer[0] = textureColors[x + y * texture.Width].B;
bmpPointer[1] = textureColors[x + y * texture.Width].G;
bmpPointer[2] = textureColors[x + y * texture.Width].R;
bmpPointer[3] = textureColors[x + y * texture.Width].A;
bmpPointer += 4;
}
bmpPointer += bmpData.Stride - (bmpData.Width * 4);
}
}
textureColors = null;
newBitmap.UnlockBits(bmpData);
return newBitmap;
}
bitmap to texture2D 的方法:
[csharp] view plaincopy
private Texture2D GetTexture(GraphicsDevice dev, System.Drawing.Bitmap bmp)
{
int[] imgData = new int[bmp.Width * bmp.Height];
Texture2D texture = new Texture2D(dev, bmp.Width, bmp.Height);
unsafe
{
// lock bitmap
System.Drawing.Imaging.BitmapData origdata =
bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
uint* byteData = (uint*)origdata.Scan0;
// Switch bgra -> rgba
for (int i = 0; i < imgData.Length; i++)
{
byteData[i] = (byteData[i] & 0x000000ff) << 16 | (byteData[i] & 0x0000FF00) | (byteData[i] & 0x00FF0000) >> 16 | (byteData[i] & 0xFF000000);
}
// copy data
System.Runtime.InteropServices.Marshal.Copy(origdata.Scan0, imgData, 0, bmp.Width * bmp.Height);
byteData = null;
// unlock bitmap
bmp.UnlockBits(origdata);
}
texture.SetData(imgData);
return texture;
}
[csharp] view plaincopy
public static Bitmap FastTextureToBitmap(Texture2D texture)
{
// Setup pointer back to bitmap
Bitmap newBitmap = new Bitmap(texture.Width, texture.Height);
// Get color data from the texture
Microsoft.Xna.Framework.Graphics.Color[ textureColors = GetColorDataFromTexture(texture);
System.Drawing.Imaging.BitmapData bmpData = newBitmap.LockBits(new System.Drawing.Rectangle(0, 0, newBitmap.Width, newBitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
// Loop through pixels and set values
unsafe
{
byte* bmpPointer = (byte*)bmpData.Scan0;
for (int y = 0; y < texture.Height; y++)
{
for (int x = 0; x < texture.Width; x++)
{
bmpPointer[0] = textureColors[x + y * texture.Width].B;
bmpPointer[1] = textureColors[x + y * texture.Width].G;
bmpPointer[2] = textureColors[x + y * texture.Width].R;
bmpPointer[3] = textureColors[x + y * texture.Width].A;
bmpPointer += 4;
}
bmpPointer += bmpData.Stride - (bmpData.Width * 4);
}
}
textureColors = null;
newBitmap.UnlockBits(bmpData);
return newBitmap;
}
bitmap to texture2D 的方法:
[csharp] view plaincopy
private Texture2D GetTexture(GraphicsDevice dev, System.Drawing.Bitmap bmp)
{
int[] imgData = new int[bmp.Width * bmp.Height];
Texture2D texture = new Texture2D(dev, bmp.Width, bmp.Height);
unsafe
{
// lock bitmap
System.Drawing.Imaging.BitmapData origdata =
bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
uint* byteData = (uint*)origdata.Scan0;
// Switch bgra -> rgba
for (int i = 0; i < imgData.Length; i++)
{
byteData[i] = (byteData[i] & 0x000000ff) << 16 | (byteData[i] & 0x0000FF00) | (byteData[i] & 0x00FF0000) >> 16 | (byteData[i] & 0xFF000000);
}
// copy data
System.Runtime.InteropServices.Marshal.Copy(origdata.Scan0, imgData, 0, bmp.Width * bmp.Height);
byteData = null;
// unlock bitmap
bmp.UnlockBits(origdata);
}
texture.SetData(imgData);
return texture;
}
全部回答
- 1楼网友:三千妖杀
- 2021-02-13 15:39
texture2d tobitmap 的方法 [csharp] view plaincopy public static bitmap fasttexturetobitmap(texture2d texture) { // setup pointer back to bitmap bitmap newbitmap = new bitmap(texture.width, texture.height); // get color data fro...
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯