0% found this document useful (0 votes)
17 views2 pages

Source Code Grayscale: Using Using Using Using Using Using Using Using

This document contains C# source code for converting an image to grayscale. It defines classes and methods for opening an image file, storing the image as a Bitmap object, and iterating through each pixel to calculate a grayscale value based on the RGB components. The RGB values of each pixel are replaced with the grayscale value to convert the image before displaying the result.

Uploaded by

Frasca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Source Code Grayscale: Using Using Using Using Using Using Using Using

This document contains C# source code for converting an image to grayscale. It defines classes and methods for opening an image file, storing the image as a Bitmap object, and iterating through each pixel to calculate a grayscale value based on the RGB components. The RGB values of each pixel are replaced with the grayscale value to convert the image before displaying the result.

Uploaded by

Frasca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

SOURCE CODE

GRAYSCALE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Percobaan_1
{
public partial class Form1 : Form
{
Bitmap gambar;
public Form1()
{
InitializeComponent();
}

private void openImageToolStripMenuItem_Click(object sender, EventArgs e)


{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = "c:\\";
ofd.Filter = "All Image Files(*.bmp;*.png;*.tif;*.jpg)|*.bmp;*.png;*.tif;*.jpg|24-Bit
Bitmap(*.bmp)|*.bmp|PNG(*.png)|*.png|TIFF(*.tif)|*.tif|JPEG(*.jpg)|*.jpg";
if (ofd.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = System.Drawing.Image.FromFile(ofd.FileName);
gambar = new Bitmap(pictureBox1.Image);
}
}

private void grayscaleImageToolStripMenuItem_Click(object sender, EventArgs e)


{
{
for (int x = 0; x < gambar.Width; x++)
{
for (int y = 0; y < gambar.Height; y++)
{
Color originalcolor = gambar.GetPixel(x, y);

int grayscale = (int)((originalcolor.R * .3) + (originalcolor.G * .59) + (originalcolor.B * .11));


Color newcolor = Color.FromArgb(grayscale, grayscale, grayscale);

gambar.SetPixel(x, y, newcolor);

}
}
pictureBox1.Image = gambar;
pictureBox1.Invalidate();
}
}
}
}

You might also like