基本信息
源码名称:WPF .NET6 颜色拾取器
源码大小:0.10M
文件格式:.rar
开发语言:C#
更新时间:2024-06-24
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
WPF .NET6 的颜色拾取器
要用到调色板功能,网上的资料基本不能用。这东西很简单,就自己写了。
真的很简单,本来想抄现成的,都不知道网上资料在说些什么。
<Window x:Class="Picker.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Picker"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Canvas x:Name="rootBox">
<Label x:Name="Label_Value" Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="1.805,2.693" Canvas.Left="657" Canvas.Top="57"/>
</Canvas>
</Grid>
</Window>
后台:
using System.Windows;
namespace Picker
{
public partial class MainWindow : Window
{
private readonly Gradient_Pad? _Pad = new(); // 初始化拾色器
public MainWindow()
{
InitializeComponent();
_Pad.Line_Extend = 20; // 标线延长20
//_Pad.Line_Thickness = 1; // 标线宽度设置为1
//_Pad.Line_Color = Colors.Green; // 标线颜色设置为绿色
_Pad.Move_Event = _Pad_Move_Event; // 添加一个鼠标移动事件,用来显示当前颜色值或其他,也可以不设置
rootBox.Children.Add(_Pad.Set(50, 50, 200, 200, 2)); // 在50,50位置,宽度200,高度200,倾斜渲染颜色
}
private void _Pad_Move_Event(object? sender, EventArgs e)
{
Label_Value.Content = _Pad!.GetColor; // 显示当前鼠标取得的颜色值
}
}
}
完毕。
Gradient_Pad.cs
****************************************
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
internal class Gradient_Pad
{
public Color GetColor = Colors.Transparent; // 取得的颜色值
public Point GetPoint; // 鼠标移动的位置
public double Line_Extend = 0; // 标线延伸长度
public Color Line_Color = Colors.Black; // 标线的颜色
public double Line_Thickness = 0.1; // 标线的粗细
public event EventHandler? Move_Event; // 自定义鼠标移动事件
private Rectangle? horizontalFillRectangle;
private RenderTargetBitmap? rtb;
private Canvas? box;
private readonly Line[] line = new Line[2];
/// <summary>
/// 取色器
/// </summary>
/// <param name="x">位置 x</param>
/// <param name="y">位置 y</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <param name="type">渲染方向:0-左右,1-上下,2-倾斜</param>
/// <returns></returns>
public Canvas Set(double x,double y, double width, double height, int type = 0)
{
box = new()
{
Width = width,
Height = height,
};
Point start;
Point end;
if (type == 1)
{
start = new Point(0.5, 0);
end = new Point(0.5, 1);
}
else
if (type == 2)
{
start = new Point(0, 0);
end = new Point(1, 1);
}
else
{
start = new Point(0, 0.5);
end = new Point(1, 0.5);
}
horizontalFillRectangle = new()
{
Width = width,
Height = height,
};
LinearGradientBrush myHorizontalGradient = new()
{
StartPoint = start,
EndPoint = end,
};
myHorizontalGradient.GradientStops.Add(new GradientStop(Colors.Yellow, 0.0));
myHorizontalGradient.GradientStops.Add(new GradientStop(Colors.Red, 0.25));
myHorizontalGradient.GradientStops.Add(new GradientStop(Colors.Blue, 0.75));
myHorizontalGradient.GradientStops.Add(new GradientStop(Colors.LimeGreen, 1.0));
horizontalFillRectangle.Fill = myHorizontalGradient;
box.Children.Add(horizontalFillRectangle);
box.PreviewMouseLeftButtonDown = Box_PreviewMouseLeftButtonDown;
box.PreviewMouseMove = Box_PreviewMouseMove;
box.Loaded = Box_Loaded;
for (int i = 0; i < 2; i )
{
line[i] = new()
{
Stroke = new SolidColorBrush(Line_Color),
StrokeThickness = Line_Thickness,
};
box.Children.Add(line[i]);
}
Canvas.SetLeft(box,x);
Canvas.SetTop(box,y);
return box;
}
private void Box_Loaded(object sender, RoutedEventArgs e)
{
Rect rect = VisualTreeHelper.GetDescendantBounds(horizontalFillRectangle);
DrawingVisual dv = new();
using (DrawingContext ctx = dv.RenderOpen())
{
VisualBrush brush = new(horizontalFillRectangle);
ctx.DrawRectangle(brush, null, new Rect(rect.Size));
}
rtb = new((int)horizontalFillRectangle!.ActualWidth, (int)horizontalFillRectangle.ActualHeight, 96, 96, PixelFormats.Pbgra32);
rtb.Render(dv);
PngBitmapEncoder encoder = new();
encoder.Frames.Add(BitmapFrame.Create(rtb));
}
private void Box_PreviewMouseMove(object sender, MouseEventArgs e)
{
var pL = (FrameworkElement)sender;
GetPoint = Mouse.GetPosition(pL);
if (e.LeftButton == MouseButtonState.Pressed)
{
line[0].X1 = Line_Extend*(-1);
line[0].Y1 = GetPoint.Y;
line[0].X2 = box!.Width Line_Extend;
line[0].Y2 = GetPoint.Y;
line[1].X1 = GetPoint.X;
line[1].Y1 = Line_Extend*(-1);
line[1].X2 = GetPoint.X;
line[1].Y2 = box.Height Line_Extend;
GetColor = GetPixelColor(rtb!, (int)GetPoint.X, (int)GetPoint.Y);
if (Move_Event!=null) Move_Event!.Invoke(sender, e);
}
}
private void Box_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var pL = (FrameworkElement)sender;
GetPoint = Mouse.GetPosition(pL);
line[0].X1 = 0;
line[0].Y1 = GetPoint.Y;
line[0].X2 = box!.Width;
line[0].Y2 = GetPoint.Y;
line[1].X1 = GetPoint.X;
line[1].Y1 = 0;
line[1].X2 = GetPoint.X;
line[1].Y2 = box.Height;
GetColor = GetPixelColor(rtb!, (int)GetPoint.X, (int)GetPoint.Y);
}
public Color GetPixelColor(BitmapSource bitmap, int x, int y)
{
if (x > box!.Width-1 || y > box.Height-1 || x < 0 || y < 0) return Colors.Transparent;
if (bitmap.Format != PixelFormats.Bgra32 && bitmap.Format != PixelFormats.Pbgra32)
{
throw new InvalidOperationException("Pixel format must be Bgra32 or Pbgra32.");
}
int width = bitmap.PixelWidth;
int height = bitmap.PixelHeight;
int stride = width * ((bitmap.Format.BitsPerPixel 7) / 8);
byte[] pixels = new byte[stride * height];
try
{
bitmap.CopyPixels(pixels, stride, 0);
}
finally
{
}
int index = (y * stride) (x * ((bitmap.Format.BitsPerPixel 7) / 8));
return Color.FromArgb(
pixels[index 3], // Alpha
pixels[index 2], // Red
pixels[index 1], // Green
pixels[index 0] // Blue
);
}
}
完毕,全部就这么多。
WPF .NET6 的颜色拾取器
要用到调色板功能,网上的资料基本不能用。这东西很简单,就自己写了。
真的很简单,本来想抄现成的,都不知道网上资料在说些什么。
临时写的,可以再简化,如有问题自行修正。
前台放置Canvas容器和Label用来显示数值
<Window x:Class="Picker.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Picker"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Canvas x:Name="rootBox">
<Label x:Name="Label_Value" Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="1.805,2.693" Canvas.Left="657" Canvas.Top="57"/>
</Canvas>
</Grid>
</Window>
后台:
using System.Windows;
namespace Picker
{
public partial class MainWindow : Window
{
private readonly Gradient_Pad? _Pad = new(); // 初始化拾色器
public MainWindow()
{
InitializeComponent();
_Pad.Line_Extend = 20; // 标线延长20
//_Pad.Line_Thickness = 1; // 标线宽度设置为1
//_Pad.Line_Color = Colors.Green; // 标线颜色设置为绿色
_Pad.Move_Event = _Pad_Move_Event; // 添加一个鼠标移动事件,用来显示当前颜色值或其他,也可以不设置
rootBox.Children.Add(_Pad.Set(50, 50, 200, 200, 2)); // 在50,50位置,宽度200,高度200,倾斜渲染颜色
}
private void _Pad_Move_Event(object? sender, EventArgs e)
{
Label_Value.Content = _Pad!.GetColor; // 显示当前鼠标取得的颜色值
}
}
}
完毕。
Gradient_Pad.cs
****************************************
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
internal class Gradient_Pad
{
public Color GetColor = Colors.Transparent; // 取得的颜色值
public Point GetPoint; // 鼠标移动的位置
public double Line_Extend = 0; // 标线延伸长度
public Color Line_Color = Colors.Black; // 标线的颜色
public double Line_Thickness = 0.1; // 标线的粗细
public event EventHandler? Move_Event; // 自定义鼠标移动事件
private Rectangle? horizontalFillRectangle;
private RenderTargetBitmap? rtb;
private Canvas? box;
private readonly Line[] line = new Line[2];
/// <summary>
/// 取色器
/// </summary>
/// <param name="x">位置 x</param>
/// <param name="y">位置 y</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <param name="type">渲染方向:0-左右,1-上下,2-倾斜</param>
/// <returns></returns>
public Canvas Set(double x,double y, double width, double height, int type = 0)
{
box = new()
{
Width = width,
Height = height,
};
Point start;
Point end;
if (type == 1)
{
start = new Point(0.5, 0);
end = new Point(0.5, 1);
}
else
if (type == 2)
{
start = new Point(0, 0);
end = new Point(1, 1);
}
else
{
start = new Point(0, 0.5);
end = new Point(1, 0.5);
}
horizontalFillRectangle = new()
{
Width = width,
Height = height,
};
LinearGradientBrush myHorizontalGradient = new()
{
StartPoint = start,
EndPoint = end,
};
myHorizontalGradient.GradientStops.Add(new GradientStop(Colors.Yellow, 0.0));
myHorizontalGradient.GradientStops.Add(new GradientStop(Colors.Red, 0.25));
myHorizontalGradient.GradientStops.Add(new GradientStop(Colors.Blue, 0.75));
myHorizontalGradient.GradientStops.Add(new GradientStop(Colors.LimeGreen, 1.0));
horizontalFillRectangle.Fill = myHorizontalGradient;
box.Children.Add(horizontalFillRectangle);
box.PreviewMouseLeftButtonDown = Box_PreviewMouseLeftButtonDown;
box.PreviewMouseMove = Box_PreviewMouseMove;
box.Loaded = Box_Loaded;
for (int i = 0; i < 2; i )
{
line[i] = new()
{
Stroke = new SolidColorBrush(Line_Color),
StrokeThickness = Line_Thickness,
};
box.Children.Add(line[i]);
}
Canvas.SetLeft(box,x);
Canvas.SetTop(box,y);
return box;
}
private void Box_Loaded(object sender, RoutedEventArgs e)
{
Rect rect = VisualTreeHelper.GetDescendantBounds(horizontalFillRectangle);
DrawingVisual dv = new();
using (DrawingContext ctx = dv.RenderOpen())
{
VisualBrush brush = new(horizontalFillRectangle);
ctx.DrawRectangle(brush, null, new Rect(rect.Size));
}
rtb = new((int)horizontalFillRectangle!.ActualWidth, (int)horizontalFillRectangle.ActualHeight, 96, 96, PixelFormats.Pbgra32);
rtb.Render(dv);
PngBitmapEncoder encoder = new();
encoder.Frames.Add(BitmapFrame.Create(rtb));
}
private void Box_PreviewMouseMove(object sender, MouseEventArgs e)
{
var pL = (FrameworkElement)sender;
GetPoint = Mouse.GetPosition(pL);
if (e.LeftButton == MouseButtonState.Pressed)
{
line[0].X1 = Line_Extend*(-1);
line[0].Y1 = GetPoint.Y;
line[0].X2 = box!.Width Line_Extend;
line[0].Y2 = GetPoint.Y;
line[1].X1 = GetPoint.X;
line[1].Y1 = Line_Extend*(-1);
line[1].X2 = GetPoint.X;
line[1].Y2 = box.Height Line_Extend;
GetColor = GetPixelColor(rtb!, (int)GetPoint.X, (int)GetPoint.Y);
if (Move_Event!=null) Move_Event!.Invoke(sender, e);
}
}
private void Box_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var pL = (FrameworkElement)sender;
GetPoint = Mouse.GetPosition(pL);
line[0].X1 = 0;
line[0].Y1 = GetPoint.Y;
line[0].X2 = box!.Width;
line[0].Y2 = GetPoint.Y;
line[1].X1 = GetPoint.X;
line[1].Y1 = 0;
line[1].X2 = GetPoint.X;
line[1].Y2 = box.Height;
GetColor = GetPixelColor(rtb!, (int)GetPoint.X, (int)GetPoint.Y);
}
public Color GetPixelColor(BitmapSource bitmap, int x, int y)
{
if (x > box!.Width-1 || y > box.Height-1 || x < 0 || y < 0) return Colors.Transparent;
if (bitmap.Format != PixelFormats.Bgra32 && bitmap.Format != PixelFormats.Pbgra32)
{
throw new InvalidOperationException("Pixel format must be Bgra32 or Pbgra32.");
}
int width = bitmap.PixelWidth;
int height = bitmap.PixelHeight;
int stride = width * ((bitmap.Format.BitsPerPixel 7) / 8);
byte[] pixels = new byte[stride * height];
try
{
bitmap.CopyPixels(pixels, stride, 0);
}
finally
{
}
int index = (y * stride) (x * ((bitmap.Format.BitsPerPixel 7) / 8));
return Color.FromArgb(
pixels[index 3], // Alpha
pixels[index 2], // Red
pixels[index 1], // Green
pixels[index 0] // Blue
);
}
}
完毕,全部就这么多。