在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → WPF .NET6 颜色拾取器

WPF .NET6 颜色拾取器

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.10M
  • 下载次数:14
  • 浏览次数:188
  • 发布时间:2024-06-24
  • 实例类别:C#语言基础
  • 发 布 人:tzjtzj18
  • 文件格式:.rar
  • 所需积分:0
 相关标签: .net6 颜色拾取器 .NET NET wpf

实例介绍

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

完毕,全部就这么多。



实例下载地址

WPF .NET6 颜色拾取器

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警