使用C#将.tiff文件转换为PNG格式

时间:2019-07-01 07:03:29

标签: c#

如果我使用FromFile

using (System.Drawing.Image imageFile = System.Drawing.Image.FromFile(fileName))

我遇到

错误
  

System.OutOfMemoryException:内存不足。

,如果我使用的是

using (System.Drawing.Image imageFile = System.Drawing.Image.FromStream(stream))

那我就得到

  

System.ArgumentException:参数无效。

,然后该文件损坏。 请帮忙! 这是代码:

public string[] GetPNGFilesFromStream(Stream stream, string destPath)
    {
        string[] pngPaths = null;
        using (FileStream fileStream = new FileStream(destPath, FileMode.Create, FileAccess.Write))
        {
            stream.CopyTo(fileStream);
            fileStream.Close();
            FileInfo finfo = new FileInfo(destPath);
            if (finfo.Extension.ToLower() == ".tiff")
                pngPaths = ConvertTiffToPng(destPath,stream);
        }

        return pngPaths;
    }

    public string[] ConvertTiffToPng(string fileName,Stream stream)
    {
        string test = "";
        using (System.Drawing.Image imageFile = System.Drawing.Image.FromStream(stream))
        {
            FrameDimension frameDimensions = new FrameDimension(imageFile.FrameDimensionsList[0]);
            int frameNum = imageFile.GetFrameCount(frameDimensions);
            string[] pngPaths = new string[frameNum];

            try
            {
                for (int frame = 0; frame < frameNum; frame++)
                {
                    imageFile.SelectActiveFrame(frameDimensions, frame);
                    using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(imageFile))
                    {
                        pngPaths[frame] = String.Format("{0}\\{1}_{2}.png", Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName), frame);
                        bmp.Save(pngPaths[frame], ImageFormat.Png);
                        bmp.Dispose(); //Added 
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            imageFile.Dispose(); 
            return pngPaths;
        }
    }

1 个答案:

答案 0 :(得分:0)

这是我为将tiff图片转换为PNG而创建的空白。我还提供了一个示例,其中包含完整的代码。

无效:

private void tiff2png(string imagepath, string outputpath)
        {
            //using System.Drawing;
            //using System.Drawing.Imaging;
            using (var tiff = new Bitmap(imagepath))
            {
                tiff.Save(outputpath, ImageFormat.Png);
            }
        }

示例:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;

namespace stkover
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            tiff2png(@"C:\Users\user\Desktop\download.tiff", @"C:\Users\user\Desktop\output.png");
        }

        private void tiff2png(string imagepath, string outputpath)
        {
            //using System.Drawing;
            //using System.Drawing.Imaging;
            using (var tiff = new Bitmap(imagepath))
            {
                tiff.Save(outputpath, ImageFormat.Png);
            }
        }
    }
}