当前位置: 首页 > news >正文

【WPF】自定义颜色拾取器

1.界面布局

 <GroupBoxPadding="0"BorderThickness="0.1"Foreground="White"Header="Color Pick"><DockPanel><GroupBoxMargin="5,0,5,2"BorderThickness="0.1"DockPanel.Dock="Right"Foreground="White"Header="Color"><WrapPanelWidth="200"DockPanel.Dock="Right"Orientation="Vertical"><WrapPanel Margin="5,5,0,0"><LabelVerticalContentAlignment="Center"Content="R:"Foreground="White" /><LabelName="Label_R"Margin="5,0,0,0"VerticalContentAlignment="Center"Content="0"Foreground="White" /></WrapPanel><WrapPanel Margin="5,5,0,0"><LabelVerticalContentAlignment="Center"Content="G:"Foreground="White" /><LabelName="Label_G"Margin="5,0,0,0"VerticalContentAlignment="Center"Content="0"Foreground="White" /></WrapPanel><WrapPanel Margin="5,5,0,0"><LabelVerticalContentAlignment="Center"Content="B:"Foreground="White" /><LabelName="Label_B"Margin="5,0,0,0"VerticalContentAlignment="Center"Content="0"Foreground="White" /></WrapPanel><WrapPanel Background="Black"><Imagex:Name="Image_Clolor"Width="200"Height="150"Margin="0" /></WrapPanel></WrapPanel></GroupBox><GroupBoxName="WrapPanel_Image"Padding="0,0,0,0"HorizontalContentAlignment="Stretch"VerticalContentAlignment="Stretch"Background="Black"BorderThickness="0"ClipToBounds="True"Cursor="Cross"DockPanel.Dock="Left"><Imagex:Name="Image_Img"Width="{Binding ElementName=WrapPanel_Image, Path=ActualWidth}"Height="{Binding ElementName=WrapPanel_Image, Path=ActualHeight}"Margin="-5,0,0,0"HorizontalAlignment="Left"VerticalAlignment="Top"ClipToBounds="True"MouseWheel="Image_Img_MouseWheel"PreviewMouseLeftButtonDown="Image_Img_PreviewMouseLeftButtonDown"PreviewMouseLeftButtonUp="Image_Img_PreviewMouseLeftButtonUp"PreviewMouseMove="Image_Img_PreviewMouseMove"PreviewMouseRightButtonDown="Image_Img_PreviewMouseRightButtonDown"RenderOptions.BitmapScalingMode="Fant"Stretch="Uniform"><Image.RenderTransform><TransformGroup><ScaleTransform x:Name="Image_Img_ScaleTransform" CenterX="0" CenterY="0" ScaleX="1" ScaleY="1" /><TranslateTransform x:Name="Image_Img_TranslateTransform" X="0" Y="0" /></TransformGroup></Image.RenderTransform></Image></GroupBox></DockPanel></GroupBox>

