Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB VB.NET
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 8:35am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
kamikazehighland at 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 at 16-Jan-13 13:48pm
   
Thanks.
You're welcome!
Sergey Alexandrovich Kryukov at 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 at 16-Jan-13 18:51pm
   
Very good, a 5.
—SA
ProgramFOX at 17-Jan-13 11:40am
   
Thank you!
Rate this: bad
good
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:
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
  Permalink  
v2
Comments
kamikazehighland at 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 at 16-Jan-13 19:55pm
   
Great. You are very welcome.
Good luck, call again.
—SA
ProgramFOX at 17-Jan-13 11:41am
   
Good answer, +5!
Sergey Alexandrovich Kryukov at 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)

  Print Answers RSS
0 OriginalGriff 295
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 Sergey Alexandrovich Kryukov 174
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
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