如何使刷子在中间没有线条的情况下变得光滑

在此处输入图像描述

大家好,就像你在之前的画笔中看到的那样,中间有一些线条,
如何让它顺利进行并不是那么顺利? (如何删除那些行)我用混合创建它

        

条带是梯度算法的假象。 它必须打破区域,每个区域都填充略有不同的颜色。 边缘实际上是一种视觉错觉,其效果使它们比您想象的更加明显。 要减少这种影响,您需要减小每个波段的宽度。

解决方案是:

  1. 填充较小的区域 – 每个波段较窄。
  2. 增加乐队数量。 使两个极端之间的对比更大。
  3. 增加显示器的颜色分辨率。 如果您有更多颜色可供选择,那么两种颜色之间的可用范围将更大。

我确实意识到这些解决方案要么a)不可能,要么b)不实用。 这是一个你不得不忍受的问题。

一个实际的解决方案可能是用Photoshop或其他图像处理包中创建的图像替换画笔。 这可能会给你一个带有较少条带的图像 – 但是你被限制在图像的大小 – 如果没有像素化你就无法生长它。

前段时间我为WPF项目编写了平滑的线性渐变。 它删除了条带,但有两个警告:

  • 它不能用于数据绑定颜色或{DynamicResource}
  • 它目前仅支持垂直渐变,因为这就是我所需要的

它使用Ordered Dithering实现为动态创建的ImageBrush 。 另外,它也是一个MarkupExtension ,因为不能简单地inheritance任何Brush类(这些都是密封的)。

 ///  /// Brush that lets you draw vertical linear gradient without banding. ///  [MarkupExtensionReturnType(typeof(Brush))] public class SmoothLinearGradientBrush : MarkupExtension { private static PropertyInfo dpiX_; private static PropertyInfo dpiY_; private static byte[,] bayerMatrix_ = { { 1, 9, 3, 11 }, { 13, 5, 15, 7 }, { 1, 9, 3, 11 }, { 16, 8, 14, 6 } }; static SmoothLinearGradientBrush() { dpiX_ = typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static); dpiY_ = typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static); } ///  /// Gradient color at the top ///  public Color From { get; set; } ///  /// Gradient color at the bottom ///  public Color To { get; set; } public override object ProvideValue(IServiceProvider serviceProvider) { //If user changes dpi/virtual screen height during applicaiton lifetime, //wpf will scale the image up for us. int width = 20; int height = (int)SystemParameters.VirtualScreenHeight; int dpix = (int)dpiX_.GetValue(null); int dpiy = (int)dpiY_.GetValue(null); int stride = 4 * ((width * PixelFormats.Bgr24.BitsPerPixel + 31) / 32); //dithering parameters double bayerMatrixCoefficient = 1.0 / (bayerMatrix_.Length + 1); int bayerMatrixSize = bayerMatrix_.GetLength(0); //Create pixel data of image byte[] buffer = new byte[height * stride]; for (int line = 0; line < height; line++) { double scale = (double)line / height; for (int x = 0; x < width * 3; x += 3) { //scaling of color double blue = ((To.B * scale) + (From.B * (1.0 - scale))); double green = ((To.G * scale) + (From.G * (1.0 - scale))); double red = ((To.R * scale) + (From.R * (1.0 - scale))); //ordered dithering of color //source: http://en.wikipedia.org/wiki/Ordered_dithering buffer[x + line * stride] = (byte)(blue + bayerMatrixCoefficient * bayerMatrix_[x % bayerMatrixSize, line % bayerMatrixSize]); buffer[x + line * stride + 1] = (byte)(green + bayerMatrixCoefficient * bayerMatrix_[x % bayerMatrixSize, line % bayerMatrixSize]); buffer[x + line * stride + 2] = (byte)(red + bayerMatrixCoefficient * bayerMatrix_[x % bayerMatrixSize, line % bayerMatrixSize]); } } var image = BitmapSource.Create(width, height, dpix, dpiy, PixelFormats.Bgr24, null, buffer, stride); image.Freeze(); var brush = new ImageBrush(image); brush.Freeze(); return brush; } } 

资源字典中的用法:

  

然后在控制风格:

  

我的另一个答案的便宜和肮脏的选择是这样的:

将渐变添加到容器中,给它一个小的负边距,使其溢出一点,向渐变添加一个BlurEffect,然后在父容器上打开ClipToBounds。 这样,梯度就会以性能为代价而变得更好。

但是,根据您的使用情况,这可能不可行。

例:

               

负梯度应等于模糊半径,以使其在边缘上不变透明。