Click here to Skip to main content
13,189,324 members (68,003 online)
Rate this:
Please Sign up or sign in to 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 16-Jan-13 7:35am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


Have a look here:[^]
Quote from Wikipedia page:
In information technology, "lossy" compression is a data encoding method that compresses data by discarding (losing) some of it.
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.
ProgramFOX 16-Jan-13 13:48pm
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.
Sergey Alexandrovich Kryukov 16-Jan-13 18:51pm
Very good, a 5.
ProgramFOX 17-Jan-13 11:40am
Thank you!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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:[^],[^],[^].

Now, .NET classes do not support PNG compression parameters. Instead, you can consider using this library:[^].
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:[^] (and later versions of .NET).

Good luck,
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.
ProgramFOX 17-Jan-13 11:41am
Good answer, +5!
Sergey Alexandrovich Kryukov 17-Jan-13 11:44am
Thank you.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171016.2 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100