微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我想知道如何使用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上测试了代码,但我认为它也可以在这里运行.

原文地址:https://www.jb51.cc/csharp/92011.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