C# 实现让图片模糊
时间: 2023-07-27 17:21:38 浏览: 49
在C#中实现图片模糊,可以使用 GDI+ 的 `System.Drawing` 命名空间中的 `Bitmap` 和 `Graphics` 类。具体步骤如下:
1. 读取图片文件,创建 `Bitmap` 对象。
```csharp
Bitmap image = new Bitmap("image.jpg");
```
2. 创建 `Graphics` 对象,以便在其上绘制图像。
```csharp
Graphics graphics = Graphics.FromImage(image);
```
3. 使用 `graphics` 对象的 `Pixelate` 方法对图像进行像素化处理。像素化处理会使图像模糊。
```csharp
graphics.Pixelate(10);
```
4. 保存处理后的图像。
```csharp
image.Save("blurred_image.jpg");
```
完整代码示例:
```csharp
using System.Drawing;
class Program
{
static void Main(string[] args)
{
Bitmap image = new Bitmap("image.jpg");
Graphics graphics = Graphics.FromImage(image);
graphics.Pixelate(10);
image.Save("blurred_image.jpg");
}
}
```
注意,像素化处理的程度可以通过 `Pixelate` 方法的参数来控制。参数越大,像素化程度越高,图像也就越模糊。