7 Replies - 413 Views - Last Post: 21 November 2016 - 09:49 AM Rate Topic: -----

#1 mhr92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-October 16

How to set pixel to the random pixel value?

Posted 20 November 2016 - 09:24 PM

I want to set pixel to different color based on the certain condition. First i selected 150 random pixel value after that i want to set those pixel values to different color which is red, blue and green. Here the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Prototype
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            OpenFileDialog dOpenFileDialog = new OpenFileDialog();
            if(dOpenFileDialog.ShowDialog()== DialogResult.OK)
            {
                try
                {
                    Bitmap indexedBitmap = (Bitmap)Image.FromFile(dOpenFileDialog.FileName);
                    int width = indexedBitmap.Width;
                    int height = indexedBitmap.Height;
                    Bitmap nonindexedBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
                    Graphics g = Graphics.FromImage(nonindexedBitmap);
                    g.DrawImage(indexedBitmap, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel);

                    bitmap = nonindexedBitmap;
                    pictureBox1.Image = nonindexedBitmap;
                }
                catch(Exception)
                {
                    throw;
                }
            }
           
        }


        Bitmap bitmap;
        Random random = new Random();
        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 150; ++i)
            {
                int x = random.Next(bitmap.Width);
                int y = random.Next(bitmap.Height);
                Color color = bitmap.GetPixel(x, y);
                pictureBox2.Image = bitmap;
                textBox1.Text += color.B.ToString() + " Location: " + x.ToString() + ";" + y.ToString() + System.Environment.NewLine;
                textBox1.Multiline = true;
                bitmap.SetPixel(x, y, Color.White);
                Debug.WriteLine(color);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Color color = Color.Red; //your desired colour
            Color color1 = Color.Green;
            Color color2 = Color.Blue;
            //Color color3 = Color.DarkRed;
            //Color color4 = Color.DarkGreen;
            //Color color5 = Color.DarkBlue;

            byte R = color.R; //for red colour
            byte B = color.B;
            byte G = color.G;

            int x = random.Next(bitmap.Width);
            int y = random.Next(bitmap.Height);
      

            for (x = 0; x < bitmap.Width; x++)
            {
               for (y = 0; y < bitmap.Height; y++)
               {
                    Color c = bitmap.GetPixel(x, y);

                   if ((c.A(x,y)>7 && c.A(x, y) <= 155)&&(c.A(x, y) >164  && c.A(x, y) <= 247)&& (c.A(x, y) > 10.56 && c.A(x, y) <= 219.772))
                    {
                        color = Color.FromArgb(R, c.B, c.G);//for red color
                        bitmap.SetPixel(x, y, color);
                    }

                    if ((c.A(x, y) > 0 && c.A(x, y) <= 164) && (c.A(x, y) > 155 && c.A(x, y) <= 247) && (c.A(x, y) > 48.911 && c.A(x, y) <= 238.916))
                    {
                        color2 = Color.FromArgb(B, d.B, d.G);//for blue color
                        bitmap.SetPixel(x, y, color2);
                    }

                    if ((c.A(x, y) > 7 && c.A(x, y) <= 155) && (c.A(x, y) > 164 && c.A(x, y) <= 247) && (c.A(x, y) > 28.565 && c.A(x, y) <= 219.692))
                    {
                        color1 = Color.FromArgb(G, f.B, f.G);//for green color
                        bitmap.SetPixel(x, y, color1);
                    }
                   
                }
            }
            // Set the PictureBox to display the image.
            pictureBox4.Image = bitmap;
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }

       
    }
}



Im stuck at button 4.

Is This A Good Question/Topic? 0
  • +

Replies To: How to set pixel to the random pixel value?

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5824
  • View blog
  • Posts: 19,831
  • Joined: 05-May 12

Re: How to set pixel to the random pixel value?

Posted 20 November 2016 - 09:53 PM

Double check your if conditions. For example, look at line 90: How can a single value C.A(x,y)be in 2 disjoint ranges at the same time?
Was This Post Helpful? 0
  • +
  • -

#3 mhr92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-October 16

Re: How to set pixel to the random pixel value?

Posted 20 November 2016 - 09:55 PM

