472,805 Members | 943 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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
1 3370
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: CDB | last post by:
Hello I was running the Image Browser example (from the JAI book by Lawrence Rodrigues) with a very large image, specifically one of the size 12,000 x 6,000 pixels, and the image does not render...
1
by: delong | last post by:
Hi I am trying to display a large image on the form and make the form scrollable. My image is about 4200 x 7000 pixel. private void Form1_Paint(object sender,...
0
by: prakash | last post by:
Dear Friends I am new guy to Visual C++.NET I've program to save website as a image vc++.net . It have a function "SaveSnapshot" to save the webpage as an image On that function ifor saving...
7
by: Leszek | last post by:
Hello, I need to set dynamically height and width attributes of an image control on a WebForm. I know how to read the whole image from the hard-drive and use its height and width properties. I...
0
by: Gavin | last post by:
Can anybody give me a code example of how to change an image size and resolution. I can upload a image file to a PostedFile. But what I need to do is change the size of that image and save it to my...
5
by: Philippe Martin | last post by:
Hi, Thanks to the NG, I got the script hereunder working. 1) I am not certain that the call to convert does much (checking the doc) 2) Can this be improved as far as the final image size in...
3
by: finecur | last post by:
I have a <img scr="mypic.jpg"in my html. I would like to display the image by width=200 if the image width is larger than 200. I also would like to display the image by its real width if the image...
11
by: tregan3 | last post by:
I'm having trouble opening a color .tiff. Calling Image.FromStream on the .tiff throws an "invalid parameter" error. Calling Bitmap.FromFile on the .tiff throws a "System.OutOfMemory" error...
6
vivekgs2007
by: vivekgs2007 | last post by:
Hi to all, I am created a Search Engine for the maps, Its working properly, but the problem is that...during uploading the map details to the database...It is not uploading the images...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.