回答MSDN问题所写。

使用Adorner+附加属性

图片类(来自这位博主的博客

  /// <summary>
/// 用于获取位图像素的类
/// </summary>
public class Imghelper
{
/// <summary>
/// 位图宽度
/// </summary>
public int Width { get; protected set; }
/// <summary>
/// 位图高度
/// </summary>
public int Height { get; protected set; }
/// <summary>
/// 像素
/// </summary>
public Color[][] Pixels { get; protected set; } /// <summary>
/// 根据指定的位图生成BitmapPixelHelper类的新实例。
/// </summary>
/// <param name="bitmap">指定的位图</param>
public Imghelper(BitmapSource bitmap)
{
FormatConvertedBitmap newBitmap = new FormatConvertedBitmap(bitmap, PixelFormats.Bgra32, BitmapPalettes.WebPaletteTransparent, );
const int bytesPerPixel = ;
Height = newBitmap.PixelHeight;
Width = newBitmap.PixelWidth;
byte[] data = new byte[Height * Width * bytesPerPixel];
newBitmap.CopyPixels(data, Width * bytesPerPixel, ); Pixels = new Color[Height][];
for (int i = ; i < Height; ++i)
{
Pixels[i] = new Color[Width];
for (int j = ; j < Width; ++j)
{
Pixels[i][j] = Color.FromArgb(
data[(i * Width + j) * bytesPerPixel + ],
data[(i * Width + j) * bytesPerPixel + ],
data[(i * Width + j) * bytesPerPixel + ],
data[(i * Width + j) * bytesPerPixel + ]);
}
}
} /// <summary>
/// 获取图片的平均色
/// </summary>
public Color GetAverageColor()
{
int a = , r = , g = , b = ;
for (int i = ; i < Height; ++i)
{
for (int j = ; j < Width; ++j)
{
a += Pixels[i][j].A;
r += Pixels[i][j].R;
g += Pixels[i][j].G;
b += Pixels[i][j].B;
}
}
a = a / Height / Width;
r = r / Height / Width;
g = g / Height / Width;
b = b / Height / Width;
return Color.FromArgb((byte)a, (byte)r, (byte)g, (byte)b);
}
}

adorner代码

public class ShowImagePixelsPopup : Adorner
{ private TextBlock GetTextBlock;
private VisualCollection collection;
private UIElement _UIElement;
private Border GetBorder; public ShowImagePixelsPopup(UIElement adornedElement) : base(adornedElement)
{
collection = new VisualCollection(this); GetTextBlock = new TextBlock(); GetTextBlock.Height = ;
GetTextBlock.Width = ;
GetTextBlock.Background = new SolidColorBrush(Colors.Wheat);
GetTextBlock.HorizontalAlignment = HorizontalAlignment.Left;
GetTextBlock.VerticalAlignment = VerticalAlignment.Top;
GetBorder = new Border();
GetBorder.Height = ;
GetBorder.Width = ;
GetBorder.HorizontalAlignment = HorizontalAlignment.Left;
GetBorder.VerticalAlignment = VerticalAlignment.Top;
collection.Add(GetTextBlock);
collection.Add(GetBorder);
_UIElement = adornedElement;
}
protected override int VisualChildrenCount => collection.Count; protected override Visual GetVisualChild(int index) => collection[index]; protected override Size MeasureOverride(Size constraint) => base.MeasureOverride(constraint); public void SetData(Point MousePoint, String Pixels,Color color)
{
GetTextBlock.Margin = new Thickness(MousePoint.X+7.5, MousePoint.Y-, ,);
GetBorder.Margin = new Thickness(MousePoint.X-7.5 , MousePoint.Y-7.5 , , );
GetBorder.Background = new SolidColorBrush(color);
GetTextBlock.Text = Pixels;
} protected override Size ArrangeOverride(Size finalSize)
{
GetTextBlock.Arrange(new Rect(finalSize));
GetBorder.Arrange(new Rect(finalSize));
return base.ArrangeOverride(finalSize); }
}

附加属性类

public class IsShowImagePixels
{
public static readonly DependencyProperty IsShowImagePixelsProperty = DependencyProperty.RegisterAttached("IsShowImagePixels", typeof(bool), typeof(IsShowImagePixels), new PropertyMetadata(false, new PropertyChangedCallback(OnIsShowImagePixelsChanged))); public static void SetIsShowImagePixels(DependencyObject d, bool value) => d.SetValue(IsShowImagePixelsProperty, value); public static bool GetIsShowImagePixels(DependencyObject d) => (bool)d.GetValue(IsShowImagePixelsProperty); public static readonly DependencyProperty ShowImagePixelsPointProperty = DependencyProperty.RegisterAttached("ShowImagePixelsPoint", typeof(Point), typeof(IsShowImagePixels), new PropertyMetadata(new Point(, ),new PropertyChangedCallback(OnShowImagePixelsPointChanged))); public static void SetIsShowImagePixelsPoint(DependencyObject d, Point value) => d.SetValue(ShowImagePixelsPointProperty, value); public static Point GetShowImagePixelsPoint(DependencyObject d) => (Point)d.GetValue(ShowImagePixelsPointProperty); private static void OnShowImagePixelsPointChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var c =(Point)e.NewValue; popup.SetData(c, $"X={(((int)c.X ) < 0 ? 0 : (int)c.X )},Y={(((int)c.Y ) < 0 ? 0 : (int)c.Y )}", imghelper.Pixels[((int)c.Y - ) < ? : (int)c.Y - ][((int)c.X - ) < ? : (int)c.X - ]); }
private static AdornerLayer layer;
private static Imghelper imghelper;
private static ShowImagePixelsPopup popup; private static void OnIsShowImagePixelsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ var NewValue = (bool)e.NewValue;
UIElement element = d as UIElement;
if (!NewValue)
{ AdornerLayer l = AdornerLayer.GetAdornerLayer(element);
var ado = l.GetAdorners(element);
for (var o = ; o < ado.Length; o++)
l.Remove(ado[o]);
element.MouseMove -= Element_MouseMove; imghelper = null;
popup = null;
layer = null;
element = null;
}
if (element == null)
return;
layer = AdornerLayer.GetAdornerLayer(element);
popup = new ShowImagePixelsPopup(element);
layer.Add(popup);
imghelper = new Imghelper((element as Image).Source as BitmapSource);
//显示鼠标位置
element.MouseMove += Element_MouseMove;
} private static void Element_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{ var c = e.GetPosition(sender as FrameworkElement);
//此处只是用了鼠标位置,也可以用ShowImagePixelsPoint直接指定位置
popup.SetData(c, $"X={(((int)c.X - 1) < 0 ? 0 : (int)c.X - 1)},Y={(((int)c.Y - 1) < 0 ? 0 : (int)c.Y - 1)}", imghelper.Pixels[((int)c.Y - ) < ? : (int)c.Y - ][((int)c.X - ) < ? : (int)c.X - ]); }
}

