gpt4 book ai didi

c# - 为什么 Color.A = 255 on Color 不会使其完全不透明?

转载 作者:行者123 更新时间:2023-11-30 16:59:36 24 4
gpt4 key购买 nike

我创建了一个显示色谱的自定义控件。我正在覆盖 OnRender() 方法以使用颜色绘制控件。由于某种原因,“计算”的颜色(R G B 值)具有一定的透明度,即使我将 Alpha 值 (A) 设置为最大可能值 (255)。我创建了一个测试窗口,在背景上显示一些文本,我的控件和一个带有 LinearGradient 背景的边框,文本可以在我的控件后面看到,但在边框后面看不到,这清楚地表明我的控件设置了一些透明度.知道为什么即使我设置了 A=255,我计算出的颜色仍具有透明度吗?我感谢任何建议。谢谢!

这是我的 Window.xaml 和我的控件的 .cs:

主窗口.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 };
}
}
}

它是这样的:

Color Bar Test screenshot

最佳答案

在 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)));

关于c# - 为什么 Color.A = 255 on Color 不会使其完全不透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23609827/

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