2.后台实现

 /// <summary>/// ColorPickerUserControl.xaml 的交互逻辑/// </summary>public partial class ColorPickerUserControl : UserControl{//图像原图private Bitmap originalBitmap;//缩放比率private double ScaleRatio = 0.2;//鼠标按下坐标private System.Windows.Point currentClickPoint = new System.Windows.Point(0, 0);//鼠标按下标识bool isImageLeftButtonDown = false;/// <summary>/// 当前选中颜色/// </summary>public System.Drawing.Color CurrentColor;public ColorPickerUserControl(){InitializeComponent();}/// <summary>/// 资源释放/// </summary>public void Dispose(){try{Image_Img.Source?.Freeze();Image_Img.Source = null;originalBitmap?.Dispose();originalBitmap = null;Image_Clolor.Source?.Freeze();Image_Clolor.Source = null;}catch (Exception ex){throw new Exception(ex.Message);}}/// <summary>/// 加载图像/// </summary>/// <param name="fileName">图像文件路径</param>public void InitImage(string fileName){try{Image_Img.Stretch = Stretch.Uniform;if (File.Exists(fileName)){Task<BitmapImage> task = Task.Run(() =>{//后台读取图像,防止图像太大卡住主线程using (var stream = File.OpenRead(fileName)){var bmp = new BitmapImage();bmp.BeginInit();bmp.StreamSource = stream;bmp.CacheOption = BitmapCacheOption.OnLoad;bmp.EndInit();bmp.Freeze(); // 冻结对象以便跨线程访问originalBitmap = new Bitmap(stream);return bmp;}});task.Wait();Image_Img.Source = task.Result;}}catch (Exception ex){throw new Exception(ex.Message);}}/// <summary>/// 加载图像/// </summary>/// <param name="bitmap">图像</param>public void InitImage(Bitmap bitmap){try{originalBitmap?.Dispose();originalBitmap = null;originalBitmap = new Bitmap(bitmap);// 将Bitmap转换为WPF的BitmapImageBitmapImage bitmapImage;using (MemoryStream memory = new MemoryStream()){bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);memory.Position = 0;bitmapImage = new BitmapImage();bitmapImage.BeginInit();bitmapImage.StreamSource = memory;bitmapImage.CacheOption = BitmapCacheOption.OnLoad;bitmapImage.EndInit();}Image_Img.Source = bitmapImage;bitmapImage?.Freeze();bitmapImage = null;}catch (Exception ex){throw new Exception(ex.Message);}}/// <summary>/// 鼠标左键按下/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Image_Img_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e){try{if (e.ChangedButton != MouseButton.Left) return;var pos = e.GetPosition((System.Windows.Controls.Image)e.Source);currentClickPoint = pos;isImageLeftButtonDown = true;var img = ((System.Windows.Controls.Image)e.Source).Source as BitmapSource;ColorPicker(img, pos);}catch (Exception ex){throw new Exception(ex.Message);}}/// <summary>/// 图像点转真实点/// </summary>/// <param name="thumbnailX"></param>/// <returns></returns>private double ToCamRealX(double thumbnailX){return (thumbnailX / Image_Img.ActualWidth) * (double)(originalBitmap?.Width ?? 0);}/// <summary>/// 图像点转真实点/// </summary>/// <param name="thumbnailY"></param>/// <returns></returns>private double ToCamRealY(double thumbnailY){return (thumbnailY / Image_Img.ActualHeight) * (double)(originalBitmap?.Height ?? 0);}/// <summary>/// 颜色拾取/// </summary>/// <param name="img"></param>/// <param name="pos"></param>/// <exception cref="Exception"></exception>protected void ColorPicker(BitmapSource img, System.Windows.Point pos){try{int stride = img.PixelWidth * 4;int size = img.PixelHeight * stride;byte[] pixels = new byte[(int)size];img.CopyPixels(pixels, stride, 0);// Get pixel//屏幕坐标转图像坐标var x = (int)ToCamRealX(pos.X);var y = (int)ToCamRealY(pos.Y);int index = y * stride + 4 * x;byte red = pixels[index];byte green = pixels[index + 1];byte blue = pixels[index + 2];byte alpha = pixels[index + 3];System.Drawing.Color color = System.Drawing.Color.FromArgb(alpha, blue, green, red);Label_R.Content = color.R.ToString();Label_G.Content = color.G.ToString();Label_B.Content = color.B.ToString();//string ColorText = "#" + color.A.ToString("X2") + color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2");ColorShow(color);}catch (Exception ex){throw new Exception(ex.Message);}}/// <summary>/// 颜色显示/// </summary>/// <param name="color"></param>/// <exception cref="Exception"></exception>private void ColorShow(System.Drawing.Color color){try{CurrentColor = color;int w = (int)Image_Clolor.Width;int h = (int)Image_Clolor.Height;Bitmap bitmap = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb);for (int x = 0; x < w; x++){for (int y = 0; y < h; y++){bitmap.SetPixel(x, y, color);}}BitmapImage bitmapImage;using (MemoryStream memory = new MemoryStream()){bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);memory.Position = 0;bitmapImage = new BitmapImage();bitmapImage.BeginInit();bitmapImage.StreamSource = memory;bitmapImage.CacheOption = BitmapCacheOption.OnLoad;bitmapImage.EndInit();}Image_Clolor.Source = bitmapImage;bitmapImage?.Freeze();bitmapImage = null;bitmap.Dispose();}catch (Exception ex){throw new Exception(ex.Message);}}/// <summary>/// 鼠标滚动缩放/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Image_Img_MouseWheel(object sender, MouseWheelEventArgs e){try{System.Windows.Point center = e.GetPosition(Image_Img);double scale = 0;if (e.Delta > 0){scale = Image_Img_ScaleTransform.ScaleX + ScaleRatio;if (scale > 30){return;}}else{scale = Image_Img_ScaleTransform.ScaleX - ScaleRatio;if (scale < 0.5){return;}}//图像Image_Img_ScaleTransform.ScaleX = scale;Image_Img_ScaleTransform.ScaleY = scale;}catch{}}/// <summary>/// 鼠标移动/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Image_Img_PreviewMouseMove(object sender, MouseEventArgs e){try{if (isImageLeftButtonDown == true){if (e.LeftButton == MouseButtonState.Pressed){//移动System.Windows.Point newPoint = e.GetPosition(Image_Img);double deltaX = newPoint.X - currentClickPoint.X;double deltaY = newPoint.Y - currentClickPoint.Y;//图像Image_Img_TranslateTransform.X += deltaX;Image_Img_TranslateTransform.Y += deltaY;}}}catch{}}/// <summary>/// 鼠标放开/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Image_Img_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e){isImageLeftButtonDown = false;}/// <summary>/// 鼠标右键/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Image_Img_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e){Recombination();}/// <summary>/// 重置图像大小/// </summary>private void Recombination(){try{//图像Image_Img_ScaleTransform.ScaleX = 1;Image_Img_ScaleTransform.ScaleY = 1;Image_Img_TranslateTransform.X = 0;Image_Img_TranslateTransform.Y = 0;}catch{}}}

