You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#实现矩形图像裁剪为带模糊透明边缘的椭圆 求技术支持

实现带透明模糊边缘的椭圆图像裁剪

你的现有代码确实能剪出椭圆形状,但它的边缘是硬切割的,没有渐变透明的模糊效果——要实现你要的效果,我们需要给椭圆边缘添加渐变透明遮罩,让图像从中心到边缘慢慢过渡到透明。下面是完整的实现方案:

核心思路

  1. 创建一个和原图尺寸一致的遮罩Bitmap,在遮罩上绘制一个边缘渐变透明的椭圆:中心完全不透明,越靠近椭圆边缘越透明。
  2. 将原图和这个遮罩进行混合,最终得到边缘模糊透明的椭圆图像。

完整代码示例

using System.Drawing;
using System.Drawing.Drawing2D;

public Image CreateEllipseWithTransparentEdge(Image srcImage, int edgeBlurWidth = 30)
{
    // 创建最终输出的图像,使用带Alpha通道的像素格式(确保支持透明)
    Bitmap dstImage = new Bitmap(srcImage.Width, srcImage.Height, PixelFormat.Format32bppArgb);
    
    using (Graphics g = Graphics.FromImage(dstImage))
    {
        // 先清空画布为完全透明
        g.Clear(Color.Transparent);
        
        // 定义椭圆区域(留出边缘模糊的空间)
        int ellipseX = edgeBlurWidth;
        int ellipseY = edgeBlurWidth;
        int ellipseWidth = srcImage.Width - edgeBlurWidth * 2;
        int ellipseHeight = srcImage.Height - edgeBlurWidth * 2;
        GraphicsPath ellipsePath = new GraphicsPath();
        ellipsePath.AddEllipse(ellipseX, ellipseY, ellipseWidth, ellipseHeight);
        
        // 创建椭圆的渐变遮罩:中心不透明,边缘完全透明
        using (PathGradientBrush brush = new PathGradientBrush(ellipsePath))
        {
            brush.CenterColor = Color.FromArgb(255, 0, 0, 0); // 中心黑色(不透明)
            brush.SurroundColors = new Color[] { Color.FromArgb(0, 0, 0, 0) }; // 边缘透明
            
            // 设置渐变的范围,控制模糊边缘的宽度
            brush.FocusScales = new PointF(1 - (float)edgeBlurWidth / ellipseWidth, 1 - (float)edgeBlurWidth / ellipseHeight);
            
            // 先绘制渐变遮罩到临时Bitmap
            Bitmap mask = new Bitmap(srcImage.Width, srcImage.Height, PixelFormat.Format32bppArgb);
            using (Graphics maskG = Graphics.FromImage(mask))
            {
                maskG.FillPath(brush, ellipsePath);
            }
            
            // 使用ImageAttributes将原图和遮罩混合
            using (ImageAttributes attributes = new ImageAttributes())
            {
                ColorMatrix matrix = new ColorMatrix();
                matrix.Matrix33 = 1f; // 保持原图透明度
                attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                attributes.SetAlphaMask(mask);
                
                // 绘制混合后的图像
                g.DrawImage(srcImage, new Rectangle(0, 0, srcImage.Width, srcImage.Height),
                            0, 0, srcImage.Width, srcImage.Height, GraphicsUnit.Pixel, attributes);
            }
        }
    }
    
    return dstImage;
}

关键细节说明

  • 像素格式选择:必须使用Format32bppArgb格式,这个格式支持Alpha透明通道,是实现边缘透明的基础。
  • 渐变遮罩:用PathGradientBrush创建从中心到边缘的透明渐变,FocusScales参数控制渐变的过渡宽度——数值越小,模糊边缘越宽。
  • 图像混合:通过ImageAttributes.SetAlphaMask把遮罩应用到原图上,实现边缘的透明过渡效果。

你可以直接调用这个方法,传入你的srcImage,还可以通过调整edgeBlurWidth参数来控制边缘模糊的程度。

内容的提问来源于stack exchange,提问作者MedMahmoud

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:53:15