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
114 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 4

Accepted Solution

by:
dsuyemoto earned 500 total points
ID: 41864715
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
ID: 41864794
The perfect answer for me. Priceless.  :)
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

830 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