G
Guest
I have the following class I use to save Tiff's. The problem I have with it
is that the final size of the images are very large. If we scan directly to a
file the final tiff may be 600-900 kb.s but with this code it is often
4000-5000 kb.s. What am I missing?
public class EmrTiff : IDisposable
{
private string fileName;
private ArrayList imageContainer = null;
public EmrTiff()
{
this.imageContainer = new ArrayList();
}
public void Add(Image image)
{
Bitmap bm = new Bitmap(image);
if (bm.GetFrameCount(FrameDimension.Page) > 1)
{
Console.WriteLine("");
}
this.imageContainer.Add(bm);
}
public bool Save()
{
this.CreateFileName();
if (this.imageContainer.Count > 1)
{
//the first item in the list is the master frame.
Bitmap masterBitmap = this.imageContainer[0] as Bitmap;
EncoderParameters encoderParameters = new EncoderParameters(1);
//save the first item
ImageCodecInfo imageCodecInfo = this.GetTiffCodec();
encoderParameters.Param[0] =
this.GetEncoderParameter(EncoderValue.MultiFrame);
masterBitmap.Save(this.fileName, imageCodecInfo,
encoderParameters);
//add all images from index 1 to n
encoderParameters.Param[0] =
this.GetEncoderParameter(EncoderValue.FrameDimensionPage);
Bitmap image = null;
for (int i = 1; i < this.imageContainer.Count; i++)
{
image = this.imageContainer as Bitmap;
masterBitmap.SaveAdd(image, encoderParameters);
}
//close out the file.
encoderParameters.Param[0] =
this.GetEncoderParameter(EncoderValue.Flush);
masterBitmap.SaveAdd(encoderParameters);
masterBitmap.Dispose();
}
else if (this.imageContainer.Count == 1)
{
//do a simple save..
Bitmap masterBitmap = this.imageContainer[0] as Bitmap;
masterBitmap.Save(this.fileName, ImageFormat.Tiff);
masterBitmap.Dispose();
}
return (this.imageContainer.Count > 0);
}
private void CreateFileName()
{
if (this.imageContainer.Count > 0)
{
this.fileName = Path.GetTempFileName();
this.fileName = Path.ChangeExtension(this.fileName, "tif");
}
}
private EncoderParameter GetEncoderParameter(EncoderValue value)
{
return new EncoderParameter(Encoder.SaveFlag,(long)value);
}
private ImageCodecInfo GetTiffCodec()
{
System.Drawing.Imaging.ImageCodecInfo result = null;
foreach (ImageCodecInfo imageCodecInfo in
System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders())
{
if (imageCodecInfo.MimeType == "image/tiff")
{
result = imageCodecInfo;
break;
}
}
return result;
}
/// <summary>
/// Property FileName (string)
/// </summary>
public string FileName
{
get { return this.fileName; }
set { this.fileName = value; }
}
#region IDisposable Members
public void Dispose()
{
if (this.imageContainer != null)
{
foreach (Image image in imageContainer)
{
image.Dispose();
}
this.imageContainer = null;
}
}
#endregion
}
is that the final size of the images are very large. If we scan directly to a
file the final tiff may be 600-900 kb.s but with this code it is often
4000-5000 kb.s. What am I missing?
public class EmrTiff : IDisposable
{
private string fileName;
private ArrayList imageContainer = null;
public EmrTiff()
{
this.imageContainer = new ArrayList();
}
public void Add(Image image)
{
Bitmap bm = new Bitmap(image);
if (bm.GetFrameCount(FrameDimension.Page) > 1)
{
Console.WriteLine("");
}
this.imageContainer.Add(bm);
}
public bool Save()
{
this.CreateFileName();
if (this.imageContainer.Count > 1)
{
//the first item in the list is the master frame.
Bitmap masterBitmap = this.imageContainer[0] as Bitmap;
EncoderParameters encoderParameters = new EncoderParameters(1);
//save the first item
ImageCodecInfo imageCodecInfo = this.GetTiffCodec();
encoderParameters.Param[0] =
this.GetEncoderParameter(EncoderValue.MultiFrame);
masterBitmap.Save(this.fileName, imageCodecInfo,
encoderParameters);
//add all images from index 1 to n
encoderParameters.Param[0] =
this.GetEncoderParameter(EncoderValue.FrameDimensionPage);
Bitmap image = null;
for (int i = 1; i < this.imageContainer.Count; i++)
{
image = this.imageContainer as Bitmap;
masterBitmap.SaveAdd(image, encoderParameters);
}
//close out the file.
encoderParameters.Param[0] =
this.GetEncoderParameter(EncoderValue.Flush);
masterBitmap.SaveAdd(encoderParameters);
masterBitmap.Dispose();
}
else if (this.imageContainer.Count == 1)
{
//do a simple save..
Bitmap masterBitmap = this.imageContainer[0] as Bitmap;
masterBitmap.Save(this.fileName, ImageFormat.Tiff);
masterBitmap.Dispose();
}
return (this.imageContainer.Count > 0);
}
private void CreateFileName()
{
if (this.imageContainer.Count > 0)
{
this.fileName = Path.GetTempFileName();
this.fileName = Path.ChangeExtension(this.fileName, "tif");
}
}
private EncoderParameter GetEncoderParameter(EncoderValue value)
{
return new EncoderParameter(Encoder.SaveFlag,(long)value);
}
private ImageCodecInfo GetTiffCodec()
{
System.Drawing.Imaging.ImageCodecInfo result = null;
foreach (ImageCodecInfo imageCodecInfo in
System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders())
{
if (imageCodecInfo.MimeType == "image/tiff")
{
result = imageCodecInfo;
break;
}
}
return result;
}
/// <summary>
/// Property FileName (string)
/// </summary>
public string FileName
{
get { return this.fileName; }
set { this.fileName = value; }
}
#region IDisposable Members
public void Dispose()
{
if (this.imageContainer != null)
{
foreach (Image image in imageContainer)
{
image.Dispose();
}
this.imageContainer = null;
}
}
#endregion
}