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();
        }
zimmer9Asked:
Who is Participating?
 
Joe Winograd, Fellow&MVEConnect With a Mentor DeveloperCommented:
Our messages just crossed — the IBM Content Manager should have no problem viewing a TIFF file with Group 4 compression — try it.

Update: I'm not familiar with .NET code, but here's a question where the asker has calls to Group 4 compression:
https://www.experts-exchange.com/questions/28966186/why-Created-tiff-files-are-coming-in-huge-size.html

I'm guessing he got the code from elsewhere and doesn't even realize that the Group 4 compression code has been commented out (hence, my post there). But if that code is good, it should work in your C# app. Look at these commented out lines that appear to be calling CCITT Group 4 compression:

' iparamPara = New EncoderParameter(iparam, CLng(EncoderValue.CompressionCCITT4))

' CompressionEncodeParam = new EncoderParameter(compressionEncoder, (long)EncoderValue.CompressionCCITT4);

' EncoderParams.Param(1) = New EncoderParameter(System.Drawing.Imaging.Encoder.Compression, EncoderValue.CompressionCCITT4)

' myEncoderParameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4);

' CompressionEncodeParam = new EncoderParameter(compressionEncoder, (long)EncoderValue.CompressionCCITT4);

Open in new window

Regards, Joe
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Save them as jpgs - that is pretty good at compressing files.
0
 
Dustin SaundersDirector of OperationsCommented:
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.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
zimmer9Author Commented:
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.
0
 
Joe Winograd, Fellow&MVEDeveloperCommented:
> 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
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
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.
0
 
zimmer9Author Commented:
The files will be sent to IBM Content Manager OnDemand for viewing.
0
 
Joe Winograd, Fellow&MVEDeveloperCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.