xaml代码

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<AdornerDecorator Grid.Row="">
<Image x:Name="img" />
</AdornerDecorator>
<Button Click="Button_Click" Grid.Row="" Height="" Content="ShowOrNot"/>
</Grid>

cs页面代码

  public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var b = new BitmapImage(new Uri("timg.jpg", UriKind.RelativeOrAbsolute));
Imghelper imghelper = new Imghelper(b);
img.Source = b;
img.SetValue(IsShowImagePixels.IsShowImagePixelsProperty, true);
Set = true;
}
private bool Set = false;
private void Button_Click(object sender, RoutedEventArgs e)
{ if (Set)
Set = false;
else
Set = true;
img.SetValue(IsShowImagePixels.IsShowImagePixelsProperty, Set);
return; }
}

WPF Adorner 简易图片取色器的更多相关文章

  1. wpf 仿QQ图片查看器

    参考博客 WPF下的仿QQ图片查看器 wpf图片查看器,支持鼠标滚动缩放拖拽 实现效果 主要参考的WPF下的仿QQ图片查看器,原博主只给出了部分代码. 没有完成的部分 1.右下角缩略图是原图不是缩略图 ...

  2. Qt 简易图片播放器

    一.前言 使用 Qt 制作了一个简单的图片播放器,点击 "浏览按钮" 浏览图片所在目录,目录中的所有图片缩小图标和名称会显示在左侧的图片列表中,点击列表中的图片项,可以在右侧区域的 ...

  3. 简易音乐播放器主界面设计 - .NET CORE(C#) WPF开发

    微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言, 如果对您有所帮助:欢迎赞赏. 简易音乐播放器主界面设计 - .NET CORE(C#) WPF开发 阅读导航 本文背景 代码 ...

  4. wpf图片查看器,支持鼠标滚动缩放拖拽

    最近项目需要,要用到一个图片查看器,类似于windows自带的图片查看器那样,鼠标滚动可以缩放,可以拖拽图片,于是就写了这个简单的图片查看器. 前台代码: <Window x:Class=&qu ...

  5. WPF技术触屏上的应用系列(四): 3D效果图片播放器(图片立体轮放、图片立体轮播、图片倒影立体滚动)效果实现

    原文:WPF技术触屏上的应用系列(四): 3D效果图片播放器(图片立体轮放.图片立体轮播.图片倒影立体滚动)效果实现 去年某客户单位要做个大屏触屏应用,要对档案资源进行展示之用.客户端是Window7 ...

  6. powerpoint取色器有什么用|ppt取色器使用教程

    在使用powerpoint过程中常常发现一些功能我们很少用到,其实是自己不会用的原因,关于powerpoint取色器有什么用呢?接下来我们一起来学一下ppt取色器使用教程. powerpoint取色器 ...

  7. TakeColor 屏幕取色器 8.0 中文绿色版

    软件名称: TakeColor 屏幕取色器软件语言: 简体中文授权方式: 免费软件运行环境: Win8 / Win7 / Vista / WinXP软件大小: 210KB图片预览: 软件简介:使用方便 ...

  8. Arava: 用 swing 写一个取色器

    备哥以前发我了一个小工具,TakeColor 取色器.来复刻一个吧. 分析一下:顶部是菜单,左侧框显示当前鼠标所在的颜色(下面显示当前坐标和颜色值,默认RGB),中间框显示鼠标周围区域,右侧显示取色的 ...

  9. wpf下的图片放大缩小

    WPF下实现图片的放大缩小移动   在windows 7里面有自带的图片查看器,这个软件可以打开一张图片然后以鼠标在图片中的焦点为原点来进行缩放,并且放大后可以随意拖动.下面我们在WPF中实现这个功能 ...

随机推荐

  1. Git 实用命令记录

    自从上次写了一篇 Git 入门 的相关博客以来,一直自以为自己能完全的掌握 Git,其实不然,今天一小伙问我,如何删除远程上面的一个分支,呃,不会. git branch -d 分支名 只能删除本地的 ...

  2. 死磕 java同步系列之ReentrantLock源码解析(二)——条件锁

    问题 (1)条件锁是什么? (2)条件锁适用于什么场景? (3)条件锁的await()是在其它线程signal()的时候唤醒的吗? 简介 条件锁,是指在获取锁之后发现当前业务场景自己无法处理,而需要等 ...

  3. 最近的项目系之3——core3.0整合Senparc

    1.前言 既然是.net下微信开发,自然少不了Senparc,可以说这个框架的存在, 至少节省了微信相关工作量的80%.事实上,项目开始前,还纠结了下是Java还是core,之所以最终选择core,除 ...

  4. python基础(4):用户交互、if判断、while循环、break和continue

    1. 用户交互 使⽤input()函数,可以让我们和计算机互动起来 语法: 内容 = input(提⽰信息) 这⾥可以直接获取到⽤户输入的内容 content = input("你吃了么?& ...

  5. Zuul 1.x 的工作原理

    Zuul简介 Zuul在微服务架构中,可以作为提供动态路由,监控,弹性,安全等边缘服务的框架.在Netflix,被用作所有请求到达streaming application的前门.Zuul使用一系列不 ...

  6. jpa分页

    法一(本地sql查询,注意表名啥的都用数据库中的名称,适用于特定数据库的查询) public interface UserRepository extends JpaRepository<Use ...

  7. 多线程学习二:线程池 ExecutorService

    创建线程池的2种方式: 使用线程池方式1--Runnable接口: 通常,线程池都是通过线程池工厂创建,再调用线程池中的方法获取线程,再通过线程去执行任务方法. Executors:线程池创建工厂类: ...

  8. 图片在DIV里边水平垂直居中

    图片在一个DIV中要垂直水平居中,首先定义一个DIV .wrap{ width: 600px; height: 400px; border: 1px #000 solid; } 插入图片 <di ...

  9. 连接常见错误linker command failed with exit code 1 (use -v to see invocation)

    这种问题,通常出现在添加第三方库文件或者多人开发时. 这种问题一般是找不到文件而导致的链接错误. 我们可以从如下几个方面着手排查. 1.以如下错误为例,如果是多人开发,你同步完成后发现出现如下的错误. ...

  10. ucoreOS_lab8 实验报告

    所有的实验报告将会在 Github 同步更新,更多内容请移步至Github:https://github.com/AngelKitty/review_the_national_post-graduat ...