如何在 C# 中加载 PNG 并导出到 TGA 并保持 alpha?
我正在编写一个工具来自动化我们为游戏制作的一些资产。我想要做的是获取 PNG 文件的文件夹,将它们组合成纹理图集,然后将图集导出为 TGA,并将 UV 坐标导出为 XML。
我不确定应该使用哪种方法在 C# 中加载 PNG 文件,因为似乎有几种方法。在 C# 中加载图像的推荐方法是什么,该方法可以访问颜色/alpha 数据,以便我可以将其提取到 TGA?
我也已经有了 C++ 中的 TGA 创建代码,我计划将其迁移到 C#,但我想知道 .Net 中是否已有任何可用于创建/保存 TGA 的内容?
感谢您的阅读。
I'm writing a tool to automate some of our asset making for a game. What I want to do is take a folder of PNG files, combine them into a texture atlas and then export the atlas as a TGA and the UV coords to XML.
I'm not sure which method I should use to load the PNG files in C# as there seem to be several. What is the recommended method to load images in C# that gives access to the colour/alpha data so I can extract it to the TGA?
I also already have TGA creation code in C++ which I plan to move to C# but I'm wondering if there is anything already available in .Net to create/save TGAs?
Thanks for reading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 PNG 文件加载到 .Net 位图中很简单:
加载位图后,您可以使用位图的 LockBits 方法(StackOverflow 上有许多 LockBits 代码示例)最有效地访问其所有信息(包括 Alpha 通道信息)。
更新:这是一个代码示例,展示了如何使用 LockBits 逐像素访问位图的数据:
您需要为您的项目设置“允许不安全代码”编译器选项才能使用此代码。您还可以使用位图的 GetPixel(x, y) 方法,但这非常慢。
Loading a PNG file into a .Net Bitmap is easy:
Once you have the Bitmap loaded, you can access all of its info (including alpha channel info) most efficiently using the Bitmap's LockBits method (there are many LockBits code samples on StackOverflow).
Update: here's a code sample that shows how to use LockBits to access the Bitmap's data pixel-by-pixel:
You need to set the "Allow unsafe code" compiler option for your project to use this code. You could also use the GetPixel(x, y) method of the Bitmap, but this is amazingly slow.
我没有代码示例,但我可以为您提供
Targa 格式
您想使用调色板吗?由于您制作了游戏,我建议您计算位图的调色板并将其放入 targa 中以减少文件大小。
哦,在我忘记之前,.NET 不使用 RGBA,而是使用 BGRA 像素。别问我为什么,但就是这样。
I dont have a code sample, but i can give you a guideline
Targa format
Do you want to use a palette ? Since your making a game, i would recommend you compute a palette for your bitmaps and put that into the targa to reduce the file size.
Oh , before i forget, .NET doesnt use RGBA, instead, the pixels are BGRA. Dont ask me why, but its like that.