我创建了一个显示色谱的自定义控件。 我重写OnRender()方法以使用颜色绘制控件。 由于某种原因,"计算的颜色" (R G B值)具有一定的透明度,即使我将Alpha值(A)设置为最大可能值(255)。 我创建了一个测试窗口,在背景上显示一些文本,我的控件和带有LinearGradient背景的边框和文本可以在我的控件后面看到但不在边框后面,这清楚地表明我的控件有一些透明度设定。 即使我设置A = 255,我的计算颜色有透明度的任何想法? 我很感激任何建议。 谢谢!
这是我的Window.xaml和我的对手的.cs:
MainWindow.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Views="clr-namespace:ColorBarTest.Views"
x:Class="ColorBarTest.MainWindow"
Title="MainWindow" Height="671.622" Width="720.102">
<Grid>
<TextBlock Margin="0,66,0,558" HorizontalAlignment="Left" Width="218"><Run Text="TESTING THE OPACITY O"/><Run Text="F C"/><Run Text="OLOR BAR"/></TextBlock>
<Views:TestRender Margin="0,65,671,0" />
<Border Margin="100,65,671,0"/>
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="577" Margin="105,65,0,0" VerticalAlignment="Top" Width="42">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Red" Offset="1"/>
<GradientStop Color="Magenta"/>
</LinearGradientBrush>
</Border.Background>
</Border>
</Grid>
</Window>
TestRender.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ColorBarTest.Views
{
public class TestRender : Control
{
private int Max = 99;
private int Min = 1;
private Color StartColor = Colors.Red;
private Color EndColor = Colors.Fuchsia;
static TestRender()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TestRender), new FrameworkPropertyMetadata(typeof(TestRender)));
}
protected override void OnRender(DrawingContext drawingContext)
{
for (double pixel = 0; pixel < ActualHeight; pixel++)
{
var value = ConvertYPixelToColorHandleValue(pixel);
var color = CalculateRGBA(value);
var brush = new SolidColorBrush(color);
brush.Opacity = 1.0f;
var pen = new Pen(brush, 1.0f);
drawingContext.DrawLine(pen, new Point(0, pixel), new Point(ActualWidth, pixel));
//Console.WriteLine("Value: {0}", value);
}
var pen2 = new Pen(new SolidColorBrush(Colors.Red), 5.0d);
var pen3 = new Pen(new SolidColorBrush(Colors.Fuchsia), 5.0d);
drawingContext.DrawLine(pen3, new Point(0, 0), new Point(ActualWidth, 0));
drawingContext.DrawLine(pen2, new Point(0, ActualHeight), new Point(ActualWidth, ActualHeight));
}
private float ConvertYPixelToColorHandleValue(double tickDrawY)
{
var pixelPercent = ((tickDrawY) / (float)ActualHeight);
return (Max - (float)(pixelPercent * (Max - Min)));
}
public Color CalculateRGBA(double value)
{
var rate = (float)((value - Min) / (Max - Min));
float posRate = rate - 1;
// Interpolate the color, there will be a HSV interpolation also
return RgbLinearInterpolate(posRate);
}
public Color RgbLinearInterpolate(double rate)
{
var nr = 1.0 - rate;
var r = (byte)((nr * StartColor.R) + (rate * EndColor.R));
var g = (byte)((nr * StartColor.G) + (rate * EndColor.G));
var b = (byte)((nr * StartColor.B) + (rate * EndColor.B));
return new Color { R = r, G = g, B = b, A = 255 };
}
}
}
它的外观如下:
答案 0 :(得分:2)
在OnRender的顶部,添加:
this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);
或者,将您的xaml更改为:
<Views:TestRender Margin="0,65,671,0" RenderOptions.EdgeMode="Aliased" />
或者,您可以尝试绘制重叠的矩形:
//drawingContext.DrawLine(pen, new Point(0, pixel), new Point(ActualWidth, pixel));
drawingContext.DrawRectangle(brush, pen, new Rect(new Point(0, pixel - 0.25), new Point(ActualWidth, pixel + 0.25)));