I just edit my code. Here my new one:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Prototype
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            OpenFileDialog dOpenFileDialog = new OpenFileDialog();
            if(dOpenFileDialog.ShowDialog()== DialogResult.OK)
            {
                try
                {
                    Bitmap indexedBitmap = (Bitmap)Image.FromFile(dOpenFileDialog.FileName);
                    int width = indexedBitmap.Width;
                    int height = indexedBitmap.Height;
                    Bitmap nonindexedBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
                    Graphics g = Graphics.FromImage(nonindexedBitmap);
                    g.DrawImage(indexedBitmap, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel);

                    bitmap = nonindexedBitmap;
                    pictureBox1.Image = nonindexedBitmap;
                }
                catch(Exception)
                {
                    throw;
                }
            }
           
        }


        Bitmap bitmap;
        Random random = new Random();
        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 150; ++i)
            {
                int x = random.Next(bitmap.Width);
                int y = random.Next(bitmap.Height);
                Color color = bitmap.GetPixel(x, y);
                pictureBox2.Image = bitmap;
                textBox1.Text += color.B.ToString() + " Location: " + x.ToString() + ";" + y.ToString() + System.Environment.NewLine;
                textBox1.Multiline = true;
                bitmap.SetPixel(x, y, Color.White);
                Debug.WriteLine(color);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Color color = Color.Black; //your desired colour

            byte R = color.R; //for red colour

            int x = random.Next(bitmap.Width);
            int y = random.Next(bitmap.Height);

            for (x = 0; x < bitmap.Width; x++)
            {
                for (y = 0; y < bitmap.Height; y++)
                {
                    Color pixelColor = bitmap.GetPixel(x, y);
                    Color newColor = Color.FromArgb(R, pixelColor.B, pixelColor.G);
                    bitmap.SetPixel(x, y, newColor);
                }
            }
            // Set the PictureBox to display the image.
            pictureBox3.Image = bitmap;

        }

        private void button4_Click(object sender, EventArgs e)
        {
            Color color1 = Color.Red; //your desired colour
            Color color2 = Color.Green;
            Color color3 = Color.Blue;
            //Color color4 = Color.DarkRed;
            //Color color5 = Color.DarkGreen;
            //Color color6 = Color.DarkBlue;

            byte R = color1.R; //for red colour
            byte B = color2.B;
            byte G = color3.G;

            int x = random.Next(bitmap.Width);
            int y = random.Next(bitmap.Height);
      

            for (x = 0; x < bitmap.Width; x++)
            {
               for (y = 0; y < bitmap.Height; y++)
               {
                    Color color = bitmap.GetPixel(x, y);

                   if ((color.A>7 && color.A <= 155)&&(color.A >164  && color.A <= 247)&& (color.A > 10.56 && color.A <= 219.772))
                    {
                        color1 = Color.FromArgb(R, color.B, color.G);//for red color
                        bitmap.SetPixel(x, y, color1);
                    }

                    if ((color.A > 0 && color.A <= 164) && (color.A > 155 && color.A <= 247) && (color.A > 48.911 && color.A <= 238.916))
                    {
                        color3 = Color.FromArgb(B, color.B, color.G);//for blue color
                        bitmap.SetPixel(x, y, color3);
                    }

                    if ((color.A > 7 && color.A <= 155) && (color.A > 164 && color.A <= 247) && (color.A > 28.565 && color.A <= 219.692))
                    {
                        color2 = Color.FromArgb(G, color.B, color.G);//for green color
                        bitmap.SetPixel(x, y, color2);
                    }
                   
                }
            }
            // Set the PictureBox to display the image.
            pictureBox4.Image = bitmap;
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }

       
    }
}



There is no error but when i click button 4, it did not change to three color i want.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5824
  • View blog
  • Posts: 19,831
  • Joined: 05-May 12

Re: How to set pixel to the random pixel value?

Posted 20 November 2016 - 10:03 PM

You still have the same issue. Stop and think about it. Look at this code:
(color.A>7 && color.A <= 155)&&(color.A >164  && color.A <= 247)&& (color.A > 10.56 && color.A <= 219.772)



Or better written:
(    7 < color.A && color.A <= 155    ) &&
(  164 < color.A && color.A <= 247    ) &&
(10.56 < color.A && color.A <= 219.772)



Let's say that color.A is 25. So we get:
(    7 < color.A && color.A <= 155    ) &&   // true
(  164 < color.A && color.A <= 247    ) &&   // false
(10.56 < color.A && color.A <= 219.772)      // true



So true && false && true == false.

Let's try again. Let's say color.A is 200. So we get:
(    7 < color.A && color.A <= 155    ) &&   // false
(  164 < color.A && color.A <= 247    ) &&   // true
(10.56 < color.A && color.A <= 219.772)      // true



So false && true && true == false.
Was This Post Helpful? 0
  • +
  • -

#5 mhr92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-October 16

Re: How to set pixel to the random pixel value?

Posted 20 November 2016 - 10:30 PM

This the output:Attached Image

I want the white color in picturebox2 change into 3 different color using set pixel in the picturebox3. How to change that white color?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5824
  • View blog
  • Posts: 19,831
  • Joined: 05-May 12

Re: How to set pixel to the random pixel value?

Posted 21 November 2016 - 05:12 AM

So determine if the pixel is white in the source image, and pick up the corresponding color from the mapping image, and set it in the destination image.

You already know how to get and set the pixels based on the code above (unless you just copied and pasted other code without understanding it). It looks like the problem is that you are not correctly determining if the pixel is white. You could simply compare for equality to white. Or did you lie and you actually need a range of grays -- not simply white?
Was This Post Helpful? 0
  • +
  • -

#7 mhr92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-October 16

Re: How to set pixel to the random pixel value?

Posted 21 November 2016 - 08:09 AM

What I want to achieve is to change the white mark in the picturebox2 to three different color in picturebox3 but it did not change the white mark. I dont know what need to be done in order that white mark change to three different color.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5824
  • View blog
  • Posts: 19,831
  • Joined: 05-May 12

Re: How to set pixel to the random pixel value?

Posted 21 November 2016 - 09:49 AM

What needs to be done:
1. Change your pixel checks to get the "whiteness" of the pixel instead of just looking at the 3 RGB channels separately.
2. Add code to pick up the color from your 3rd picture.

This is tangent to you current question, but since this is brand new code, why are you writing code in WinForms instead of WPF? WinForms is at end of life, while WPF will give you an easy path forward to either Xamarin or Windows Universal.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1