C# edit image with pen?

Chris Spect 1 Reputation point
2021-04-17T02:39:57.163+00:00

Hello friends. My question is very simple. I can write with a pencil on a picture but I don't want to write. I just want to blurthe places where I passed over the picture like pencil. I have blurring and pen writing codes. But I can't integrate these two together? How can I do it?

The example I drew the picture with pencil. (like paint)
88751-img1.jpg

I want to show you an example. It's like changing the photo in the photoshop app. (like blur tool)
88716-img2.jpg

ex pencil codes;

    public Form1()  
        {  
            InitializeComponent();  
            g = pictureBox1.CreateGraphics();  
        }  
        int? initX = null;  
        int? initY = null;  
        Graphics g;  

private void picturebox1_MouseMove(object sender, MouseEventArgs e)  
        {  

 Pen p = new Pen(Color.Black,float.Parse(changesize.Text));  

                g.DrawLine(p, new Point(initX ?? e.X, initY ?? e.Y), new Point(e.X, e.Y));  
                initX = e.X;  
                initY = e.Y;  
         }  

or

 SolidBrush sb = new SolidBrush(Color.Black);                 
                g.FillEllipse(sb, e.X, e.Y, int.Parse(comboBox1.Text), int.Parse(comboBox1.Text));  

****ex blur codes; (meanfilter metod)****

public void meanfilter ()  
 {  
 Color WColor;  
 Bitmap image1;  
 image1 = new Bitmap(pictureBox1.Image);  
 int imagewidth = image1.Width;  
 int imageheight = image1.Height;  
 int templatesize = 5;  

 int x, y, i, j, totalR, totalG, totalB, averR, averG, averB;  
 for (x = (templatesize - 1) / 2; x < imagewidth - (templatesize - 1) / 2; x++)  
 {  
 for (y = (templatesize - 1) / 2; y < imageheight - (templatesize - 1) / 2; y++)  
 {  
 totalR = 0;  
 totalG = 0;  
 totalB = 0;  
 for (i = -((templatesize - 1) / 2) ; i <= (templatesize - 1) / 2 ; i++)  
 {  
 for (j = -((templatesize - 1) / 2); j <= (templatesize - 1) / 2; j++)  
 {  
 WColor = image1.GetPixel(x + i, y + j);  
 totalR = totalR + WColor.R;  
 totalG = totalG + WColor.G;  
 totalB = totalB + WColor.B;  
 }  
 }  
 averR = totalR / (templatesize * templatesize);  
 averG = totalG / (templatesize * templatesize);  
 averB = totalB / (templatesize * templatesize);  

image1.SetPixel(x, y, Color.FromArgb(averR, averG, averB));  
 }  
 }  
 pictureBox1.Image = image1;  
}  

I can not use the like pen to blurç I cannot find it, I will go crazy :D Thanks for your help in advance.

Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-04-19T05:55:58.093+00:00

    Try the following code. In the MouseMove event, I constructed a Rectangle with a height and width of 10 based on the current mouse position, and then blurred the Rectangle area.

           private System.Drawing.Image Blur(System.Drawing.Image image, Rectangle rectangle, Int32 blurSize)  
            {  
                Bitmap blurred = new Bitmap(image);   //image.Width, image.Height);  
                using (Graphics graphics = Graphics.FromImage(blurred))  
                {  
                    // look at every pixel in the blur rectangle  
                    for (Int32 xx = rectangle.Left; xx < rectangle.Right; xx += blurSize)  
                    {  
                        for (Int32 yy = rectangle.Top; yy < rectangle.Bottom; yy += blurSize)  
                        {  
                            Int32 avgR = 0, avgG = 0, avgB = 0;  
                            Int32 blurPixelCount = 0;  
                            Rectangle currentRect = new Rectangle(xx, yy, blurSize, blurSize);  
      
                            // average the color of the red, green and blue for each pixel in the  
                            // blur size while making sure you don't go outside the image bounds  
                            for (Int32 x = currentRect.Left; (x < currentRect.Right && x < image.Width); x++)  
                            {  
                                for (Int32 y = currentRect.Top; (y < currentRect.Bottom && y < image.Height); y++)  
                                {  
                                    Color pixel = blurred.GetPixel(x, y);  
      
                                    avgR += pixel.R;  
                                    avgG += pixel.G;  
                                    avgB += pixel.B;  
                                    blurPixelCount++;  
                                }  
                            }  
                            avgR = avgR / blurPixelCount;  
                            avgG = avgG / blurPixelCount;  
                            avgB = avgB / blurPixelCount;  
      
                            // now that we know the average for the blur size, set each pixel to that color  
                            graphics.FillRectangle(new SolidBrush(Color.FromArgb(avgR, avgG, avgB)), currentRect);  
                        }  
                    }  
                    graphics.Flush();  
                }  
                return blurred;  
            }  
            private void Form1_Load(object sender, EventArgs e)  
            {  
                pictureBox1.MouseMove += PictureBox1_MouseMove;  
            }  
             
            private void PictureBox1_MouseMove(object sender, MouseEventArgs e)  
            {  
                if (e.Button != MouseButtons.Left)  
                    return;  
                Rectangle Rect = new Rectangle();  
                Point tempEndPoint = e.Location;  
                Rect.Location = tempEndPoint;  
                Rect.Size = new Size(10,10);  
                pictureBox1.Image = Blur(pictureBox1.Image, Rect, 2);  
                pictureBox1.Refresh();  
            }  
    

    Before blurring:

    88908-3.png

    After blurring:

    88909-4.png

    The code comes from this post, I made some modifications to it to meet your requirements: Blur the selected part of the image


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.