簡體   English   中英

使用 c# 將 .tiff 文件轉換為 PNG 格式

[英]converting the .tiff file into PNG format using 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;
        }
    }

這是我為將 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);
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM