Solved

Is there a way to modify my C# code to make 2 check images larger? Presently there is a lot of space between the 2 check images when I merge them and they appear small & thus are hard to read.

Posted on 2016-10-27
2
83 Views
Last Modified: 2016-10-28
I am in the process of creating a C# console application (using .Net Framework 4.0) in which I take the front image of a check stored in 1 TIF file and the back image of a check stored in another TIF file and merge the 2 files into 1 file which displays the front and back image of checks in 1 file. Is there a way to modify my following C# code to make the 2 check images larger because presently there is a lot of empty space between the 2 check images when I merge them and the images themselves are rather small in size in the merged file.

Here is my C# code that takes 2 TIF files and merges them into 1 file to which I assign the file name extension of "ard.out":
I have attached a snapshot of 1 of the "ard.out" files so that you can see what I'm looking to improve upon.
I renamed the file extension in the attachment as "*.tiff" so that it is readable to you.
The images appear with the front check image above the back check image.

 public static void CreateTifFile(string file_tif)
        {
            Bitmap bmp = new Bitmap(1024, 512);
            using (Graphics graph = Graphics.FromImage(bmp))
            {
                Rectangle ImageSize = new Rectangle(0, 0, 1024, 512);
                StringFormat sf = new StringFormat();
                sf.LineAlignment = StringAlignment.Center;
                sf.Alignment = StringAlignment.Center;

                graph.FillRectangle(Brushes.White, ImageSize);
                graph.DrawString("IMAGE NOT AVAILABLE", new Font("Tahoma", 50), Brushes.Black, ImageSize, sf);
            }
            file_tif = file_tif.Replace(".TIF", ".TIF");
            bmp.Save(Globals.s_NoImage + file_tif);
        }

        internal class ProcessImageThread
        {
            private string _checkImgPrefix;
            private string _directory;

            public ProcessImageThread(string checkImgPrefix, string directory)
            {
                _checkImgPrefix = checkImgPrefix;
                _directory = directory;
            }

            public void ProcessImage(Object threadContext)
            {
                try
                {
                    int threadIndex = (int)threadContext;

                    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();

                    string fileName = Path.GetFileName(tif3);
                    byte[] imgBits = File.ReadAllBytes(tif3);

                    using (MemoryStream ms = new MemoryStream(imgBits))
                    {
                        using (Image i = Image.FromStream(ms))
                        {
                            EncoderParameters parms = new EncoderParameters(1);
                            ImageCodecInfo codec = ImageCodecInfo.GetImageDecoders()
                                                                 .FirstOrDefault(decoder => decoder.FormatID == ImageFormat.Tiff.Guid);

                            parms.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionCCITT4);

                            i.Save(tif3, codec, parms);
                        }
                    }                    
                }
                finally
                {

                    if (Interlocked.Decrement(ref _numerOfThreadsNotYetCompleted) == 0)
                        _doneEvent.Set();
                }
            }
        }

Open in new window

Sample.tiff
0
Comment
Question by:zimmer9
2 Comments
 
LVL 2

Accepted Solution

by:
dsuyemoto earned 500 total points
Comment Utility
The problem appears to be with:

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

Which retains the original size and doesn't account for the dpi. if you change to the following:

g.DrawImage(img1, 0, 0, img1.Width, img1.Height);
g.DrawImage(img2, 0, img1.Height, img2.Width, img2.Height);

This should remove the "spacing" and alter the image to the size of the one that's created.
0
 

Author Closing Comment

by:zimmer9
Comment Utility
The perfect answer for me. Priceless.  :)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

7 Experts available now in Live!

Get 1:1 Help Now