在C#语法中,怎么实现将图片做成马赛克效果?下面web建站小编给大家详细介绍一下具体实现代码!
代码如下:
复制代码using System.Drawing;
using System.Drawing.Imaging;
using System.Web.Mvc;
namespace MVC2017_Sample.Controllers
{
public class DefaultController : Controller
{
public ActionResult Index()
{
//原图
Image img = Image.FromFile("e:\\pic.jpg");
Bitmap map = new Bitmap(img);
//马赛克处理后的图片
Image img2 = AdjustTobMosaic(map, 20);
img2.Save("c:\\1_bak.jpg", ImageFormat.Jpeg);
return View();
}
/// <summary>
/// 马赛克处理
/// </summary>
/// <param name="bitmap"></param>
/// <param name="effectWidth"> 影响范围 每一个格子数 </param>
/// <returns></returns>
public Bitmap AdjustTobMosaic(System.Drawing.Bitmap bitmap, int effectWidth)
{
// 差异最多的就是以照一定范围取样 玩之后直接去下一个范围
for (int heightOfffset = 0; heightOfffset < bitmap.Height; heightOfffset += effectWidth)
{
for (int widthOffset = 0; widthOffset < bitmap.Width; widthOffset += effectWidth)
{
int avgR = 0, avgG = 0, avgB = 0;
int blurPixelCount = 0;
for (int x = widthOffset; (x < widthOffset + effectWidth && x < bitmap.Width); x++)
{
for (int y = heightOfffset; (y < heightOfffset + effectWidth && y < bitmap.Height); y++)
{
System.Drawing.Color pixel = bitmap.GetPixel(x, y);
avgR += pixel.R;
avgG += pixel.G;
avgB += pixel.B;
blurPixelCount++;
}
}
// 计算范围平均
avgR = avgR / blurPixelCount;
avgG = avgG / blurPixelCount;
avgB = avgB / blurPixelCount;
// 所有范围内都设定此值
for (int x = widthOffset; (x < widthOffset + effectWidth && x < bitmap.Width); x++)
{
for (int y = heightOfffset; (y < heightOfffset + effectWidth && y < bitmap.Height); y++)
{
System.Drawing.Color newColor = System.Drawing.Color.FromArgb(avgR, avgG, avgB);
bitmap.SetPixel(x, y, newColor);
}
}
}
}
return bitmap;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
C#语法中的关于Environment.Exit()方法的运用
上面是“C#语法怎么实现图片马赛克效果?”的全面内容,想了解更多关于 php入门 内容,请继续关注web建站教程。
当前网址:https://ipkd.cn/webs_4128.html
workflows工作流
乒乓卡通3d人物
一只透明老虎骨骼标本ComfyUI工作流
一张由表情符号组成的照片ComfyUI工作流
一位身着传统红色服装的女战士ComfyUI工作流
一个男人正走进科幻的大门ComfyUI工作流
穿着蘑菇帽的小蚂蚁探险家ComfyUI工作流
一辆老式灵车在黑暗中从雾中出现ComfyUI工作流
一张科幻照片,火星车在沙漠里ComfyUI工作流
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!