473,372 Members | 1,329 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

How to specify COMPRESSION level when saving (loseless) PNGs ?

I have been using something like:

public void SaveJPG(Image Image, string FileName, long
QualityLevel_0_100, long ColorDepthLevel)
{
ImageCodecInfo ImageCodecInfoJPG = GetEncoderInfo("image/jpeg");
EncoderParameters EP = new EncoderParameters(2);
EP.Param(0) = new EncoderParameter(Encoder.Quality,
QualityLevel_0_100);
EP.Param(1) = new EncoderParameter(Encoder.ColorDepth,
ColorDepthLevel);
Image.Save(FileName, ImageCodecInfoJPG, EP);
}
to save JPG with some give given quality. I expected to do the same
with PNG. I know its a loseless format, but I expect to be able to
control the compression level. Is that right or no ?

My PNG are ways too big (relative to similar jpg's). Can anyone tell
me what am I doing wrong. The following code (invented) is not
working. What's the right way to do it?

public void SavePNG(Image Image, string FileName, long
QualityLevel_0_100, long ColorDepthLevel)
{
ImageCodecInfo ImageCodecInfoPNG = GetEncoderInfo("image/png");

EncoderParameters EP = new EncoderParameters(2);
EP.Param(0) = new EncoderParameter(Encoder.Compression, 100l -
QualityLevel_0_100);
EP.Param(1) = new EncoderParameter(Encoder.ColorDepth,
ColorDepthLevel);
Image.Save(FileName, ImageCodecInfoPNG, EP);
}
-P

Aug 29 '07 #1
7 17164
"pamela fluente" <pa***********@libero.itwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
>I have been using something like:

public void SaveJPG(Image Image, string FileName, long
QualityLevel_0_100, long ColorDepthLevel)
{
ImageCodecInfo ImageCodecInfoJPG = GetEncoderInfo("image/jpeg");
EncoderParameters EP = new EncoderParameters(2);
EP.Param(0) = new EncoderParameter(Encoder.Quality,
QualityLevel_0_100);
EP.Param(1) = new EncoderParameter(Encoder.ColorDepth,
ColorDepthLevel);
Image.Save(FileName, ImageCodecInfoJPG, EP);
}
to save JPG with some give given quality. I expected to do the same
with PNG. I know its a loseless format, but I expect to be able to
control the compression level. Is that right or no ?
No
My PNG are ways too big (relative to similar jpg's). Can anyone tell
me what am I doing wrong. The following code (invented) is not
working. What's the right way to do it?
Your PNGs are so big because nothing is LOST in the compression. That's why
Jpeg is able to compress so well. Specifying the amount of information
you're willing to LOSE for a smaller size, gives it a lot more flexibility.

Just out of curiousity, what would you think the "compression level" you
want out of PNG to do? It can't use less information than the exact colors
of every pixel compressed as well as its algorithm allows. So what else is
there for it to trade off?

Aug 29 '07 #2
pamela fluente wrote:
[...]
to save JPG with some give given quality. I expected to do the same
with PNG. I know its a loseless format, but I expect to be able to
control the compression level. Is that right or no ?

My PNG are ways too big (relative to similar jpg's).
You should not expect to be able to control the compression level in the
way that you are doing.

PNG does support a variety of parameters for controlling how the
compression is done. But different parameters will result in better
results for different input images.

JPEG's compression level is essentially a control over how much
information to throw away. Higher quality discards less information,
lower quality discards more. The more information you throw out, the
smaller the file. .NET provides a simple 0 to 100 parameter to control
how much information is discarded.

PNG's parameters (assuming you have an implementation that provides
access to them) control varying ways that the algorithm can deal with
the input data. A given set of parameters will not always produce the
smallest files; for a given input image, the smallest file is obtained
with a specific set of parameters, but a different input image may
require a different set of parameters.

The only way to optimize (make the smallest) the output of PNG
compression is to try a wide variety of parameters and pick the best
results. This means compressing the image multiple times, and depending
on how optimal you want to get, the number of times could be very large.

You should not, in any case, expect the size of a PNG-compressed file to
approximate the size of a JPEG-compressed file. The two algorithms
serve very different purposes, and aren't really comparable. JPEG
throws out data, which makes it a lot easier to get very small files.
But because it throws out data, it works best with images for which you
won't notice the loss of data, like photographs or elaborate drawings.

For basic images, like bitmaps with text or simple drawings, compressing
using JPEG can produce very unlikable results. The image will have
"artifacts" all over it, causing clean lines to get blurry or distorted.
On the other hand, for simple images like this, PNG's compression,
even a default mode, can often do a pretty good job.

Whether it can do a better job than JPEG depends a lot on the input data
and how you are using each compression technique. But generally
speaking, JPEG isn't going to provide anywhere near the equivalent
quality for simple images like those PNG is well-suited for, especially
when it's used in a way that produces a file significantly smaller than PNG.

Now, all that said...as far as I know, .NET's support for PNG offers
basically no options for controlling the parameters for PNG. So you
don't really have a choice, if you're going to use .NET's PNG support.
There are other options though.

Here's a command-line utility that does allow for control of various
parameters:
http://advsys.net/ken/util/pngout.htm

Here's a .NET program that uses that tool, but via a GUI:
http://brh.numbera.com/software/pnggauntlet/

Here's a web site full of useful information about PNG:
http://www.libpng.org/pub/png/

Here's that web site's introduction of PNG to beginners:
http://www.libpng.org/pub/png/pngintro.html

Hope that helps.

Pete
Aug 29 '07 #3
<"pedrito" <pixbypedrito at yahoo.com>wrote:

<snip>
Just out of curiousity, what would you think the "compression level" you
want out of PNG to do? It can't use less information than the exact colors
of every pixel compressed as well as its algorithm allows. So what else is
there for it to trade off?
Well, potentially time spent doing compression. Most zip tools allow
you to express a preference as to how "hard" you want them to try to
compress, but it's still lossless.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 29 '07 #4
On 29 Ago, 20:48, "pedrito" <pixbypedrito at yahoo.comwrote:
"pamela fluente" <pamelaflue...@libero.itwrote in message

news:11**********************@19g2000hsx.googlegro ups.com...
I have been using something like:
public void SaveJPG(Image Image, string FileName, long
QualityLevel_0_100, long ColorDepthLevel)
{
ImageCodecInfo ImageCodecInfoJPG = GetEncoderInfo("image/jpeg");
EncoderParameters EP = new EncoderParameters(2);
EP.Param(0) = new EncoderParameter(Encoder.Quality,
QualityLevel_0_100);
EP.Param(1) = new EncoderParameter(Encoder.ColorDepth,
ColorDepthLevel);
Image.Save(FileName, ImageCodecInfoJPG, EP);
}
to save JPG with some give given quality. I expected to do the same
with PNG. I know its a loseless format, but I expect to be able to
control the compression level. Is that right or no ?

No
My PNG are ways too big (relative to similar jpg's). Can anyone tell
me what am I doing wrong. The following code (invented) is not
working. What's the right way to do it?

Your PNGs are so big because nothing is LOST in the compression. That's why
Jpeg is able to compress so well. Specifying the amount of information
you're willing to LOSE for a smaller size, gives it a lot more flexibility.

Just out of curiousity, what would you think the "compression level" you
want out of PNG to do? It can't use less information than the exact colors
of every pixel compressed as well as its algorithm allows. So what else is
there for it to trade off?- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -
I do not know exactly. I have seen that usually when there is a
"compression", one can specify
a "compression level". So I was wondering whether there are parameters
that need to be adjusted
to control somehow the size. I could not find them

-P

Aug 31 '07 #5
On 29 Ago, 20:59, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
pamela fluente wrote:
[...]
to save JPG with some give given quality. I expected to do the same
with PNG. I know its a loseless format, but I expect to be able to
control the compression level. Is that right or no ?
My PNG are ways too big (relative to similar jpg's).

You should not expect to be able to control the compression level in the
way that you are doing.

PNG does support a variety of parameters for controlling how the
compression is done. But different parameters will result in better
results for different input images.

JPEG's compression level is essentially a control over how much
information to throw away. Higher quality discards less information,
lower quality discards more. The more information you throw out, the
smaller the file. .NET provides a simple 0 to 100 parameter to control
how much information is discarded.

PNG's parameters (assuming you have an implementation that provides
access to them) control varying ways that the algorithm can deal with
the input data. A given set of parameters will not always produce the
smallest files; for a given input image, the smallest file is obtained
with a specific set of parameters, but a different input image may
require a different set of parameters.

The only way to optimize (make the smallest) the output of PNG
compression is to try a wide variety of parameters and pick the best
results. This means compressing the image multiple times, and depending
on how optimal you want to get, the number of times could be very large.

You should not, in any case, expect the size of a PNG-compressed file to
approximate the size of a JPEG-compressed file. The two algorithms
serve very different purposes, and aren't really comparable. JPEG
throws out data, which makes it a lot easier to get very small files.
But because it throws out data, it works best with images for which you
won't notice the loss of data, like photographs or elaborate drawings.

For basic images, like bitmaps with text or simple drawings, compressing
using JPEG can produce very unlikable results. The image will have
"artifacts" all over it, causing clean lines to get blurry or distorted.
On the other hand, for simple images like this, PNG's compression,
even a default mode, can often do a pretty good job.

Whether it can do a better job than JPEG depends a lot on the input data
and how you are using each compression technique. But generally
speaking, JPEG isn't going to provide anywhere near the equivalent
quality for simple images like those PNG is well-suited for, especially
when it's used in a way that produces a file significantly smaller than PNG.

Now, all that said...as far as I know, .NET's support for PNG offers
basically no options for controlling the parameters for PNG. So you
don't really have a choice, if you're going to use .NET's PNG support.
There are other options though.

Here's a command-line utility that does allow for control of various
parameters:http://advsys.net/ken/util/pngout.htm

Here's a .NET program that uses that tool, but via a GUI:http://brh.numbera.com/software/pnggauntlet/

Here's a web site full of useful information about PNG:http://www.libpng.org/pub/png/

Here's that web site's introduction of PNG to beginners:http://www.libpng.org/pub/png/pngintro.html

Hope that helps.

Pete
Tha's all very interesting. But back to my specific issue, what about
within .NET ?

Can I adjust the encoder parameters? And if yes, how? Is there any
example ?

-P

Aug 31 '07 #6
pamela fluente wrote:
Tha's all very interesting. But back to my specific issue, what about
within .NET ?

Can I adjust the encoder parameters? And if yes, how? Is there any
example ?
As I wrote in my post, which you quoted in its entirety:
>Now, all that said...as far as I know, .NET's support for PNG offers
basically no options for controlling the parameters for PNG. So you
don't really have a choice, if you're going to use .NET's PNG support.
Was there something about that statement that confused you?
Aug 31 '07 #7
On 31 Ago, 18:11, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
pamela fluente wrote:
Tha's all very interesting. But back to my specific issue, what about
within .NET ?
Can I adjust the encoder parameters? And if yes, how? Is there any
example ?

As I wrote in my post, which you quoted in its entirety:
Now, all that said...as far as I know, .NET's support for PNG offers
basically no options for controlling the parameters for PNG. So you
don't really have a choice, if you're going to use .NET's PNG support.

Was there something about that statement that confused you?
Yes I understood that. But was a kind of denial :-)

Just wanted to make sure whether, shamefully, there is NO HOPE for
..NET programmers :-((( to output more reasonable PNGs.
-P :-)) Thanks a lot !

Sep 3 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Kennedy [UB] | last post by:
Hi Everyone, I have this multithreaded C# windows forms application which does a lot of image processing. Occasionally, I get the following error: A generic error occurred in GDI+....
1
by: terrorix | last post by:
I want to save uploaded file to disk. I have this construction: HttpPostedFile myFile = ((HttpRequest)Request).Files; if (myFile != null) { string fn =...
2
by: darrel | last post by:
I have written an image upload/resize tool that then saves out the image as either a JPG or GIF. I got the JPG save working nicely, using a codec and setting the compression to 90%. However,...
1
by: M Keeton | last post by:
I currently have a picture which is stored in a "System.Drawing.Image" variable and I want to save it as a bitmap file. I have tried 2 different approaches and both give me the following error: ...
6
by: Chad A. Beckner | last post by:
Hi everyone, Ok, here's the deal. I have a laptop installed with VS 2005. I'm programming in C#. When I am at home, connected to my LinkSys router, it saves fine. However, whenever I am...
2
by: darrel | last post by:
I'm currently saving a JPG file as such: imgOutput.Save("path/filename.jpg", System.Drawing.Imaging.ImageFormat.Jpeg) That saves the imgOutput as a JPG to the path shown. Works fine. Now,...
1
by: JohnJohnUSA | last post by:
I am using Python IDLE 2.4.3 on Windows XP. I use File->New Window to create a new program. In the Save As dialog, it always takes me to the python program directory (where python is installed)....
4
by: Steve | last post by:
When I create a new project in 2005, and I go to save the project files, vb.net displays a dialog box that repeats the name of the application and has a checkbox if I want to create a new directory...
0
by: Tony K | last post by:
ERROR MESSAGE RECEIVED WHEN SAVING. "The operation could not be completed. No such interface supported." Dell Inspirion E1705, 1GB RAM, Windows Vista Ultimate I can start debugging and the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.