Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using System.IO;
- namespace OpenSavePicture
- {
- public partial class Form1 : Form
- {
- Bitmap img;
- byte[] imgBytes;
- static float noisePercentage = 100;
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- OpenFileDialog f = new OpenFileDialog();
- f.Filter = "Image|*.bmp";
- if (f.ShowDialog() == DialogResult.OK)
- {
- imgBytes = File.ReadAllBytes(f.FileName);
- using (MemoryStream mStream = new MemoryStream(imgBytes))
- img = (Bitmap)Image.FromStream(mStream);
- pictureBox1.Width = img.Width;
- pictureBox1.Height = img.Height;
- pictureBox1.Image = img;
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- SaveFileDialog f = new SaveFileDialog();
- f.Filter = "Image|*.jpg;*.png";
- if (f.ShowDialog() == DialogResult.OK)
- {
- File.WriteAllBytes(f.FileName, imgBytes);
- }
- }
- private void button3_Click(object sender, EventArgs e)
- {
- Bitmap bm = GenerateNoise((Bitmap)pictureBox1.Image);
- pictureBox1.Image = bm;
- }
- public Bitmap GenerateNoise(Bitmap finalBmp) {
- noisePercentage = float.Parse(textBox1.Text);
- Random r = new Random();
- int xl = (int)(finalBmp.Width * noisePercentage);
- int yl = (int)(finalBmp.Height * noisePercentage);
- for (int x = 0; x < finalBmp.Width; x++) {
- for (int y = 0; y < finalBmp.Height; y++) {
- if (r.Next(0, 101) < noisePercentage) {
- Color curColor = finalBmp.GetPixel(r.Next(0, finalBmp.Width), r.Next(0, finalBmp.Height));
- if (curColor.Name == "ffffffff") {
- finalBmp.SetPixel(x, y, Color.Black);
- }
- else
- finalBmp.SetPixel(x, y, Color.White);
- }
- }
- }
- return finalBmp;
- }
- public Bitmap Smooth(Bitmap bmpInput) {
- Bitmap temp;
- Color c;
- float sum = 0;
- for (int i = 0; i < bmpInput.Width; i++) {
- for (int j = 0; j < bmpInput.Height; j++) {
- c = bmpInput.GetPixel(i, j);
- byte gray = (byte)(.333 * c.R + .333 * c.G + .333 * c.B);
- bmpInput.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
- }
- }
- temp = bmpInput;
- for (int i = 0; i <= bmpInput.Width - 3; i++)
- for (int j = 0; j <= bmpInput.Height - 3; j++) {
- for (int x = i; x <= i + 2; x++)
- for (int y = j; y <= j + 2; y++) {
- c = bmpInput.GetPixel(x, y);
- sum = sum + c.R;
- }
- int color = (int)Math.Round(sum / 9, 10);
- temp.SetPixel(i + 1, j + 1, Color.FromArgb(color, color, color));
- sum = 0;
- }
- bmpInput = temp;
- return bmpInput;
- }
- Bitmap Average(Bitmap bmpOrig) {
- var bitmap = new Bitmap(bmpOrig);
- var windowSize = int.Parse(textBox2.Text);
- for (int column = (windowSize - 1); column < (bitmap.Width - windowSize); column++) {
- for (int row = (windowSize - 1); row < (bitmap.Height - windowSize); row++) {
- double sumBlue = 0;
- double sumGreen = 0;
- double sumRed = 0;
- for (int i = -(windowSize - 1) / 2; i <= (windowSize - 1) / 2; i++) {
- for (int j = -(windowSize - 1) / 2; j <= (windowSize - 1) / 2; j++) {
- var pixelValue = bitmap.GetPixel(column + i, row + j);
- var red = pixelValue.R;
- var green = pixelValue.G;
- var blue = pixelValue.B;
- sumRed = sumRed + red;
- sumGreen = sumGreen + green;
- sumBlue = sumBlue + blue;
- }
- }
- var averageRed = (int)(sumRed / (windowSize * windowSize));
- var averageGreen = (int)(sumGreen / (windowSize * windowSize));
- var averageBlue = (int)(sumBlue / (windowSize * windowSize));
- bitmap.SetPixel(column, row, Color.FromArgb(averageRed, averageGreen, averageBlue));
- }
- }
- return bitmap;
- }
- private void button4_Click(object sender, EventArgs e) {
- Bitmap bm = Average((Bitmap)pictureBox1.Image);
- pictureBox1.Image = bm;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement