如何在 C# 中加载 PNG 并导出到 TGA 并保持 alpha?

发布于 2024-08-06 02:58:23 字数 288 浏览 4 评论 0原文

我正在编写一个工具来自动化我们为游戏制作的一些资产。我想要做的是获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黄昏下泛黄的笔记 2024-08-13 02:58:23

将 PNG 文件加载到 .Net 位图中很简单:

Bitmap bmp = (Bitmap)Bitmap.FromFile("c:\wherever\whatever.png");
// yes, the (Bitmap) cast is necessary. Don't ask me why.

加载位图后,您可以使用位图的 LockBits 方法(StackOverflow 上有许多 LockBits 代码示例)最有效地访问其所有信息(包括 Alpha 通道信息)。

更新:这是一个代码示例,展示了如何使用 LockBits 逐像素访问位图的数据:

System.Drawing.Imaging.BitmapData data =
    bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
    System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
unsafe
{
    // important to use the BitmapData object's Width and Height
    // properties instead of the Bitmap's.
    for (int x = 0; x < data.Width; x++)
    {
        int columnOffset = x * 4;
        for (int y = 0; y < data.Height; y++)
        {
            byte* row = (byte*)data.Scan0 + (y * data.Stride);
            byte B = row[columnOffset];
            byte G = row[columnOffset + 1];
            byte R = row[columnOffset + 2];
            byte alpha = row[columnOffset + 3];
        }
    }
}
bmp.UnlockBits(data);

您需要为您的项目设置“允许不安全代码”编译器选项才能使用此代码。您还可以使用位图的 GetPixel(x, y) 方法,但这非常慢。

Loading a PNG file into a .Net Bitmap is easy:

Bitmap bmp = (Bitmap)Bitmap.FromFile("c:\wherever\whatever.png");
// yes, the (Bitmap) cast is necessary. Don't ask me why.

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:

System.Drawing.Imaging.BitmapData data =
    bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
    System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
unsafe
{
    // important to use the BitmapData object's Width and Height
    // properties instead of the Bitmap's.
    for (int x = 0; x < data.Width; x++)
    {
        int columnOffset = x * 4;
        for (int y = 0; y < data.Height; y++)
        {
            byte* row = (byte*)data.Scan0 + (y * data.Stride);
            byte B = row[columnOffset];
            byte G = row[columnOffset + 1];
            byte R = row[columnOffset + 2];
            byte alpha = row[columnOffset + 3];
        }
    }
}
bmp.UnlockBits(data);

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.

記柔刀 2024-08-13 02:58:23

我没有代码示例,但我可以为您提供

  1. 使用 Image.FromFile() 加载 PNG 的指南。 .NET 支持加载 PNG
  2. 打开 targa 的文件句柄。创建一个 Xml 文档。 .NET不支持targa,所以你必须手动编写它。
  3. 锁定位图以获取像素。 GetPixel() 非常慢。我认为该方法名为 LockBits()。您将获得一个指向表面的指针,以读取
  4. 写入 targa 的像素。 Targa 是一种容器格式,因此任何位图都应该适合。
  5. 将 UV 保存为 Xml。

Targa 格式

您想使用调色板吗?由于您制作了游戏,我建议您计算位图的调色板并将其放入 targa 中以减少文件大小。

哦,在我忘记之前,.NET 不使用 RGBA,而是使用 BGRA 像素。别问我为什么,但就是这样。

I dont have a code sample, but i can give you a guideline

  1. Load PNG using Image.FromFile(). .NET supports loading PNG
  2. Open a file handle to your targa. Create an XmlDocument. .NET doesnt support targa, so you have to manually write it.
  3. Lock the bitmap to get at the pixels. GetPixel() is very slow. I think the method is named LockBits(). You get a pointer to the surface to read the pixels
  4. Write to targa. Targa is a container format, so any bitmap should fit.
  5. Save the UV as Xml.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文