SIGN UP MEMBER LOGIN:    
ARTICLE

GrayScale Image in WPF

Posted by Mahesh Chand Articles | WPF with C# February 09, 2010
The PixelFormats class is used to represent image pixel formats. The class has about 2 dozens of static properties representing various pixel formsts such as BlackWhite, Gray16, Gray32Float, Gray8, Rgba64 and so on.
Reader Level:
Download Files:
 

The PixelFormats class is used to represent image pixel formats. The class has about 2 dozens of static properties representing various pixel formsts such as BlackWhite, Gray16, Gray32Float, Gray8, Rgba64 and so on.

 

To apply pixel formatting of an image, we use DestinationFormat property of FormatConvertedBitmap. The Source property of FormatConvertedBitmap is the original BitmapImage that you would like to convert to gray scale.

 

Listing 43 creates a FormatConvertedBitmap from a BitmapImage and sets its DestinationFormat to Gray8.

 

private void ConvertImageToGrayScaleImage()

{

    // Create an Image control

    Image grayImage = new Image();

 

    // Create a BitmapImage and sets its DecodePixelWidth and DecodePixelHeight

    BitmapImage bmpImage = new BitmapImage();

    bmpImage.BeginInit();

    bmpImage.UriSource = new Uri(@"C:\Images\Garden.jpg", UriKind.RelativeOrAbsolute);

    bmpImage.EndInit();

 

    // Create a new image using FormatConvertedBitmap and set DestinationFormat to GrayScale

    FormatConvertedBitmap grayBitmap = new FormatConvertedBitmap();

    grayBitmap.BeginInit();

    grayBitmap.Source = bmpImage;

    grayBitmap.DestinationFormat = PixelFormats.Gray8;

    grayBitmap.EndInit();

 

    // Set Source property of Image

    grayImage.Source = grayBitmap;

 

    LayoutRoot.Children.Add(grayImage);

}

 

Listing 43

The output of Listing 43 generates Figure 47 which is a gray image.

 

GrayScaleImg.jpg

Figure 47

Login to add your contents and source code to this article
share this article :
post comment
 

How to make Bitmap color transform to black-White , then Black-White convert to binary 01

Posted by ery ferdian May 13, 2011
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor