Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Drawing / March 2008

Tip: Looking for answers? Try searching our database.

struggling to save tiff as png and keep file size down

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sklett - 28 Feb 2008 21:39 GMT
(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
Bob Powell [MVP] - 29 Feb 2008 21:55 GMT
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.

Signature

--
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.

> (I posted this in the C# NG on accident, reposting here)
>
[quoted text clipped - 53 lines]
> Thanks,
> Steve
sklett - 29 Feb 2008 22:22 GMT
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

> 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
[quoted text clipped - 66 lines]
>> Thanks,
>> Steve
Bob Powell [MVP] - 01 Mar 2008 12:56 GMT
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?

Signature

--
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.

> Hi Bob,
>
[quoted text clipped - 80 lines]
>>> Thanks,
>>> Steve
Steve K. - 01 Mar 2008 23:53 GMT
> 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?

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] - 02 Mar 2008 12:37 GMT
See my site for the secrets of 1bpp tiffs.

Signature

--
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.

>> 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
[quoted text clipped - 33 lines]
> Thanks for your help,
> Steve
Steve K. - 12 Mar 2008 02:48 GMT
Is this the one you are referring to or is there another one?
http://www.bobpowell.net/onebit.htm

> See my site for the secrets of 1bpp tiffs.
>
[quoted text clipped - 35 lines]
>> Thanks for your help,
>> Steve
Bob Powell [MVP] - 17 Mar 2008 23:05 GMT
Yes. That's the one.

Signature

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.

> Is this the one you are referring to or is there another one?
> http://www.bobpowell.net/onebit.htm
[quoted text clipped - 38 lines]
>>> Thanks for your help,
>>> Steve

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.