Click here to Skip to main content
15,887,328 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,

I'm having trouble in converting a PNG image to a grayscale image and preserving the existing transparency in the image.

Currently the conversion is done but all transparent pixels are converted to black (as clearly stated in documentation).

When searching about the subject, I found examples that convert the image pixel by pixel but I'm curious if there's more elegant ways.

Please note that I'm quite new to imaging.

Current conversion
C#
System.Windows.Media.Imaging.BitmapImage original = new System.Windows.Media.Imaging.BitmapImage();
System.Windows.Media.Imaging.FormatConvertedBitmap converted = new System.Windows.Media.Imaging.FormatConvertedBitmap();

original.BeginInit();
original.UriSource = new System.Uri("pack://application:,,,/Images/Add.png", System.UriKind.Absolute);
original.EndInit();


converted.BeginInit();
converted.Source = original;
converted.DestinationFormat = System.Windows.Media.PixelFormats.Gray32Float;
converted.AlphaThreshold = 100;
converted.EndInit();

Any help is appreciated.
Posted

1 solution

Hi,

I'm also no expert in imaging, but also interesting question for me.
I've read about converting each pixel too and I think its not the elegant way but "stable".
Another way could be extracting/store the RGB values (and Alpha Channel value) and create a new 32-Bit Grayscale image with it.
Do you had a view in some Graphic Librarys?
http://freeimage.sourceforge.net/features.html[^]

With Best Regards
 
Share this answer
 
v2
Comments
Wendelius 11-Apr-12 23:44pm    
Thanks for your answer. I'll have a try with the new image creation.

About graphics libraries, no I haven't tried any. Do you have a specific library in mind?
El_Codero 13-Apr-12 8:26am    
Any progress? I've updated my solution with link to FreeImage Graphics library. Best Regards

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