Guest User

Untitled

a guest
Apr 16th, 2011
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4.  
  5. namespace WindowsFormsApplication1
  6. {
  7.     public partial class Form1 : Form
  8.     {
  9.         Timer t1 = new Timer();
  10.         Bitmap bmp = new Bitmap(@"D:\img.png");
  11.         Bitmap outputImage;
  12.  
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.  
  17.             pictureBox1.Image = bmp;
  18.  
  19.             t1.Tick += new EventHandler(t1_Tick);
  20.             t1.Interval = 100;
  21.         }
  22.  
  23.         private void button1_Click(object sender, EventArgs e)
  24.         {
  25.             outputImage = new Bitmap(bmp);
  26.             x = 0;
  27.             y = 0;
  28.  
  29.             t1.Start();
  30.         }
  31.  
  32.         private void button2_Click(object sender, EventArgs e)
  33.         {
  34.             pictureBox2.Image = toGreyscale(bmp);
  35.         }
  36.  
  37.         private Bitmap toGreyscale(Bitmap source)
  38.         {
  39.             Bitmap outputImage = new Bitmap(source);
  40.  
  41.             for (int y = 0; y < source.Height; y++)
  42.             {
  43.                 for (int x = 0; x < source.Width; x++)
  44.                 {
  45.                     int r, g, b;
  46.  
  47.                     r = source.GetPixel(x, y).R;
  48.                     g = source.GetPixel(x, y).G;
  49.                     b = source.GetPixel(x, y).B;
  50.  
  51.                     int greyscale = (r + g + b) / 3;
  52.  
  53.                     Color greyscalecolor = new Color();
  54.                     greyscalecolor = Color.FromArgb(greyscale, greyscale, greyscale);
  55.  
  56.                     outputImage.SetPixel(x, y, greyscalecolor);
  57.                 }
  58.             }
  59.             return outputImage;
  60.         }
  61.  
  62.         int x, y;
  63.         int r, g, b;
  64.         int greyscale;
  65.         Color greyscalecolor = new Color();
  66.  
  67.         void t1_Tick(object sender, EventArgs e)
  68.         {    
  69.             if (x < bmp.Width)
  70.             {
  71.                 r = bmp.GetPixel(x, y).R;
  72.                 g = bmp.GetPixel(x, y).G;
  73.                 b = bmp.GetPixel(x, y).B;
  74.  
  75.                 greyscale = (r + g + b) / 3;
  76.  
  77.                 greyscalecolor = Color.FromArgb(greyscale, greyscale, greyscale);
  78.  
  79.                 outputImage.SetPixel(x, y, greyscalecolor);
  80.                 pictureBox2.Image = outputImage;
  81.                 pictureBox2.Refresh();
  82.                
  83.                 x++;
  84.  
  85.                 if (x >= bmp.Width)
  86.                 {
  87.                     y++;
  88.                     x = 0;
  89.                 }
  90.             }
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment