When the mouse is clicked at a point on the picture box during the run time( i.e when the image is loaded) it should change color of the pixels at that location.i have this piece of code showing error.
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
Bitmap bm=new Bitmap(1,1);
bm.SetPixel(0,0,Color.Red);
e.Graphics.DrawImageUnscaled(bm,e.X,e.Y);
}
the error is 'System.Windows.Forms.MouseEventArgs' does not contain a definition for 'Graphics' and no extension method 'Graphics' accepting a first argument of type 'System.Windows.Forms.MouseEventArgs' could be found (are you missing a using directive or an assembly reference?)
even though i included system.drawing it is 开发者_运维技巧showing this error.
Try changing the image in the picturebox instead of trying to redraw it, as the MouseEventArgs does not contain any Graphics property.
Example:
int radius = 3; //Set the number of pixel you wan to use here
//Calculate the numbers based on radius
int x0 = Math.Max(e.X-(radius/2),0),
y0 = Math.Max(e.Y-(radius/2),0),
x1 = Math.Min(e.X+(radius/2),pictureBox1.Width),
y1 = Math.Min(e.Y+(radius/2),pictureBox1.Height);
Bitmap bm = pictureBox1.Image as BitMap; //Get the bitmap (assuming it is stored that way)
for (int ix = x0; ix < x1; ix++)
{
for (int iy = y0; iy < y1; iy++)
{
bm.SetPixel(ix,iy, Color.Read); //Change the pixel color, maybe should be relative to bitmap
}
}
pictureBox1.Refresh(); //Force refresh
EDIT: Alowed multiple pixels to be selected in accordance with a fill radius
精彩评论