我想问一下如何从文件加载 Texture2D 图像(我不想使用 XNA 的内容管道)同时保持图像的透明度,因为我想在我的游戏中蒙皮。
我的代码:
使用 _cursor.Texture = _helper.LoadPicture("skin\\cursor.png"); 初始化纹理
使用 spriteBatch.Draw(Texture, rectangle, Color.BlanchedAlmond); 绘制纹理
public Texture2D LoadPicture(string filename) {
FileStream setStream = File.Open(filename, FileMode.Open);
StreamReader reader = new StreamReader(setStream);
Texture2D NewTexture = Texture2D.FromStream(_graphicsDevice, setStream);
setStream.Dispose();
return NewTexture;
}
屏幕截图 1(结果):http://prntscr.com/cxjzwu
屏幕截图 2(预期):http://prntscr.com/cxk065
最佳答案
您也可以从加载中处理它,因此您可以将 BlendState 保留为 AlphaBlend。
这是我用来从文件加载纹理的函数:
private Texture2D PremultiplyTexture(String FilePath, GraphicsDevice device)
{
Texture2D texture;
FileStream titleStream = File.OpenRead(FilePath);
texture = Texture2D.FromStream(device, titleStream);
titleStream.Close();
Color[] buffer = new Color[texture.Width * texture.Height];
texture.GetData(buffer);
for (int i = 0; i < buffer.Length; i++)
buffer[i] = Color.FromNonPremultiplied(buffer[i].R, buffer[i].G, buffer[i].B, buffer[i].A);
texture.SetData(buffer);
return texture;
}
关于c# - 如何使用 Texture2D.FromStream 加载透明 PNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40194266/