Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save edward1986/ed1876f37482db3d1260a422c309e99f to your computer and use it in GitHub Desktop.
Save edward1986/ed1876f37482db3d1260a422c309e99f to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace WindowsColorGray
{
public partial class Form1 : Form
{
Bitmap bit;
Image file;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
Bitmap grayScaleBP = new System.Drawing.Bitmap(2, 2, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
private void button1_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == null)
{
return;
}
pictureBox1.Image = bit;
for (int x = 0; x < bit.Width; x++)
{
for (int y = 0; y < bit.Height; y++)
{
Color originalColor = bit.GetPixel(x, y);
int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59) + (originalColor.B * .11));
Color newColor = Color.FromArgb(grayScale, grayScale, grayScale);
bit.SetPixel(x, y, newColor);
}
}
pictureBox1.Image = bit;
}
private void button2_Click(object sender, EventArgs e)
{
openFileDialog1.FileName = "";
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
file = Image.FromFile(openFileDialog1.FileName.ToString());
pictureBox1.Image = file;
bit = new Bitmap(openFileDialog1.FileName.ToString());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment