gpt4 book ai didi

c# - .NET 渐晕图像效果算法

转载 作者:太空狗 更新时间:2023-10-29 21:07:20 25 4
gpt4 key购买 nike

我想知道如何创建 vignetting effect使用 C# 和 .NET 在图片上。

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

最佳答案

我相信这会做你想做的事:

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, 0, 0);
brush.SurroundColors = new Color[] { Color.FromArgb(255, 0, 0, 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, 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, 0, final.Width, final.Height));
return final;
}
}

这是怎么回事?首先,我编写的代码将使用从白色变为黑色的椭圆形渐变画笔填充矩形。然后我修改了代码,使填充区域也包括角。为此,我将矩形尺寸增加了矩形尺寸与 sqrt(2)/2 * 矩形尺寸之间的差值。

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

然后我将渐变的 Blend 调整为中心更白。

然后我将颜色从白色更改为纯透明黑色,从黑色更改为纯不透明黑色。这具有在到达中心的途中将远角涂成黑色和阴影较少的效果。

最后,我编写了一个在 Bitmap 上运行的实用方法(我没有测试这部分 - 我在 Panel 的图形上测试了代码,但我认为它也可以在这里工作。

关于c# - .NET 渐晕图像效果算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2065199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com