SoFunction
Updated on 2025-03-06

C# Implementation code that can control the degree of blurring by mosaicing images

The specific code is as follows:

using ;
using ;
using ;
namespace MVC2017_Sample.Controllers
{
 public class DefaultController : Controller
 {
  public ActionResult Index()
  {
   //Original picture   Image img = ("c:\\");
   Bitmap map = new Bitmap(img);
   //The image after mosaic processing Image img2 = AdjustTobMosaic(map, 20);   ("c:\\1_bak.jpg", );
   return View();
  }
  /// <summary>
  /// Mosaic processing  /// </summary>
  /// <param name="bitmap"></param>
  /// <param name="effectWidth"> Range of impact Number of each grid </param>  /// &lt;returns&gt;&lt;/returns&gt;
  public Bitmap AdjustTobMosaic( bitmap, int effectWidth)
  {
   // The most significant difference is to take samples from a certain range. After playing, go directly to the next range.   for (int heightOfffset = 0; heightOfffset &lt; ; heightOfffset += effectWidth)
   {
    for (int widthOffset = 0; widthOffset &lt; ; widthOffset += effectWidth)
    {
     int avgR = 0, avgG = 0, avgB = 0;
     int blurPixelCount = 0;
     for (int x = widthOffset; (x &lt; widthOffset + effectWidth &amp;&amp; x &lt; ); x++)
     {
      for (int y = heightOfffset; (y &lt; heightOfffset + effectWidth &amp;&amp; y &lt; ); y++)
      {
        pixel = (x, y);
       avgR += ;
       avgG += ;
       avgB += ;
       blurPixelCount++;
      }
     }
     // Calculate range average     avgR = avgR / blurPixelCount;
     avgG = avgG / blurPixelCount;
     avgB = avgB / blurPixelCount;
     // This value is set in all ranges     for (int x = widthOffset; (x &lt; widthOffset + effectWidth &amp;&amp; x &lt; ); x++)
     {
      for (int y = heightOfffset; (y &lt; heightOfffset + effectWidth &amp;&amp; y &lt; ); y++)
      {
        newColor = (avgR, avgG, avgB);
       (x, y, newColor);
      }
     }
    }
   }
   return bitmap;
  }
 }
} 

Summarize

The above is the implementation code that mosaics the picture to control the degree of blurring. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!