0

If I'm using FromFile

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

i'm getting error of

System.OutOfMemoryException: Out of memory.

and if i'm using the

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

then i'm getting the

System.ArgumentException: Parameter is not valid.

and after that file gets corrupted. please help! this is the code:

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;
        }
    }
  • 3
    _"please help!"_ - Then give us a chance to do so. Start by going through [ask]. A [mcve] would be awesome. But _at least_ we need some code for context and some info about your input. – Fildor Jul 01 '19 at 07:05
  • Oh, and: Welcome to SO! – Fildor Jul 01 '19 at 07:19
  • Does the solution from https://stackoverflow.com/questions/1566188/converting-tiff-files-to-png-in-net works? – xdtTransform Jul 01 '19 at 07:21
  • Currently you are having problems with opening a tiff file. First you might want to have a look at the docs: https://learn.microsoft.com/en-us/dotnet/api/system.drawing.image.fromfile?view=netframework-4.8 (it says such exception might appear when the file format is not supported). And then you can search for similar problems and find a SO answer: https://stackoverflow.com/questions/4265491/to-load-tiff-file-in-c-sharp – Stas Ivanov Jul 01 '19 at 07:22
  • actually, this problem is only for one invoice rest of them is working fine and converting into the png – Vinay Pandey Jul 01 '19 at 07:27
  • `bmp.Dispose(); //Added ` Doesn't this give you a warning? Exiting the `using` block will already dispose of bmp. – Fildor Jul 01 '19 at 07:35
  • `catch (Exception ex) { throw ex; }` this will leave `imageFile` undisposed. You may want to put that into a `finally`. – Fildor Jul 01 '19 at 07:37
  • Back on topic: Can you verify that the problematic tiff is of the same kind of tiff as your other input files? I guess you are expecting a multi-page-tiff. But there are also single-page-tiffs... – Fildor Jul 01 '19 at 07:39
  • Yes, it is the same. – Vinay Pandey Jul 01 '19 at 07:51
  • 1
    If this problem only occurs for 1 file and the rest are working just fine, most likely that single file is either corrupt, or the particular content is not supported by .NET, such as bits-per-pixel, or compression, or whatnot. – Lasse V. Karlsen Jul 01 '19 at 08:12
  • But i have used the same image which was working and change the name of that file but it also didn't work – Vinay Pandey Jul 02 '19 at 10:57

1 Answers1

0

here it is the void I created for convert tiff image to PNG. I also included a sample working full code.

VOID:

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);
            }
        }

SAMPLE:

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);
            }
        }
    }
}
icrescenti
  • 21
  • 3