文章标题实在不懂怎么表述才清楚。

描述问题:多个图片(图层)重叠时,如何通过鼠标点击来拾取到相应的图层。因为图层中会有很多空白的地方(比如图片四周),要求是获取鼠标点击位置的像素颜色值,如果为空白时或透明度小于50%,则穿透到下一层图层再次进行颜色判断,直到找到符合条件的图层。


根据颜色拾取搜索WPF Color Picker,找到一些相关的资料:

为了适应当前需求,稍作整理之后,提取为一个工具类,代码有点长这里只放个链接,本文重点是如何使用这个工具类来实现需求:


阅读Demo可知:

  • 需要把能被拾取到的图片,专门放到一个集合中,如
ObservableCollection<ImageColorPicker> ImageList
  • 在遍历该集合时,遍历的顺序即是检测点击位置的像素颜色值所属图层的顺序(点击穿透的顺序)。即在将图片加入集合的时候,应该将处于上层的图片先加入集合。
        /// <summary>
/// 根据鼠标位置,拾取图像
/// </summary>
private void PickImageByMousePosition()
{
// 获得鼠标相对于当前窗口的位置
Point mousePoint = Mouse.GetPosition(curWindow);        // 遍历图层列表,找到符合条件的图层
foreach (ImageColorPicker image in ImageList)
{
// 鼠标相对于当前遍历到的图片的位置(以图片左下角为原点?)
Point p = Mouse.GetPosition(image); Color color = new Color(); // 鼠标在图片外面,就遍历下一张图片
if (p.X < || p.Y < || p.X > image.ActualWidth || p.Y > image.ActualHeight)
{
continue;
} // 获取鼠标位置的颜色值
color = image.PickColor(p.X, p.Y); // 无色或透明度小于0.5的图层,不是目标图层
if (color != null && color.A <= )
{
continue;
}          // 能执行到这一步,说明找到了目标图层
         // to what you want break;
} }

进阶:给每张图片(每个图层)添加鼠标事件,通过点击事件来执行遍历检查。

先给图片集合中的每个图片添加一个鼠标左键弹起事件,该事件是激发下一张图片的鼠标左键弹起事件,从而实现事件穿透,直到找到符合要求的图片。

        // 每次添加处理器AddHandler之前先清空Handler,防止重复多次添加
for (int i = ; i < ImageList.Count; i++)
{
ImageList[i].RemoveHandler(Image.MouseLeftButtonUpEvent, new RoutedEventHandler(DesignImage_PreviewMouseLeftButtonUp));
} // 给拾取图片添加鼠标事件
foreach (ImageColorPicker image in ImageList)
{
image.AddHandler(Image.MouseLeftButtonUpEvent, new RoutedEventHandler(DesignImage_PreviewMouseLeftButtonUp));
}

然后是激发的事件。

        private void DesignImage_PreviewMouseLeftButtonUp(object sender, RoutedEventArgs e)
{
// 当前激发事件的图片
ImageColorPicker image = (ImageColorPicker)sender;

// 当前图片在集合中的角标
int currentIndex = GetIndex(image); // 鼠标相对于该图片的位置
Point p = Mouse.GetPosition(image); Color color = new Color(); try
{
// 位于图像外部,抛异常执行Catch(跳过该层,继续穿透到下一层)
if (p.X < || p.Y < || p.X > image.ActualWidth || p.Y > image.ActualHeight)
{
throw new Exception("p.X或p.Y小于0!");
} color = image.PickColor(p.X, p.Y); // 获取鼠标位置的颜色值
if (color != null && color.A <= ) // 无色或透明度小于0.5
{
ImageColorPicker NextImage = null;
if (currentIndex < ImageList.Count - )
{
NextImage = ImageList[currentIndex + ];
}
if (NextImage != null) // 传递到下一图层,下一层激发该事件
{
MouseDevice mouseDevice = Mouse.PrimaryDevice;
MouseButtonEventArgs mouseButtonEventArgs = new MouseButtonEventArgs(mouseDevice, , MouseButton.Left);
mouseButtonEventArgs.RoutedEvent = Image.MouseLeftButtonUpEvent;
mouseButtonEventArgs.Source = NextImage;
NextImage.RaiseEvent(mouseButtonEventArgs);
}
}
else // 找到了有颜色的图层,即是目标图层
{
// do what you want
}
}
}
catch (Exception)
{
System.Console.WriteLine("----- 图层拾取发生异常,则继续穿透到下一层 ------");
// 点击到了图层的外部,则继续穿透到下一层
ImageColorPicker NextImage = null;
if (currentIndex < fileDataService.DesignViewModelList[i].ImageList.Count - )
{
NextImage = fileDataService.DesignViewModelList[i].ImageList[currentIndex + ];
}
if (NextImage != null)
{
string nextType = NextImage.Uid;
MouseDevice mouseDevice = Mouse.PrimaryDevice;
MouseButtonEventArgs mouseButtonEventArgs = new MouseButtonEventArgs(mouseDevice, , MouseButton.Left);
mouseButtonEventArgs.RoutedEvent = Image.MouseLeftButtonUpEvent;
mouseButtonEventArgs.Source = NextImage;
NextImage.RaiseEvent(mouseButtonEventArgs);
}
}
} /// <summary>
/// 当前image在ImageList列表中的角标
/// </summary>
/// <param name="image">当前ImageColorPicker</param>
/// <returns></returns>
int GetIndex(ImageColorPicker image)
{
object targetTag = image.Tag;
int index = -; for (int i = ; i < ImageList.Count; i++)
{
ImageColorPicker obj = ImageList[i];
if (obj.Equals(image))
{
index = i;
break;
}
} return index;
}

