Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- Timer t1 = new Timer();
- Bitmap bmp = new Bitmap(@"D:\img.png");
- Bitmap outputImage;
- public Form1()
- {
- InitializeComponent();
- pictureBox1.Image = bmp;
- t1.Tick += new EventHandler(t1_Tick);
- t1.Interval = 100;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- outputImage = new Bitmap(bmp);
- x = 0;
- y = 0;
- t1.Start();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- pictureBox2.Image = toGreyscale(bmp);
- }
- private Bitmap toGreyscale(Bitmap source)
- {
- Bitmap outputImage = new Bitmap(source);
- for (int y = 0; y < source.Height; y++)
- {
- for (int x = 0; x < source.Width; x++)
- {
- int r, g, b;
- r = source.GetPixel(x, y).R;
- g = source.GetPixel(x, y).G;
- b = source.GetPixel(x, y).B;
- int greyscale = (r + g + b) / 3;
- Color greyscalecolor = new Color();
- greyscalecolor = Color.FromArgb(greyscale, greyscale, greyscale);
- outputImage.SetPixel(x, y, greyscalecolor);
- }
- }
- return outputImage;
- }
- int x, y;
- int r, g, b;
- int greyscale;
- Color greyscalecolor = new Color();
- void t1_Tick(object sender, EventArgs e)
- {
- if (x < bmp.Width)
- {
- r = bmp.GetPixel(x, y).R;
- g = bmp.GetPixel(x, y).G;
- b = bmp.GetPixel(x, y).B;
- greyscale = (r + g + b) / 3;
- greyscalecolor = Color.FromArgb(greyscale, greyscale, greyscale);
- outputImage.SetPixel(x, y, greyscalecolor);
- pictureBox2.Image = outputImage;
- pictureBox2.Refresh();
- x++;
- if (x >= bmp.Width)
- {
- y++;
- x = 0;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment