加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用.NET的渐晕图像效果算法

发布时间:2020-12-15 04:19:46 所属栏目:百科 来源:网络整理
导读:我想知道如何使用C#和.NET在图片上创建 vignetting effect. 有没有人有任何想法如何做到这一点?或者是否有任何资源可以为我完成算法? 解决方法 我相信这会做你想要的: public void PaintVignette(Graphics g,Rectangle bounds){ Rectangle ellipsebounds
我想知道如何使用C#和.NET在图片上创建 vignetting effect.

有没有人有任何想法如何做到这一点?或者是否有任何资源可以为我完成算法?

解决方法

我相信这会做你想要的:
public void PaintVignette(Graphics g,Rectangle bounds)
{
    Rectangle ellipsebounds = bounds;
    ellipsebounds.Offset(-ellipsebounds.X,-ellipsebounds.Y);
    int x = ellipsebounds.Width - (int)Math.Round(.70712 * ellipsebounds.Width);
    int y = ellipsebounds.Height - (int)Math.Round(.70712 * ellipsebounds.Height);
    ellipsebounds.Inflate(x,y);

    using (GraphicsPath path = new GraphicsPath())
    {
        path.AddEllipse(ellipsebounds);
        using (PathGradientBrush brush = new PathGradientBrush(path))
        {
            brush.WrapMode = WrapMode.Tile;
            brush.CenterColor = Color.FromArgb(0,0);
            brush.SurroundColors = new Color[] { Color.FromArgb(255,0) };
            Blend blend = new Blend();
            blend.Positions = new float[] { 0.0f,0.2f,0.4f,0.6f,0.8f,1.0F };
            blend.Factors = new float[] { 0.0f,0.5f,1f,1.0f,1.0f };
            brush.Blend = blend;
            Region oldClip = g.Clip;
            g.Clip = new Region(bounds);
            g.FillRectangle(brush,ellipsebounds);
            g.Clip = oldClip;
        }
    }
}

public Bitmap Vignette(Bitmap b)
{
    Bitmap final = new Bitmap(b);
    using (Graphics g = Graphics.FromImage(final)) {
        PaintVignette(g,new Rectangle(0,final.Width,final.Height));
        return final;
    }
}

这里发生了什么?首先,我编写了一个代码,用一个椭圆形渐变画笔填充一个矩形,从白色到黑色.然后我修改了代码,以便填充区域也包括角落.我通过增加矩形大小和矩形尺寸之间的差异和sqrt(2)/ 2 *矩形尺寸来实现这一点.

为什么sqrt(2)/ 2?因为点(sqrt(2)/ 2,sqrt(2)/ 2)是单位圆上的45度角点.按宽度和高度缩放给出了使矩形膨胀所需的距离,以确保它完全被覆盖.

然后我调整渐变的混合在中心更白.

然后我将颜色从白色变为纯透明黑色,从黑色变为纯不透明黑色.这样就可以在通往中心的途中将远角涂成黑色和阴影.

最后,我写了一个在Bitmap上运行的实用程序方法(我没有测试过这部分 – 我在Panel上测试了代码,但我认为它也可以在这里运行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读