Link to home
Start Free TrialLog in
Avatar of zimmer9
zimmer9Flag for United States of America

asked on

Can you suggest a way I can shrink the file size of output image files?

I am developing a C# application using VS2010 and .Net Framework 4.0

I use the following function to combine 2 check image files into 1 check image file with one check image appearing below the other.

However, the output image files created are rather large in file size.

Can you suggest a way I can shrink the size of the output image files and there is a lot of empty space between the 2 check images
in the output check image file.

 public static void ProcessImage(string checkImgPrefix, string directory)
        {
            string tif1 = directory + checkImgPrefix + "_Front.tif";
            string tif2 = directory + checkImgPrefix + "_Rear.tif";
            string tif3 = directory + checkImgPrefix + ".ard.out";

            Image img1 = Image.FromFile(tif1);
            Image img2 = Image.FromFile(tif2);

            int width = Math.Max(img1.Width, img2.Width);
            int height = img1.Height + img2.Height;

            Bitmap img3 = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(img3);

            g.Clear(Color.Black);
            g.DrawImage(img1, new Point(0, 0));
            g.DrawImage(img2, new Point(0, img1.Height));

            g.Dispose();
            img1.Dispose();
            img2.Dispose();

            img3.Save(tif3, System.Drawing.Imaging.ImageFormat.Tiff);
            img3.Dispose();
        }
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Save them as jpgs - that is pretty good at compressing files.
Yes, or PNG is good for size reduction (less than JPG) but better quality than JPG.  You could also zip up the 2 source images to reduce the size as well, since you won't need them as they're combined in the final output.
Avatar of zimmer9

ASKER

I should have noted that I receive the input check image files from the bank in .tif file format and I have no choice in this arrangement.
> the input check image files from the bank in .tif file format

Uncompressed TIFF files are large, so they are often compressed to make them smaller. I don't know what libraries you are using in your C# app, but if you already have one that does TIFF compression, use it. Otherwise, there are free command line executables out there that you could call from your C# app. For example, although most folks use IrfanView as a GUI imaging app, it also has a command line interface. One of the params in its CLI is /tifc (stands for TIFF Compression) with these choices:

0=None
1=LZW
2=Packbits
3=ITU-T Group 3
4=ITU-T Group 4
5=Huffman
6=JPG
7=ZIP

If it's a one-page TIFF file, this line of code will do it:

i_view32.exe input.tif /convert=output.tif /killmesoftly /silent /tifc=N

Open in new window

where N is an integer from the list above — experiment to see what works for you. If it's a multi-page TIFF file, look at the CLI doc for the /multitif param (the CLI doc is in a file called i_options.txt in the installation folder).

Another possibility is GraphicsMagick or ImageMagick. Here's an EE article that shows how to download/install GM and discusses the various editions:
Reduce the file size of many JPG files in many folders via an automated, mass, batch compression method

It also shows how to shrink the file size of JPG files, although the -quality option doesn't work for TIFF files. Instead, use the -compress option:

-compress TYPE

where the choices for TYPE are:

None
BZip
Fax
Group4
JPEG
Lossless
LZW
RLE
Zip
LZMA

Note that +compress makes the output file uncompressed. Also note that Lossless is just for lossless JPEG, so does not apply to TIFF files (all of the command line params are documented at http://www.graphicsmagick.org/GraphicsMagick.html).

To convert and modify the source file directly, use the mogrify sub-command, something like:

gm mogrify -compress Group4 input.tif

Open in new window

To convert and store the compressed file in a new file, use the convert sub-command, something like:

gm convert input.tif -compress Group4 output.tif

Open in new window


I just tested GM with a 50 MB uncompressed TIFF file — shrank it to a 96 KB TIFF file with Group4 compression! ImageMagick also supports the same convert and mogrify functions. Regards, Joe
If you have no choice about the file type (from a third party) you might not be able to compress them and still have the third party be able to use them.
Avatar of zimmer9

ASKER

The files will be sent to IBM Content Manager OnDemand for viewing.
Given that check images from the bank are likely black&white (even if the actual checks are color), I suggest ITU-T (formerly CCITT) Group 4 compression, which should work very well on them, resulting in a significant reduction of the file size. And third-party software should certainly be able to use a TIFF file with Group 4 compression, which has been an industry standard for decades. Regards, Joe
ASKER CERTIFIED SOLUTION
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial