Groups | Blog | Home
all groups > dotnet drawing api > february 2008 >

dotnet drawing api : struggling to save tiff as png and keep file size down


sklett
2/28/2008 1:39:32 PM
(I posted this in the C# NG on accident, reposting here)

I have a Tiff (fax) with the following properties:
width: 1728
height: 1090
x resolution: 204
y resolution: 98
bit depth: 1

If I open this tiff in Photoshop and change the resolution to 96 and resize
to 816 x 1056, then save as png the size is 3.1Kb (great!)

If I resize the tiff with gdi+ and save as png (same setting as Photoshop)
the filesize is 31Kb (bad!!!)

Here is the code I'm using to resize and save as png:
<code>
Image img = Bitmap.FromFile("../../2109790117_080129_77862164.tif");
if(img.HorizontalResolution != img.VerticalResolution)
{
const float resolution = 96F;

// get the physical dimensions of the document
SizeF size = new SizeF(img.Width / img.HorizontalResolution, img.Height
/ img.VerticalResolution);
Size pixelDimensions = new Size((int)(size.Width * resolution),
(int)(size.Height * resolution));

Bitmap newImage = new Bitmap(pixelDimensions.Width,
pixelDimensions.Height);
newImage.SetResolution(resolution, resolution);
using(Graphics g = Graphics.FromImage(newImage))
{
g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.DrawImage(img, new Rectangle(0, 0, pixelDimensions.Width,
pixelDimensions.Height),
0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
}

img = newImage;
}

string fn = Guid.NewGuid().ToString() + ".png";
img.Save(fn, System.Drawing.Imaging.ImageFormat.Png);
</code>

The image quality of the resize using GDI+ is much better than Photoshops.
I'm using NearestNeighbor in both cases but the results are not the same.

If anyone can shed some light, offer some pointers, whatever I would really
appreciate it. I need to keep the filesize as small as possible.

Thanks,
Steve


sklett
2/29/2008 2:22:44 PM
Hi Bob,

Thanks for the reply.
According to Photoshop, the png it saves is a 1BBP and the one gdi+ saved is
an 8BPP.
You are the guru, so I'm not taking photoshop's word for it just yet. ;0)

Is it possible that gdi+ could be saving the png as a 8bpp? Is there a
definitive way to determine the bit depth?

Thanks,
Steve

[quoted text, click to view]

Bob Powell [MVP]
2/29/2008 10:55:34 PM
It's possible that photoshop is saving the file in an 8 bit palettised mode
with an optimised colour palette. GDI+ will always save as a 32bit per pixel
(3*8bits +8 bits alpha)

Png compression is lossless so you can do nothng to change that.

The only other option would be to use the 3.0 framework image libraries that
have more possibilities. See the PngBitmapEncoder.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


[quoted text, click to view]
Bob Powell [MVP]
3/1/2008 1:56:54 PM
Hmm, I don't know if the encoder can do a 1bpp. I was unaware that it did
8bpp. Maybe this is what it does if the original image has a palette. I
guess you're saving line-art, in that case, why not go for a TIFF 1bpp fax
format?

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


[quoted text, click to view]
Steve K.
3/1/2008 3:53:39 PM

[quoted text, click to view]
Funny, the image I'm trying to save as PNG are fax tiffs ;0)

I wanted to save them as PNG so that I can display them in a web page and
also make them easy to view by any users.

I'm starting to think that I might revert back to saving them as TIFFs after
running into so much trouble with saving as PNGs.
My application displays a multi-page tiff to the user for data entry. Part
of the data entry process requires the users to select specific pages from
the tiff and "Attach" them to sections of the data entry. For example, they
might enter customer and order details from a single TIFF, the tiff has a
custom info page and an order page. When the data entry is complete and the
data saved in our ordering system I'm attaching the relevant pages to the
appropriate records (IE: customer info page to customer record).

So the requirement started as "I need to break apart the tiff to separate
images files" and I chose PNG (I really like PNG). Maybe a TIFF will be
fine. I could also create a PDF which would solve the problem of users
being able to open them up.

Can GDI+ save a 1BPP tiff? I seem to recal coming across an article
somewhere last night that was claiming .net "can't save 1BPP images" - it
could be that you can't get a Graphics object from a 1BPP image which would
be a problem for me. My TIFFs have a funky resolution (204 * 98) - I've
been resizing them with a square pixel aspect ratio and this requires a
Graphics object.

Lots to learn...

Thanks for your help,
Steve

Bob Powell [MVP]
3/2/2008 1:37:19 PM
See my site for the secrets of 1bpp tiffs.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


[quoted text, click to view]
Steve K.
3/11/2008 6:48:19 PM
Is this the one you are referring to or is there another one?
http://www.bobpowell.net/onebit.htm


[quoted text, click to view]

Bob Powell [MVP]
3/17/2008 11:05:56 PM
Yes. That's the one.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]
AddThis Social Bookmark Button