RGB image converted to grayscale image
using System; using ; using ; namespace ConsoleApp { class Program { static void Main(string[] args) { // Create RGB images Image img = new Bitmap("RGB image path"); // Get the Width and Height of RGB images int width = ; int height = ; // Create grayscale images Image grayImg = new Bitmap(width, height); // BytesPerPixel to obtain grayscale images int grayBytesPerPixel = (Color.Format32bppArgb); // Calculate the total number of pixels in a grayscale image int grayPixelCount = width * height; // Iterate through each pixel of the RGB image, convert it to a grayscale value and write to the grayscale image for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color c = (x, y); int r = (int)( / 255 * 255); int g = (int)( / 255 * 255); int b = (int)( / 255 * 255); int gray = (r + g + b) / 3; (x, y, (gray)); } } // Display grayscale image ("Grayscale image path"); } } }
Convert grayscale image to RGB image
using System; using ; using ; namespace ConsoleApp { class Program { static void Main(string[] args) { // Create grayscale images Image img = new Bitmap("Grayscale image path"); // Get the Width and Height of grayscale images int width = ; int height = ; // Create RGB images Image rgbImg = new Bitmap(width, height); // BytesPerPixel to obtain RGB images int rgbBytesPerPixel = (Color.Format32bppArgb); // Calculate the total number of pixels in the RGB image int rgbPixelCount = width * height; // Iterate through each pixel of the grayscale image, convert it to an RGB value and write to the RGB image for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color c = (x, y); int gray = ; (x, y, (gray, gray, gray)); } } // Display RGB image ("RGB image path"); } } }
This is the end of this article about the implementation of C# RGB images and grayscale images. For more related content on C# RGB images and grayscale images, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!