467,156 Members | 971 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,156 developers. It's quick & easy.

Save Tiff very large image size

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.FrameDimensi onPage);
Bitmap image = null;
for (int i = 1; i < this.imageContainer.Count; i++)
{
image = this.imageContainer[i] 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.GetImageEnco ders())
{
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
}
Oct 18 '06 #1
  • viewed: 3158
Share:
1 Reply
Hi Stedak,

You're not using any compression. TIFF files may be compressed by any of
several compression formats, most commonly LZW. You would use am
EncoderParameter to set the compression. There's a TIFF example here:

http://msdn2.microsoft.com/en-us/lib...mpression.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Shooter
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.

"Stedak" <St****@discussions.microsoft.comwrote in message
news:0E**********************************@microsof t.com...
>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.FrameDimensi onPage);
Bitmap image = null;
for (int i = 1; i < this.imageContainer.Count; i++)
{
image = this.imageContainer[i] 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.GetImageEnco ders())
{
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
}

Oct 18 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by CDB | last post: by
7 posts views Thread by Leszek | last post: by
5 posts views Thread by Philippe Martin | last post: by
3 posts views Thread by finecur | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.