473,548 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ImageCodecInfoJ PG = GetEncoderInfo( "image/jpeg");
EncoderParamete rs EP = new EncoderParamete rs(2);
EP.Param(0) = new EncoderParamete r(Encoder.Quali ty,
QualityLevel_0_ 100);
EP.Param(1) = new EncoderParamete r(Encoder.Color Depth,
ColorDepthLevel );
Image.Save(File Name, ImageCodecInfoJ PG, 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 ImageCodecInfoP NG = GetEncoderInfo( "image/png");

EncoderParamete rs EP = new EncoderParamete rs(2);
EP.Param(0) = new EncoderParamete r(Encoder.Compr ession, 100l -
QualityLevel_0_ 100);
EP.Param(1) = new EncoderParamete r(Encoder.Color Depth,
ColorDepthLevel );
Image.Save(File Name, ImageCodecInfoP NG, EP);
}
-P

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

public void SaveJPG(Image Image, string FileName, long
QualityLevel_0_ 100, long ColorDepthLevel )
{
ImageCodecInfo ImageCodecInfoJ PG = GetEncoderInfo( "image/jpeg");
EncoderParamete rs EP = new EncoderParamete rs(2);
EP.Param(0) = new EncoderParamete r(Encoder.Quali ty,
QualityLevel_0_ 100);
EP.Param(1) = new EncoderParamete r(Encoder.Color Depth,
ColorDepthLevel );
Image.Save(File Name, ImageCodecInfoJ PG, 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 "compressio n 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 "compressio n 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.co m>
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.goog legroups.com...
I have been using something like:
public void SaveJPG(Image Image, string FileName, long
QualityLevel_0_ 100, long ColorDepthLevel )
{
ImageCodecInfo ImageCodecInfoJ PG = GetEncoderInfo( "image/jpeg");
EncoderParamete rs EP = new EncoderParamete rs(2);
EP.Param(0) = new EncoderParamete r(Encoder.Quali ty,
QualityLevel_0_ 100);
EP.Param(1) = new EncoderParamete r(Encoder.Color Depth,
ColorDepthLevel );
Image.Save(File Name, ImageCodecInfoJ PG, 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 "compressio n 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
"compressio n", one can specify
a "compressio n 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...@Nn OwSlPiAnMk.comw rote:
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...@Nn OwSlPiAnMk.comw rote:
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
48193
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+. System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder,...
1
5104
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 = "c:\\Inetpub\\wwwroot\\MyWeb\\upload\\File_1"; try { myFile.SaveAs(fn); lblMsg.Text = "File was uploaded and saved successfully!";
2
1755
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, I can't seem to manipulate the GIF using the same parameter. All my GIFs appear to be using a restricted pallette (reduced windows system?) and they...
1
1983
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: An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in system.drawing.dll Additional information: A...
6
1218
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 anywhere else, it saves very slowly (takes about 1 minute), on the same file(s). Any ideas? Thanks! Chad
2
1012
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, I'd like to change the JPG compression it uses. I apparently can do that with an EncoderParameter:
1
6518
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). Since this is not where I want to save my source file, I have to select the directory I want. Is there a way that I can have python always bring up...
4
1503
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 for it. If I click the checkbox, the new subdirectory gets the same name as the higher subdirectory (which is the application name). For example...
0
1327
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 website appears in my browser with pics so that tells me that data is being retrieved from the db file. Not sure what is going on. I receive this...
0
7444
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7711
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7954
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6039
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5367
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5085
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
755
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.