Home > Software design >  Remove gray from the photo c# windows forms
Remove gray from the photo c# windows forms

Time:10-06

I want to remove grayness from the picture in pictureBox1, as in the first photo below. The second photo shows the formulas that can be used to do this. I wrote an approximate code, but the compiler throws an error, that variables red1, green1 and blue1 cannot be written to Color newPixel due to type incompatibility. Please help me fixing my code.

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; 
 
namespace WindowsFormsApp2 
{ 
    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        private void pictureBox1_Click(object sender, EventArgs e) 
        { 
            OpenFileDialog dlg = new OpenFileDialog(); 
            dlg.Title = "Відкрити зображення"; 
            dlg.Filter = "jpg files (*.jpg)|*.jpg|All filles (*.*)|*.*"; 
            if (dlg.ShowDialog() == DialogResult.OK) 
            { 
                pictureBox1.Image = new Bitmap(dlg.OpenFile()); 
                pictureBox1.Height = pictureBox1.Image.Height; 
                pictureBox1.Width = pictureBox1.Image.Width; 
           
            } 
            dlg.Dispose(); 
            label1.Visible = false; 
        } 
 
        private void button1_Click(object sender, EventArgs e) 
        { 
            Bitmap input = new Bitmap(pictureBox1.Image); 
            Bitmap output = new Bitmap(input.Width, input.Height); 
            int[] red = new int[256]; 
            int[] green = new int[256]; 
            int[] blue = new int[256]; 
            for (int x = 0; x < input.Width; x  ) 
            { 
                for (int y = 0; y < input.Height; y  ) 
                { 
                    Color pixel = ((Bitmap)pictureBox1.Image).GetPixel(x, y); 
                    red[pixel.R]  ; 
                    green[pixel.G]  ; 
                    blue[pixel.B]  ; 
 
                    double Ri = red.Average(); 
                    double Gi = green.Average(); 
                    double Bi = blue.Average(); 
                    double Avg = (Ri Bi Gi)/3; 
                    double red1 = red (Avg/Ri); 
                    double green1 = green(Avg / Gi); 
                    double blue1 = blue(Avg / Bi); 
                    Color newPixel = ((Color)red1| (Color)green1 | (Color)blue1); 
                    output.SetPixel(x, y, Color.((int)newPixel)); 
                } 
            } 
            pictureBox2.Image = output; 
        } 
    } 
}

photo1

photo2

CodePudding user response:

You should use The output of the example

  • Related