DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • How to Convert Excel and CSV Documents to HTML in Java
  • Time Zone Change in Mule Application
  • A Complete Guide on How to Convert InputStream to String In Java
  • The Generic Way To Convert Between Java and PostgreSQL Enums

Trending

  • Best GitHub-Like Alternatives for Machine Learning Projects
  • LLMs for Bad Content Detection: Pros and Cons
  • The Emergence of Cloud-Native Integration Patterns in Modern Enterprises
  • Supercharging Productivity in Microservice Development With AI Tools

How to Convert an Image to Gray Scale With WPF

Christian Helle user avatar by
Christian Helle
·
Jul. 02, 12 · Interview
Like (0)
Save
Tweet
Share
9.20K Views

Join the DZone community and get the full member experience.

Join For Free
I've been playing with the Windows Presentation Foundation today and I had a task where I needed to convert an image to gray scale to do some image analysis on it. I've done this a bunch of times before using GDI methods or by accessing the BitmapData class in .NET. For this short post I'd like to demonstrate how to manipulate images using the WriteableBitmap class.

The easiest way to convert an image to gray scale is to set the RGB values of every pixel to the average of each pixels RBG values.

R = (R + B + G) / 3
G = (R + B + G) / 3
B = (R + B + G) / 3

Here's a code snippet for manipulating a BitmapSource object using the WriteableBitmap class into a gray scale image:

public unsafe static BitmapSource ToGrayScale(BitmapSource source)

{

    const int PIXEL_SIZE = 4;

    int width = source.PixelWidth;

    int height = source.PixelHeight;

    var bitmap = new WriteableBitmap(source);

 

    bitmap.Lock();

    var backBuffer = (byte*)bitmap.BackBuffer.ToPointer();

    for (int y = 0; y < height; y++)

    {

        var row = backBuffer + (y * bitmap.BackBufferStride);

        for (int x = 0; x < width; x++)

        {

            var grayScale = (byte)(((row[x * PIXEL_SIZE + 1]) + (row[x * PIXEL_SIZE + 2]) + (row[x * PIXEL_SIZE + 3])) / 3);

            for (int i = 0; i < PIXEL_SIZE; i++)

                row[x * PIXEL_SIZE + i] = grayScale;

        }

    }

    bitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));

    bitmap.Unlock();

 

    return bitmap;

}


Another way to to convert an image to gray scale is to set the RGB values of every pixel to the sum of 30% of the red value, 59% of the green value, and 11% of the blue value. Hope you find this useful.
Windows Presentation Foundation Convert (command)

Published at DZone with permission of Christian Helle, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert Excel and CSV Documents to HTML in Java
  • Time Zone Change in Mule Application
  • A Complete Guide on How to Convert InputStream to String In Java
  • The Generic Way To Convert Between Java and PostgreSQL Enums

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: