Solved

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

Posted on 2016-08-26
8
72 Views
Last Modified: 2016-08-29
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();
        }
0
Comment
Question by:zimmer9
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41772111
Save them as jpgs - that is pretty good at compressing files.
0
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41772464
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
 

Author Comment

by:zimmer9
ID: 41773654
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
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 41773714
> 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41773768
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
 

Author Comment

by:zimmer9
ID: 41773780
The files will be sent to IBM Content Manager OnDemand for viewing.
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 41773787
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
 
LVL 51

Accepted Solution

by:
Joe Winograd, EE MVE earned 500 total points
ID: 41773788
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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The goal of the tutorial is to teach the user how to add a water mark to there photo. Once you have a photo you like you have to go into the water mark setting and add a water mark to the image. You can either choose a text watermark or an image…
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now