C# 基于.NET的渐晕图像效果算法
我想知道如何使用C#和.NET在图片上创建一个C# 基于.NET的渐晕图像效果算法,c#,.net,image-manipulation,C#,.net,Image Manipulation,我想知道如何使用C#和.NET在图片上创建一个 有人知道怎么做吗?或者是否有任何资源已经为我完成了算法?如果您的图片在文件中,并且速度足够快,可以使用的命令行工具convert,它有一个选项-vignette。要从C#程序中调用它,您可以通过System.Diagnostics.Process.Start或ImageMagick运行它。我相信这会满足您的要求: public void PaintVignette(Graphics g, Rectangle bounds) { Rectan
有人知道怎么做吗?或者是否有任何资源已经为我完成了算法?如果您的图片在文件中,并且速度足够快,可以使用的命令行工具
convert
,它有一个选项-vignette
。要从C#程序中调用它,您可以通过System.Diagnostics.Process.Start或ImageMagick运行它。我相信这会满足您的要求:
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度角点。根据宽度和高度进行缩放,可以获得膨胀直肠以确保其完全覆盖所需的距离
然后,我调整了渐变的混合,使其中心的颜色更白
然后我将颜色从白色改为纯透明黑色,从黑色改为纯不透明黑色。这有一种效果,即在进入中心的过程中,将远角涂成黑色并减少阴影
最后,我编写了一个在位图上运行的实用方法(我还没有测试这一部分-我在一个面板的图形上测试了代码,但我认为它在这里也会起作用。谢谢,这是解决我问题的一个好办法。不过,我想看看这个效果背后的算法,以便我自己能够实现它。还有人注意到这个小插曲偏移到了中心的右侧吗?我已经进行了三次检查在一张1200像素宽的图像上,渐晕图的右边大约是50-60像素。