【WPF/C#】图层筛选/拾取——Color Picker的更多相关文章

  1. NX二次开发-Block UI C++界面Object Color Picker(对象颜色拾取器)控件的获取(持续补充)

    Object Color Picker(对象颜色拾取器)控件的获取 NX9+VS2012 #include <uf.h> #include <uf_obj.h> UF_init ...

  2. [wordpress]后台自定义菜单字段和使用wordpress color picker

    Wordpress Version 4.4.2 参考链接 插件使用wordpress color picker:Add A New Color Picker To WordPress 后台菜单自定义字 ...

  3. 使用canvas制作的移动端color picker

    使用canvas制作的移动端color picker 项目演示地址(用手机或者手机模式打开) 我在另一个中demo,需要用到color picker,但是找不到我需要的移动端color picker, ...

  4. WPF 自定义列表筛选 自定义TreeView模板 自定义ListBox模板

    有很多项目,都有数据筛选的操作.下面提供一个案例,给大家做参考. 左侧是数据源,搜索框加TreeView控件,右侧是ListBox控件.在左侧数据列点击添加数据,然后点击确定,得到所筛选的数据. 下面 ...

  5. 例子:Camera Color Picker Sample (YCbCr->ARGB)

    本例演示了如何从相机preview缓冲区获取YCbCr模块,并且转化为ARGB. 1. 什么是YCbCr y:像素的亮度.以范围从 0 到 255 的字节值形式返回(亮度值始终为正值). cr:像素的 ...

  6. 颜色采集器colpick Color Picker

    简单 RGB.HSB.十六进制颜色选取器 jQuery 插件. 非常直观类似 Photoshop 的界面. 光明和黑暗很容易自定义 CSS3 外观. 28 KB 总由浏览器加载看起来不错甚至在 IE7 ...

  7. C# WPF Datagrid的筛选

    public static void SearchResult(DataGrid dg,string condition) { #region string code = string.Empty; ...

  8. Linux下无法运行Color picker

    ➜ ~ com.github.ronnydo.colorpicker com.github.ronnydo.colorpicker: error while loading shared librar ...

  9. An easy to use android color picker library

    https://github.com/xdtianyu/ColorPicker

随机推荐

  1. C/C++语言中闭包的探究及比较

    这里主要讨论的是C语言的扩展特性block.该特性是Apple为C.C++.Objective-C增加的扩展,让这些语言可以用类Lambda表达式的语法来创建闭包.前段时间,在对CoreData存取进 ...

  2. 第二篇:呈现内容_第二节:WebControl呈现

    一.WebControl的呈现过程 WebControl派生自Control类,所以WebControl的呈现功能基于Control的呈现逻辑之上,但有了比较大的扩展. 首先,WebControl重写 ...

  3. Maven知识点整理

    1. 基础:maven 概念及生命周期 ===>Nexus创建本地Maven仓库(Maven私服) https://www.cnblogs.com/zishengY/p/7794923.html ...

  4. Android Activity全面解析

    Android Activity全面解析 首先,就从Android四大组件Activity开始. 1.Activity生命周期方法完全解析   activity_lifecycle.png 1).on ...

  5. mongoimport mongo导入Json的用法

    //导入json mongoimport --db ML_OER --collection lecture --file /home/tmp/course_temp.json //导入Json数组 m ...

  6. JSTL标签之&lt;c:choose&gt;&lt;c:when&gt;&lt;c:otherwise&gt;标签

    假设是JSTL1.1版本号,使用<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" ...

  7. RecyclerView 与 ItemTouchHelper 实现拖拽效果

    截图 需求 App 开发新的需求,要求 RecyclerView 实现的九宫格样式可以拖拽,松手以后变更位置,类似于手机桌面拖动 app 变更位置. 分析 经过搜索,发现 support 中带有一个类 ...

  8. 如何改变git的默认路径

    1.win10下git默认启动路径是用户的根目录,东西太多太乱了. 2.修改很容易,右键单击桌面的快捷方式,选择“属性”. 3.删除“目录”中的 --cd-to-home 选项,再将“起始位置&quo ...

  9. pannel加载form

    panel2.Controls.Clear(); frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; frm.ShowIc ...

  10. 使用FiddlerCore来截取替换Http请求中的网页内容

    做过测试的应该都知道Fiddler,它可以很方便截取Internet上的网页替换成本地的,或者修改其中的一部分内容后呈现.简单地说就是可能监测所有HTTP连接,设置断点,胡乱修改.是测试调试的一件利器 ...