T O P I C R E V I E W |
pcmueller77 |
Posted - Dec 08 2023 : 07:33:55 Hi, is there a faster way to put 50 images into one tiff file? it should be gray, good in quality and small. this is the c# code for to this. Any suggestions to get this fast and keep the same file as a result are welcome! Thanks & with best regards, Thomas.
using HiComponents.IEvolution;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static HiComponents.IEvolution.IOParams;
using static System.Net.Mime.MediaTypeNames;
namespace IEvolution_gray_tif_fails
{
internal class Program
{
const string sourceDir = @"C:\Laufwerk_d\Net\IEmagen_grayscaletiff_error\IEvolution gray_tif_fails\SourceFiles\";
static void Main(string[] args)
{
string targetTiff = GetAppPath + "multipage.tif";
string pageFile = sourceDir + "Document_0001_page_{0}.png";//0001
int ip = 1;
//color & compression:
//TIFF grayScale:
int bps = 16;
int spp = 1;
var ieCompression = HiComponents.IEvolution.IOParams.TIFFCompression.ZIP;
//create container for tiff:
var ieMulti = new HiComponents.IEvolution.IEMulti();
//start
string currentFile = string.Format(pageFile, AddLeadingZeros(ip.ToString()));
while (System.IO.File.Exists(currentFile))
{
//add image:
using (IEViewer ieViewer = new IEViewer())
{
ieViewer.Image.LoadImage(GetPdfMemoryStream(currentFile));
ieViewer.Image.ConvertToGray();
ieMulti.ImageList.AppendImage(ieViewer.Image);
}
//add to container:
//next:
ip++;
currentFile = string.Format(pageFile, AddLeadingZeros(ip.ToString()));
//free mem
FreeMem();
}
Console.WriteLine("converting to tiff:");
//set outputformat
for (int i = 0; i < ieMulti.ImageList.ImageCount; i++)
{
//dpi
ieMulti.ImageList.GetIOParams(i).DpiX = 300;
ieMulti.ImageList.GetIOParams(i).DpiY = 300;
//other
ieMulti.ImageList.GetIOParams(i).TIFF_Compression = ieCompression;
ieMulti.ImageList.GetIOParams(i).BitsPerSample = bps;
ieMulti.ImageList.GetIOParams(i).SamplesPerPixel = spp;
//ie16g
ieMulti.ImageList.GetIOParams(i).TIFF_ZIPCompression = HiComponents.IEvolution.IOParams.TIFFZIPCompression.Max;
}
//save:
ieMulti.ImageList.SaveImages(targetTiff);
//free mem:
ieMulti?.Dispose();
ieMulti = null;
}
static void FreeMem()
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
static MemoryStream GetPdfMemoryStream(string fullFilename)
{
MemoryStream ms = new MemoryStream();
using (FileStream file = new FileStream(fullFilename, FileMode.Open, FileAccess.Read))
{
byte[] bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
ms.Write(bytes, 0, (int)file.Length);
}
ms.Position = 0;
return ms;
}
static string AddLeadingZeros(string input, int lenght = 4)
{
return input.PadLeft(4, '0');
}
static string GetAppPath
{
get
{
string functionReturnValue = string.Empty;
functionReturnValue = AppDomain.CurrentDomain.BaseDirectory;
if (!functionReturnValue.EndsWith("\\")) functionReturnValue += "\\";
return functionReturnValue;
}
}
}
}
|
7 L A T E S T R E P L I E S (Newest First) |
xequte |
Posted - Dec 18 2023 : 17:37:08 Thanks Thomas, you too.
Nigel Xequte Software www.imageen.com
|
pcmueller77 |
Posted - Dec 18 2023 : 01:45:58 Hi Nigel, you are totally right! That was my mistake! No it works and doesn't consume much memory. (max 37mb, normal 12 mb) so perfekt for me. Thanks a lot & have nice x-mas & a happy new year! With best regards, Thomas. |
xequte |
Posted - Dec 15 2023 : 18:50:36 Hi Thomas
Your SaveAsTiff() method does not explicitly specify a file format, so IEvolution must infer it from the file extension and fails when one is not specifies (actually it will be defaulting to a non-TIFF type).
So you should specify a file format for SaveImage():
https://www.imageen.com/ievolutionhelp/html/909cbe23-5902-62e3-9a8b-4f3e12989332.htm
Nigel Xequte Software www.imageen.com
|
pcmueller77 |
Posted - Dec 15 2023 : 07:45:28 Hi, thanks, but I guess I found a bug: For merging files, I created temp-tiffs. but than its not working. It only works if the extension of the inputfile is tif/tiff. Is this correct? This is bad, If I use System.IO.Path.GetTempFileName();
Sample Code for this:
static void Test2(string targetTiff)
{
List<string> tempFiles = new List<string>();
try
{
/*
Bilder vorbereiten und als Tiff-Einzelbild speichern
Mergen via
InsertTIFFImageFile
*/
string pageFile = sourceDir + "Document_0001_page_{0}.png";//0001
//color & compression:
//TIFF grayScale:
/*Parameters
* sourceFileName The source file name
* insertingFileName The file to insert
* outFileName The destination file name
* index index is the page where to insert the file.
*/
//create container for tiff:
//IEImage.InsertTIFFImageFile(last,imageToInsert,newLast,index)
//start
int ip = 1;
string currentFile = string.Format(pageFile, AddLeadingZeros(ip.ToString()));
string tempFile = string.Empty;
Kill(targetTiff);
string targetTiff_new = targetTiff + ".tiff";
while (System.IO.File.Exists(currentFile))
{
if (!System.IO.File.Exists(targetTiff))
{
SaveAsTiff(currentFile, targetTiff);
#if DEBUG
//string input1 = string.Format(pageFile, AddLeadingZeros("1"));
//string input2 = string.Format(pageFile, AddLeadingZeros("2"));
//string input3 = string.Format(pageFile, AddLeadingZeros("3"));
//string input1Tiff = GetAppPath + "input_1" + ".tiff";
//string input2Tiff = GetAppPath + "input_2" + ".tiff";
//string input3Tiff = GetAppPath + "input_3" + ".tiff";
//string outtif = GetAppPath + "1-2.tiff";
//Kill(input1Tiff);
//Kill(input2Tiff);
//Kill(input3Tiff);
//Kill(outtif);
//SaveAsTiff(input1, input1Tiff);
//SaveAsTiff(input2, input2Tiff);
//SaveAsTiff(input3, input3Tiff);
//IEImage.InsertTIFFImageFile(input2Tiff, input1Tiff, outtif, 0);
////Seite 3 anfügen:
//string outtif_old= GetAppPath + "1-2.tiff";
//outtif = GetAppPath + "1-3.tiff";
//IEImage.InsertTIFFImageFile(input3Tiff, outtif_old, outtif, 0);
//Console.WriteLine("test");
#endif
}
else
{
tempFile = GetAppPath + "tmp.tif";// System.IO.Path.GetTempFileName();
tempFiles.Add(tempFile);
//add image:
SaveAsTiff(currentFile, tempFile);
//add to container:
// IEImage.InsertTIFFImageFile(input2Tiff, input1Tiff, outtif, 0);
IEImage.InsertTIFFImageFile(tempFile,targetTiff, targetTiff_new, 0);
Kill(targetTiff);
System.IO.File.Move(targetTiff_new, targetTiff);
}
//next:
ip++;
currentFile = string.Format(pageFile, AddLeadingZeros(ip.ToString()));
//free mem
FreeMem();
}
}
finally
{
foreach (string tmpFile in tempFiles)
Kill(tmpFile);
}
}
static void SaveAsTiff(string source, string target)
{
using (IEViewer ieViewer = new IEViewer())
{
ieViewer.Image.LoadImage(GetPdfMemoryStream(source));
ieViewer.Image.ConvertToGray();
SetCompression(ieViewer.Image);
ieViewer.Image.SaveImage(target);
}
}
static void Kill(string fullfilename)
{
try
{
if (System.IO.File.Exists(fullfilename))
System.IO.File.Delete(fullfilename);
}
catch {/*by design */}
}
Thanks & with best regards, Thomas.
|
xequte |
Posted - Dec 14 2023 : 17:07:16 The other way, which will avoid maintaining the whole tiff file in memory, is:
https://www.imageen.com/ievolutionhelp/html/1fd74fef-cbe6-9de8-7aa5-acabeddc3b63.htm
But see the note at:
https://www.imageen.com/ievolutionhelp/html/7774e9d3-06fb-a55f-8e7f-ff0a2a8a3fa3.htm
Nigel Xequte Software www.imageen.com
|
pcmueller77 |
Posted - Dec 12 2023 : 01:46:40 Hello Nigel. Thanks for the answer, but we have a problem with the component. The memory consumption at ieMulti.ImageList.SaveImages(targetTiff); is much to hight. Can we suppress this? The negative effect we have is: image 39 to 45 (the rest) are the same (from image 39 in the tiff the next are the same. Of course they are not in the source fl older, and there are loaded successfully with IEViewer, double-checked this). The program is a twain scanner application, so we can't switch to 64bit. While saving the ram goes up to 1.5 GB (in the scanner app) The console-test-app is about 1.16 GB. I also get LoaderLock Messages. How can I get rid of them? Thanks a lot & best regards, Thomas. |
xequte |
Posted - Dec 09 2023 : 16:46:06 Hi
Yes, that looks like the best way to do it in IEvolution.
Nigel Xequte Software www.imageen.com
|
|
|