zimmer9
asked on
I am wring a console application in C# using VS2010 with Framework 4.0 and do not know how to write multiple TIFF images to a file?
I am writing a C# console application using VS2010 with Framework Version 4.0 to generate multipage TIFF images and save these images to a file.
The variable "img" contains the front image of a check.
The variable "img2" contains the back image of a check.
Function A is performed for each pair of front image (img) and back images (img2) of each check processed from an input file:
Funtion A
{
Byte[] img = bytes.Skip(irecpositionrev ised + irectypelength).Take(intpo s1revised) .ToArray() ;
Byte[] img1 = bytes.Skip(irecpositionrev ised + irectypelength + intpos1revised).Take(intpo s2revised) .ToArray() ;
newImage[CheckOrderNum - 1] = MergeTwoImages(ConvertFrom ByteArray( img), ConvertFromByteArray(img1) );
}
After all checks have been read & processed via Function A, the following foreach loop is performed to write out
ALL multipage TIFF images:
foreach (Image image in newImage)
{
image.Save("L:\\F\\Source\ \test1.tif f", System.Drawing.Imaging.Ima geFormat.T iff); <-- this Save only Saves the current TIFF Image
}
My goal is to write out ALL mulitpage TIFF Images to a file titled test1.tiff, so that I can see the front image & back image of ALL checks my application reads from an input file.
Currenlty my program OVERWRITES the existing TIFF multipage Image with the most recent TIFF mulitpage each time the file is saved.
-------------------------- ---------- ---------- ---------- ---------
I believe the following approach will work for Version 4.5 but unfortunately I am working with Version 4.0 (VS2010)
using System;
using System.Collections.Generic ;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
namespace EE_Q28711383
{
class Program
{
static void Main(string[] args)
{
List<Image> images = new List<Image>();
images.Add(Image.FromFile( Path.Combi ne(AppDoma in.Current Domain.Bas eDirectory , "image1.jpg")));
images.Add(Image.FromFile( Path.Combi ne(AppDoma in.Current Domain.Bas eDirectory , "image2.jpg")));
images.MergeImages(new FileInfo(Path.Combine(AppD omain.Curr entDomain. BaseDirect ory, "image3.tiff")));
}
}
static class Extensions
{
public static void MergeImages(this IEnumerable<Image> images, string fileName)
{
images.MergeImages(new FileInfo(fileName));
}
public static void MergeImages(this IEnumerable<Image> images, FileInfo file)
{
if (images == null)
throw new ArgumentNullException("ima ges", "Images cannot be null");
if (file == null)
throw new ArgumentNullException("fil e", "File cannot be null");
ImageCodecInfo codec = null;
Image tiff = null;
MemoryStream stream = null;
EncoderParameters parameters = null;
try
{
codec = (from _codec in ImageCodecInfo.GetImageEnc oders() where _codec.MimeType.Equals("im age/tiff", StringComparison.OrdinalIg noreCase) select _codec).FirstOrDefault();
if (codec != default(ImageCodecInfo))
{
bool first = true;
parameters = new EncoderParameters(2);
parameters.Param[0] = new EncoderParameter(Encoder.C ompression , (long)EncoderValue.Compres sionCCITT4 );
parameters.Param[1] = new EncoderParameter(Encoder.S aveFlag, (long)EncoderValue.MultiFr ame);
foreach (Image image in images)
{
stream = new MemoryStream();
image.Save(stream, ImageFormat.Tiff);
if (first)
{
tiff = Image.FromStream(stream);
tiff.Save(file.FullName, codec, parameters);
first = false;
}
else
{
parameters.Param[1] = new EncoderParameter(Encoder.S aveFlag, (long)EncoderValue.FrameDi mensionPag e);
tiff.SaveAdd(Image.FromStr eam(stream ), parameters);
}
stream.Dispose();
image.Dispose();
}
parameters.Param[1] = new EncoderParameter(Encoder.S aveFlag, (long)EncoderValue.Flush);
tiff.SaveAdd(parameters);
}
}
catch (Exception ex)
{
Console.WriteLine("Excepti on reported: {0}", ex);
}
finally
{
parameters.Dispose();
stream.Dispose();
tiff.Dispose();
}
}
}
}
The variable "img" contains the front image of a check.
The variable "img2" contains the back image of a check.
Function A is performed for each pair of front image (img) and back images (img2) of each check processed from an input file:
Funtion A
{
Byte[] img = bytes.Skip(irecpositionrev
Byte[] img1 = bytes.Skip(irecpositionrev
newImage[CheckOrderNum - 1] = MergeTwoImages(ConvertFrom
}
After all checks have been read & processed via Function A, the following foreach loop is performed to write out
ALL multipage TIFF images:
foreach (Image image in newImage)
{
image.Save("L:\\F\\Source\
}
My goal is to write out ALL mulitpage TIFF Images to a file titled test1.tiff, so that I can see the front image & back image of ALL checks my application reads from an input file.
Currenlty my program OVERWRITES the existing TIFF multipage Image with the most recent TIFF mulitpage each time the file is saved.
--------------------------
I believe the following approach will work for Version 4.5 but unfortunately I am working with Version 4.0 (VS2010)
using System;
using System.Collections.Generic
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
namespace EE_Q28711383
{
class Program
{
static void Main(string[] args)
{
List<Image> images = new List<Image>();
images.Add(Image.FromFile(
images.Add(Image.FromFile(
images.MergeImages(new FileInfo(Path.Combine(AppD
}
}
static class Extensions
{
public static void MergeImages(this IEnumerable<Image> images, string fileName)
{
images.MergeImages(new FileInfo(fileName));
}
public static void MergeImages(this IEnumerable<Image> images, FileInfo file)
{
if (images == null)
throw new ArgumentNullException("ima
if (file == null)
throw new ArgumentNullException("fil
ImageCodecInfo codec = null;
Image tiff = null;
MemoryStream stream = null;
EncoderParameters parameters = null;
try
{
codec = (from _codec in ImageCodecInfo.GetImageEnc
if (codec != default(ImageCodecInfo))
{
bool first = true;
parameters = new EncoderParameters(2);
parameters.Param[0] = new EncoderParameter(Encoder.C
parameters.Param[1] = new EncoderParameter(Encoder.S
foreach (Image image in images)
{
stream = new MemoryStream();
image.Save(stream, ImageFormat.Tiff);
if (first)
{
tiff = Image.FromStream(stream);
tiff.Save(file.FullName, codec, parameters);
first = false;
}
else
{
parameters.Param[1] = new EncoderParameter(Encoder.S
tiff.SaveAdd(Image.FromStr
}
stream.Dispose();
image.Dispose();
}
parameters.Param[1] = new EncoderParameter(Encoder.S
tiff.SaveAdd(parameters);
}
}
catch (Exception ex)
{
Console.WriteLine("Excepti
}
finally
{
parameters.Dispose();
stream.Dispose();
tiff.Dispose();
}
}
}
}
According to MSDN Image.SaveAdd() method exists from version 1.1. Why can't you use it with version 4.0? Where does it fail? Can you show us the errors?
ASKER
Ok, I got my code to compile now as follows:
Do you know how I can resolve the following error:
Error 1 The name 'MergeImages' does not exist in the current context U:\Visual Studio 2010\Projects\Citi1\Citi1\ Program.cs 582 17 Citi1
Do you know how I can resolve the following error:
Error 1 The name 'MergeImages' does not exist in the current context U:\Visual Studio 2010\Projects\Citi1\Citi1\
ASKER
The error is in the following "foreach" loop when I try and call the new function MergeImages:
foreach (Image image in newImage)
{
MergeImages(image, "L:\\test1.tiff");
}
I have attached my source code.
multipletiff.txt
foreach (Image image in newImage)
{
MergeImages(image, "L:\\test1.tiff");
}
I have attached my source code.
multipletiff.txt
if you put it inside the static class Extensions as per the code above then you need to use
Extensions.MergeImages(image, "L:\\test1.tiff");
If you used the whole EE_Q28711383 namespace then you need to use: EE_Q28711383.Extensions.MergeImages(image, "L:\\test1.tiff");
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks so very much. You were extremely helpful. )