Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.19 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.IO;
  5.  
  6. namespace OpenSavePicture
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.         Bitmap img;
  11.         byte[] imgBytes;
  12.         static float noisePercentage = 100;
  13.  
  14.  
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             OpenFileDialog f = new OpenFileDialog();
  23.             f.Filter = "Image|*.bmp";
  24.  
  25.             if (f.ShowDialog() == DialogResult.OK)
  26.             {
  27.                 imgBytes = File.ReadAllBytes(f.FileName);
  28.  
  29.                 using (MemoryStream mStream = new MemoryStream(imgBytes))
  30.                     img = (Bitmap)Image.FromStream(mStream);
  31.  
  32.  
  33.                 pictureBox1.Width = img.Width;
  34.                 pictureBox1.Height = img.Height;
  35.                 pictureBox1.Image = img;
  36.             }
  37.         }
  38.  
  39.  
  40.  
  41.         private void button2_Click(object sender, EventArgs e)
  42.         {
  43.             SaveFileDialog f = new SaveFileDialog();
  44.             f.Filter = "Image|*.jpg;*.png";
  45.  
  46.             if (f.ShowDialog() == DialogResult.OK)
  47.             {
  48.                 File.WriteAllBytes(f.FileName, imgBytes);
  49.             }
  50.         }
  51.  
  52.         private void button3_Click(object sender, EventArgs e)
  53.         {
  54.             Bitmap bm = GenerateNoise((Bitmap)pictureBox1.Image);
  55.             pictureBox1.Image = bm;
  56.         }
  57.  
  58.         public Bitmap GenerateNoise(Bitmap finalBmp) {
  59.  
  60.             noisePercentage = float.Parse(textBox1.Text);
  61.  
  62.             Random r = new Random();
  63.  
  64.             int xl = (int)(finalBmp.Width * noisePercentage);
  65.             int yl = (int)(finalBmp.Height * noisePercentage);
  66.  
  67.             for (int x = 0; x < finalBmp.Width; x++) {
  68.                 for (int y = 0; y < finalBmp.Height; y++) {
  69.  
  70.                     if (r.Next(0, 101) < noisePercentage) {
  71.  
  72.                         Color curColor = finalBmp.GetPixel(r.Next(0, finalBmp.Width), r.Next(0, finalBmp.Height));
  73.  
  74.                         if (curColor.Name == "ffffffff") {
  75.                             finalBmp.SetPixel(x, y, Color.Black);
  76.                         }
  77.                         else
  78.                             finalBmp.SetPixel(x, y, Color.White);
  79.                     }
  80.                 }
  81.             }
  82.  
  83.             return finalBmp;
  84.         }
  85.  
  86.  
  87.         public Bitmap Smooth(Bitmap bmpInput) {
  88.  
  89.             Bitmap temp;
  90.  
  91.             Color c;
  92.             float sum = 0;
  93.             for (int i = 0; i < bmpInput.Width; i++) {
  94.                 for (int j = 0; j < bmpInput.Height; j++) {
  95.                     c = bmpInput.GetPixel(i, j);
  96.                     byte gray = (byte)(.333 * c.R + .333 * c.G + .333 * c.B);
  97.                     bmpInput.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
  98.                 }
  99.             }
  100.  
  101.             temp = bmpInput;
  102.             for (int i = 0; i <= bmpInput.Width - 3; i++)
  103.                 for (int j = 0; j <= bmpInput.Height - 3; j++) {
  104.                     for (int x = i; x <= i + 2; x++)
  105.                         for (int y = j; y <= j + 2; y++) {
  106.                             c = bmpInput.GetPixel(x, y);
  107.                             sum = sum + c.R;
  108.                         }
  109.                     int color = (int)Math.Round(sum / 9, 10);
  110.                     temp.SetPixel(i + 1, j + 1, Color.FromArgb(color, color, color));
  111.                     sum = 0;
  112.                 }
  113.             bmpInput = temp;
  114.  
  115.             return bmpInput;
  116.         }
  117.  
  118.  
  119.         Bitmap Average(Bitmap bmpOrig) {
  120.  
  121.             var bitmap = new Bitmap(bmpOrig);
  122.             var windowSize = int.Parse(textBox2.Text);
  123.             for (int column = (windowSize - 1); column < (bitmap.Width - windowSize); column++) {
  124.                 for (int row = (windowSize - 1); row < (bitmap.Height - windowSize); row++) {
  125.                     double sumBlue = 0;
  126.                     double sumGreen = 0;
  127.                     double sumRed = 0;
  128.                     for (int i = -(windowSize - 1) / 2; i <= (windowSize - 1) / 2; i++) {
  129.                         for (int j = -(windowSize - 1) / 2; j <= (windowSize - 1) / 2; j++) {
  130.                             var pixelValue = bitmap.GetPixel(column + i, row + j);
  131.                             var red = pixelValue.R;
  132.                             var green = pixelValue.G;
  133.                             var blue = pixelValue.B;
  134.                             sumRed = sumRed + red;
  135.                             sumGreen = sumGreen + green;
  136.                             sumBlue = sumBlue + blue;
  137.                         }
  138.                     }
  139.                     var averageRed = (int)(sumRed / (windowSize * windowSize));
  140.                     var averageGreen = (int)(sumGreen / (windowSize * windowSize));
  141.                     var averageBlue = (int)(sumBlue / (windowSize * windowSize));
  142.                     bitmap.SetPixel(column, row, Color.FromArgb(averageRed, averageGreen, averageBlue));
  143.                 }
  144.             }
  145.  
  146.             return bitmap;
  147.         }
  148.  
  149.         private void button4_Click(object sender, EventArgs e) {
  150.             Bitmap bm = Average((Bitmap)pictureBox1.Image);
  151.             pictureBox1.Image = bm;
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement