Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using Bitmap to water mark tif image files after it reach file number 440 the image size increases which results in a small text watermark. My method is designed to process large amount of files, it also checks if the image is colored or not and process accordingly. Any suggestions on what I should try. I don't know why it is happening. orginal size: 2480 x 3580
large size: 5250 x 7497

What I have tried:

C#
public void waterMark(string euroPrefix, string resultedTifFolders, string waterMarkedTif)
        {

            using (Brush brush = new SolidBrush(Color.Black))
            {
                using (Font font = new Font("Arial", 50, GraphicsUnit.Pixel))
                {
                    using (Bitmap original_bitmap = new Bitmap(resultedTifFolders))
                    {
                        int compressionTagIndex = Array.IndexOf(original_bitmap.PropertyIdList, 0x103);
                        PropertyItem compressionTag = original_bitmap.PropertyItems[compressionTagIndex];
                        byte[] com = compressionTag.Value;
                        Encoder encoder = Encoder.Compression;
                        EncoderParameters myEncoderParameters = new EncoderParameters(1);
                        EncoderParameter myEncoderParameter = new EncoderParameter(encoder, (long)EncoderValue.CompressionCCITT4);
                        myEncoderParameters.Param[0] = myEncoderParameter;
                        ImageCodecInfo myImageCodecInfo;
                        myImageCodecInfo = GetEncoderInfo("image/tiff");
                        SizeF euroPrefixSize;
                        if (new[] { 5, 7 }.Contains(com[0]))

                        {
                            using (Graphics tempGraphics = Graphics.FromImage(original_bitmap))
                            {
                                euroPrefixSize = tempGraphics.MeasureString(euroPrefix, font);
                            }

                            using (Bitmap new_bitmap = new Bitmap(original_bitmap.Width, original_bitmap.Height + (int)euroPrefixSize.Height + 10))
                            {
                                new_bitmap.SetResolution(original_bitmap.HorizontalResolution,original_bitmap.VerticalResolution);
                                using (Graphics graphics = Graphics.FromImage(new_bitmap))
                                {

                                    graphics.FillRectangle(Brushes.White, 0, 0, original_bitmap.Width, original_bitmap.Height + 100);

                                    graphics.DrawImage(original_bitmap, 0, 0, original_bitmap.Width, original_bitmap.Height);

                                    Point position = new Point(original_bitmap.Width - ((int)euroPrefixSize.Width + 200), original_bitmap.Height + 5);

                                    graphics.DrawString(euroPrefix, font, brush, position);

                                    new_bitmap.Save(waterMarkedTif, /*ImageFormat.Tiff,*/ myImageCodecInfo, myEncoderParameters);


                                    new_bitmap.Save(waterMarkedTif, ImageFormat.Tiff);
                                }
                            }
                            return;
                        }
                        else
                        {

                            Bitmap tempBitmap = new Bitmap(original_bitmap.Width, original_bitmap.Height);

                            using (Graphics tempGraphics = Graphics.FromImage(tempBitmap))
                            {
                                euroPrefixSize = tempGraphics.MeasureString(euroPrefix, font);
                            }
                            tempBitmap = new Bitmap(original_bitmap.Width, original_bitmap.Height + (int)euroPrefixSize.Height + 10);

                            tempBitmap.SetResolution(original_bitmap.HorizontalResolution,original_bitmap.VerticalResolution);

                            using (Graphics graphics = Graphics.FromImage(tempBitmap))
                            {
                                graphics.FillRectangle(Brushes.White, 0, 0, original_bitmap.Width, original_bitmap.Height + 100);

                                graphics.DrawImage(original_bitmap, 0, 0, original_bitmap.Width, original_bitmap.Height);

                                Point position = new Point(original_bitmap.Width - ((int)euroPrefixSize.Width + 200), original_bitmap.Height + 5);

                                graphics.DrawString((euroPrefix), font, brush, position);

                                tempBitmap.Save(waterMarkedTif, ImageFormat.Tiff);
                            }
                        }
                    }
                }
            }
        }
Posted
Updated 20-Apr-20 0:14am
v2
Comments
Daniele Rota Nodari 20-Apr-20 6:01am    
I would check effects of "SetResolution", in case postponing its invocation or trying avoiding it.
Variable myEncoderParameters is another candidate.
In additon, I would move all "Save" invocations out of "using (Graphics)" blocks (this would also allow to move SetResolution between using block and Save invocation).
AskalotLearnalot 20-Apr-20 10:09am    
Thank you, I will look into it. As far as usings and save I did it this way because the GDI runs out of space when i edit large amount of files at a run.
Daniele Rota Nodari 20-Apr-20 10:56am    
Yes, as you already did, disposing the Graphics objects is important to reduce resource usage, and "using" is an easy way to ensure that.
This is a further reason to move "Save" out of "using", in order to dispose Graphics objects as soon as they are no longer needed.
You can move before any "using" block any instruction that DOES NOT interact with the Graphics object allocated by that "using"; this could also help into refactoring code by moving some code to new, specialized, methods (e.g.: there are 2 "using(Graphics...." blocks that seem almost identical to each other).
Last thing: using/Dispose are missing for tempBitmap; pay attention to this one because two new Bitmap objects are assigned to that variable.

1 solution

We can't really tell, without running your code on your data - and we don't have access to either!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

You may want to add code to make breakpointing when it's failing easier - a count perhaps, or a check on the file name / size so you can step through a working one, and a failing one.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
AskalotLearnalot 20-Apr-20 9:10am    
please add this as a comment as it is. I know how to debug, thank you.
phil.o 20-Apr-20 12:33pm    
So, we're supposed to know that you already tried to debug? What did you learn from the debug session(s) you conducted? Any useful information?
AskalotLearnalot 20-Apr-20 14:36pm    
it was 0 bugs and warnings
phil.o 20-Apr-20 14:49pm    
Then, you think you know what debugging is, but you don't. Debugging does not point you obvious bugs and warnings, it is used to investigate a piece of code and discover why the result you get is not what you expect.
Getting back to your current issue, you should use debugging to find the reason of the size increase; this means putting a breakpoint in the code, launching a debug session, and watching carefully for your variables' values and return values from functions. As OG told you, we cannot do that for you, because this investigation cannot be conducted without the actual data.
OriginalGriff 20-Apr-20 15:21pm    
Compilers don't tell you about bugs, they tell you about syntax errors: where you wrote code in German and it expected English. Suppose your code is instructions to make the bed:
1) Spread sheet over mattress, tuck in.
2) Pour petrol over the sheet.
3) Light match, throw on sheet.
4) Fit Duvet.
5) Add pillows.

That's all English, so the compiler doesn't care that following the instructions would burn the house down, it's fine about it.

But when you do run the code, you find a bug: you can't tell if the bed is made because your house is wrecked, and you are in hospital.

Debuggers allow you to step through the instructions one at a time (among other things and look at what is happening: when you get to step 3, you might notice that something serious is about to happen, and look more closely to find out why.

So break out the debugger, and use it to look at what your code is doing while it is running: the compiler won't tell you that, only running the code will. The debugger is a tool that makes looking what is happening a lot, lot easier. Google for it, and see what you can find out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900