Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I need to know why the following code alters the image.
Dim a = New Bitmap( _
My.Application.Info.DirectoryPath & "\a.png")
Dim b As New Bitmap(a)
b.Save("b.png", System.Drawing.Imaging.ImageFormat.Png)

And please don't reply
Dim b = a

I need to know why the colors in the resulting image are altered (i.e. white becomes 254, and other colors and alpha values change). I need to write this so that the resulting picture's colors match those of the original.
Posted

Hi,

Have a look here:
http://en.wikipedia.org/wiki/Lossy_compression[^]
Quote from Wikipedia page:
In information technology, "lossy" compression is a data encoding method that compresses data by discarding (losing) some of it.
 
Share this answer
 
Comments
kamikazehighland 16-Jan-13 13:46pm    
Hmm. Thanks. I was hoping to avoid that by using PNG. I'll have to look into it further.
Thomas Daniels 16-Jan-13 13:48pm    
Thanks.
You're welcome!
Sergey Alexandrovich Kryukov 16-Jan-13 19:03pm    
And now, I suggest how to resolve this problem by using lossless compression. Please see my solution.
—SA
Sergey Alexandrovich Kryukov 16-Jan-13 18:51pm    
Very good, a 5.
—SA
Thomas Daniels 17-Jan-13 11:40am    
Thank you!
You can use lossless compression, for example, in the TIFF container, which can support a number of different compression algorithms, most of them are lossless. PNG supports lossless compression as well. Please see:
http://en.wikipedia.org/wiki/Portable_Network_Graphics[^],
http://en.wikipedia.org/wiki/Tagged_Image_File_Format[^],
http://en.wikipedia.org/wiki/Lossless_data_compression[^].

Now, .NET classes do not support PNG compression parameters. Instead, you can consider using this library: http://optipng.sourceforge.net/[^].
Unfortunately, this is a C library. You can use it in your .NET projects via P/Invoke or C++/CLI (mixed mode).
You might want to search more in hope to find something better.

So, you might be better off with TIFF, as .NET FCL offers a class for TIFF support:
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.tiffbitmapencoder%28v=vs.90%29.aspx[^] (and later versions of .NET).

Good luck,
—SA
 
Share this answer
 
v2
Comments
kamikazehighland 16-Jan-13 19:31pm    
Thank you both! 5 stars each. I had always understood that PNG was lossless, so this threw me off. Even the wikipedia on lossy compression doesn't mention PNG. I'm glad I've found the cause.
Sergey Alexandrovich Kryukov 16-Jan-13 19:55pm    
Great. You are very welcome.
Good luck, call again.
—SA
Thomas Daniels 17-Jan-13 11:41am    
Good answer, +5!
Sergey Alexandrovich Kryukov 17-Jan-13 11:44am    
Thank you.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900