[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
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
Medium Priority
?
155 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

864 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