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