Avatar of hasan_tek
hasan_tek
 asked on

c#- Drawing rectangle on a Tiff image bloats the file

Hi I am trying to add a small rectanlge graphics on a exiting Tiff file using C# and sav it back as a new tiff file. But the image sevelry bloats from 118kb to 954 kb just for adding a small rectangle. My original image doesn't have any color except black and white(may be greyscale). The rectangle box that I am drawing uses black color.
Please help in fixing this issue.
imgPath = "D:\\sample.tiff";
            newImgPath = "D:\\samplenew.tiff";
            Bitmap org = (Bitmap)Image.FromFile(imgPath);
            Bitmap bm = new Bitmap(org.Width, org.Height);
            Graphics g = Graphics.FromImage(bm);
            g.DrawImage(org, 0, 0, org.Width, org.Height);
            g.FillRectangle(Brushes.Black, 5, 5, 100, 30);
            g.DrawRectangle(Pens.Black, 5, 5, 100, 30);
            g.Dispose();
            bm.Save(newImgPath, System.Drawing.Imaging.ImageFormat.Tiff);
            bm.Dispose();

Open in new window

.NET ProgrammingEditors IDEs

Avatar of undefined
Last Comment
hasan_tek

8/22/2022 - Mon
Mike Tomlinson

Try...
            imgPath = "D:\\sample.tiff";
            newImgPath = "D:\\samplenew.tiff";
 
            System.IO.FileStream fs = new System.IO.FileStream(imgPath, System.IO.FileMode.Open);
            Image img = Image.FromStream(fs);
            fs.Close();
 
            Graphics g = Graphics.FromImage(img);
            g.FillRectangle(Brushes.Black, 5, 5, 100, 30);
            g.DrawRectangle(Pens.Black, 5, 5, 100, 30);
            g.Dispose();
 
            img.Save(newImgPath, System.Drawing.Imaging.ImageFormat.Tiff);

Open in new window

hasan_tek

ASKER
Thanks for responding.
Getting Error:
A Graphics object cannot be created from an image that has an indexed pixel format.
at the line
Graphics g = Graphics.FromImage(img);
Any suggestions?
hasan_tek

ASKER
I tried your approach before and got the error and somebody suggested the alternate way of creating a bitmap(my original code) but that bloats the resulting tiff...
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Mike Tomlinson

Hmm....sorry.  I did a quick test with a tiff but i guess it wasn't "indexed".  =\
Mike Tomlinson

I'm not really good with all the different image formats...perhaps you need to specify the format when creating "bm" using this constructor?
(with your original code that is)
http://msdn.microsoft.com/en-us/library/3z132tat.aspx
http://msdn.microsoft.com/en-us/library/system.drawing.imaging.pixelformat.aspx
hasan_tek

ASKER
I tried changing it as

Bitmap bm = new Bitmap(org.Width, org.Height,org.PixelFormat);
but its giving the same error :
A Graphics object cannot be created from an image that has an indexed pixel format.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
hasan_tek

ASKER
I even tried adding encoder.compression for the bitmap as per: http://msdn.microsoft.com/en-us/library/system.drawing.imaging.encoder.compression.aspx
That doesn't help a lot. It creates a tiff  of 954 kb  size from a 118kb original tiff files. Please help.
ASKER CERTIFIED SOLUTION
hasan_tek

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.