11
Answers

SetPixel method in C#

Christos Alexiou

Christos Alexiou

14y
15.5k
1
Hello to everyone,

I'm writing code for an application form with a pictureBox inside it.
All i want to do is, to load an image (in a picture box)  and after that to change pixels in this image.
After searching, I've already read that SetPixel Method-in C#-can help me with that!
In PaintEvent of pictureBox the code is:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            // Create a Bitmap object from a file.
            Bitmap myBitmap = new Bitmap("D:\\...\\SP_A0195.jpg");
            // Draw myBitmap to the screen.
            e.Graphics.DrawImage(myBitmap,0,0,myBitmap.Width,myBitmap.Height);
            // Set each pixel in myBitmap to red.
            for (int Xcount = 0; Xcount < myBitmap.Width; Xcount++)
            {
                for (int Ycount = 0; Ycount < myBitmap.Height; Ycount++)
                {
                    myBitmap.SetPixel(Xcount, Ycount, Color.Red);
                }
            }
            // Draw myBitmap to the screen again.
            e.Graphics.DrawImage(myBitmap,myBitmap.Width,0,myBitmap.Width,myBitmap.Height);

        }

The code above doesn't work but i can not understand why...please help on how to do that.

Thank you in advance for your time!
Answers (11)
1
Jaish Mathews

Jaish Mathews

NA 7.3k 1.2m 14y

//2nd button event handler
private void button2_Click(object sender, EventArgs e)
{
//Passing the filename selected in OpenDialog
   SetMyPicture(open.FileName);
}
//Method to process the 1st Image and setting the processed image to 2nd PictureBox
private void SetMyPicture(string image)
        {
            Bitmap myBitmap = new Bitmap(image);
            for (int Xcount = 0; Xcount < myBitmap.Width; Xcount++)
            {
                for (int Ycount = 0; Ycount < myBitmap.Height; Ycount++)
                {
                    myBitmap.SetPixel(Xcount, Ycount, Color.Red);
                }
            }
         //Setting the 2nd picture box
            pictureBox2.Image = myBitmap;
        }
Accepted
1
Jaish Mathews

Jaish Mathews

NA 7.3k 1.2m 14y

Hi,
This is for your 2nd set of Q.
Use 2 PictureBoxes. 1st one for displaying real image and 2nd for displaying rocessed modified image.
Also use 2 butttons. 1st one for loading original image and 2nd for executing our new method which will process the original image and set it to the 2nd Picture box.
 
1
Jaish Mathews

Jaish Mathews

NA 7.3k 1.2m 14y

 
Alright. Belwo are details.
1. Create a Form_Load event handler, which will be fired once Form is loading. Call our new method inside it as you need to set your Image once Form loading. Below is my EventHandler code.

private void Form1_Load(object sender, EventArgs e)
        {
            SetMyPicture(@"E:\JAISH\img.jpg");
           
        }

2.  The new method created will be an private method to the Form class you can place any where inside class.

private void SetMyPicture(string image)
        {
            Bitmap myBitmap = new Bitmap(image);
            for (int Xcount = 0; Xcount < myBitmap.Width; Xcount++)
            {
                for (int Ycount = 0; Ycount < myBitmap.Height; Ycount++)
                {
                    myBitmap.SetPixel(Xcount, Ycount, Color.Red);
                }
            }
            pictureBox1.Image = myBitmap;
        }


       
1
Jaish Mathews

Jaish Mathews

NA 7.3k 1.2m 14y

Hi,
Don't hanlde Paint_Event. Create a new method like below.
private void SetMyPicture(string image)
        {
            Bitmap myBitmap = new Bitmap(image);
            for (int Xcount = 0; Xcount < myBitmap.Width; Xcount++)
            {
                for (int Ycount = 0; Ycount < myBitmap.Height; Ycount++)
                {
                    myBitmap.SetPixel(Xcount, Ycount, Color.Red);
                }
            }
            pictureBox1.Image = myBitmap;
        }
Call above method by pasing image path. I called in Page_Load
 SetMyPicture(@"E:\JAISH\img.jpg");
0
Christos Alexiou

Christos Alexiou

NA 61 0 14y
Thank you very much Jaish!!!
0
Christos Alexiou

Christos Alexiou

NA 61 0 14y
Hi Jaish again,

Thanx again about your reply! I know how to load an image in a pictureBox.
It's easy for me to make a second pictureBox and load the processing image but
how to connect-I mean the code-the two separate pictureBoxes or buttons???
The code to load a picture in a pictureBox is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                OpenFileDialog open = new OpenFileDialog();

                open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";

                if (open.ShowDialog() == DialogResult.OK)
                {

                    pictureBox1.Image = new Bitmap(open.FileName);

                }

            }

            catch (Exception)
            {

                throw new ApplicationException("Failed loading image");

            }

        }
    }
}

What's the appropriate code below the "Process" button so in the second pictureBox-pictureBox2-have the processing image???
Thanx in advance!!!

0
Jaish Mathews

Jaish Mathews

NA 7.3k 1.2m 14y

NP,please close/mark this thread.
0
Christos Alexiou

Christos Alexiou

NA 61 0 14y
Dear Jaish,

This is the code I really use:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

       
        private void SetMyPicture(string image)
        {
            Bitmap myBitmap = new Bitmap(image);
            for (int Xcount = 0; Xcount < myBitmap.Width; Xcount++)
            {
                for (int Ycount = 0; Ycount < myBitmap.Height; Ycount++)
                {
                    myBitmap.SetPixel(Xcount, Ycount, Color.Red);
                }
            }
            pictureBox1.Image = myBitmap;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            SetMyPicture("C:\\Users\\Christos\\Pictures\\My Photos_1\\Christos_3");
        }
    }
}

of course the image exists in local machine...

ok...I found the mistake...
In line SetMyPicture("C:\\Users\\Christos\\Pictures\\My Photos_1\\Christos_3"); the right is
SetMyPicture("C:\\Users\\Christos\\Pictures\\My Photos_1\\Christos_3.jpg");
Now the code works fine!
Thank you very much!!!
Also I want to ask you something else...
My problem is to have in Application Form a pictureBox-to load an image in it-and a button which name
is "Load" so pressing the button the program loading the image in my picture Box!!!!
After that I can have the opportunity to process this photo...How do I achieve this???

0
Jaish Mathews

Jaish Mathews

NA 7.3k 1.2m 14y

Hi,
In Line, Bitmap myBitmap = new Bitmap(image), check that image specified is really exists in your local machine?
Most probably the image path u mentioned not phisically available. If this is nto the case reply with the error message u getting.
0
Christos Alexiou

Christos Alexiou

NA 61 0 14y
Sorry but after building a solution and pressing f5,I had an "ArgumentException was unhandled" message...
The code sucks here: Bitmap myBitmap = new Bitmap(image); I don't know why...

0
Christos Alexiou

Christos Alexiou

NA 61 0 14y
First of all,thank you Jaish for your reply.
You wrote me to create a new method.Ok,where's the location of this method??
In form handle???Somewhere else??Sorry but i'm newbie in C# and don't know much...
You wrote me also, to call the method above by passing image path.Ok,from where???
Page_load???Did you mean button_load???
Thank you again in advance!