C#如何转换PNG / JPG到TGA 32位?

时间:2018-10-06 19:28:51

标签: c# image

        private void ConvertButton_Click(object sender, EventArgs e)
        {
          Bitmap original = new Bitmap(@"filepath.cover.png");
          Bitmap clone = new Bitmap(original.Width, original.Height, PixelFormat.Format32bppPArgb);
          using (Graphics conv = Graphics.FromImage(clone))
          {
            conv.DrawImage(original, new Rectangle(0, 0, clone.Width, clone.Height));
          }
        }

大家好,我需要一些帮助。

我试图将PNG或JPEG文件转换为TGA 32位文件并保存,但是我仍然是编程的新手,在这里找不到答案。我在这里只找到了这个代码片段,并试图让他运行,我尝试了很多版本来获取输出文件,但是没有任何效果,有时我得到了一个空白文件,有时是一个损坏的文件

感谢所有帮助我的人。

编辑: 首先,谢谢,我尝试了您的代码,它给了我一个空文件。我只是在尝试:

private void TgaConvert_Click(object sender, EventArgs e)
{
    TGA original = new TGA(@"file.path.cover.png");
    TGA clone = new TGA(original.Width, original.Height, TgaPixelDepth.Bpp32,
    TgaImageType.Uncompressed_TrueColor);

    using (??? conv = ???(clone))
    {
        conv.???(original, new ???(0, 0, clone.Width, clone.Height));
        clone.Save(@"file.path.cover.tga");
    }
}

在带有“ ???”的位置我无能为力了

2 个答案:

答案 0 :(得分:0)

。遗憾的是,.net framefork中不包含对TGA的写入支持。 但是,还有其他可用的开源库。看看Zelenskyi Alexandr(https://github.com/ALEXGREENALEX/TGASharpLib)的TGASharpLib。

如果将他的示例应用于您的代码,则结果如下:

using TGASharpLib;
...

private void ConvertButton_Click(object sender, EventArgs e)
{
    var tga = new TGA(@"filepath.cover.png");
    tga.Save(@"filepath.cover.tga");        
}

答案 1 :(得分:0)

我做到了:

using TGASharpLib;
....
TGA T;
private void ConvertButton_Click(object sender, EventArgs e)
    {
        using (Bitmap original = new Bitmap("file.path.jpg"))
        using (Bitmap clone = new Bitmap(original))
        using (Bitmap newbmp = clone.Clone(new Rectangle(0, 0, clone.Width, clone.Height), PixelFormat.Format32bppArgb))
        T = (TGA)newbmp;
        T.Save("file.path.cover.tga");
    }
相关问题