473,756 Members | 4,165 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 17246
"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
48240
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, EncoderParameters encoderParams)
1
5173
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
1770
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 all come out incredibly grainy. Is there away to change the color pallet the GIF uses when...
1
1997
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 generic error occurred in GDI+.
6
1229
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
1026
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
6570
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 the Save As dialog pointing to the directory of my choice? In other words, I would like to...
4
1526
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 if the project is called Test, then the subdirectory structure looks like Visual Studio 2005...
0
1344
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 error when saving a new website. This one in particular is a starter kit for the Business website. I...
0
9487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9904
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9884
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8736
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7285
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6556
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2697
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.