3.效果
在这里插入图片描述

http://www.dtcms.com/a/515240.html

相关文章:

  • MahApps.Metro WPF 开发使用过程中遇到的问题 - 未能加载文件或程序集“Microsoft.Xaml.Behaviors,
  • 【普中Hi3861开发攻略--基于鸿蒙OS】-- 第 26 章 WIFI实验-AP 建立网络
  • ARM架构深度解析:ARMv7、ARMv8、ARMv9的技术演进、芯片实现与未来展望
  • 线下剧本杀预约小程序核心功能玩法解析:轻量化载体重构娱乐消费生态
  • 【矩阵分析与应用】【第8章 特征分析】【8.3 凯莱-哈密顿定理求解矩阵高次幂详解】
  • 合肥制作企业网站免费收录网站推广
  • 阿里云安装docker-compose
  • Centos 7 :VMware Tools 启动脚本未能在虚拟机中成功运行
  • 基于vue的停车场管理系统
  • 短剧小程序系统开发:开启影视娱乐新纪元
  • 系统架构设计师备考第49天——数字孪生体云计算大数据技术
  • 阿里云渠道商:阿里云哪些功能很必要?
  • 鱼馆网站的前期策划网站审核备案 几天
  • 建设银行申请信用卡网站股权融资
  • 即刻创作:用 Trickle + GLM-4.6 API 构建互动小说创作工具
  • 标定系数为什么会存储在相机模组里面,在标定的时候,算法是在割草机的X3板上运行的啊?
  • windows系统安装wls/Ubuntu子系统教程
  • 【Linux】gcc/g++编辑器 初识动静态库 程序翻译过程
  • AI服务器工作之系统下查看硬件(ubuntu为例)
  • 基于python智慧医疗问答系统 知识图谱 Flask框架 数据可视化 neo4j图数据库 计算机专业 优秀项目(源码)✅
  • P3957 [NOIP 2017 普及组] 跳房子
  • 日语学习-日语知识点小记-构建基础-JLPT-N3阶段-二阶段(6):文法運用
  • 【数据结构入坑指南】--《层序分明:堆的实现、排序与TOP-K问题一站式攻克(源码实战)》
  • 做网站的公司怎么推销湘潭做网站价格 q磐石网络
  • 用Python Streamlit sqlite3 写一个简单博客
  • 微型计算机接口与原理笔记
  • 大学生免费ppt网站广州分销商城开发
  • 良策金宝AI定制方案:构建企业专属的“工程智能体”
  • docker 容器web站点 中文文件名访问404问题
  • 【第五章:计算机视觉-项目实战之推荐/广告系统】2.粗排算法-(1)粗排用来干什么?“我大体筛一下“