By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,141 Members | 1,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,141 IT Pros & Developers. It's quick & easy.

Save Tiff very large image size

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.