WPF Adorner 简易图片取色器
回答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 简易图片取色器的更多相关文章
- wpf 仿QQ图片查看器
参考博客 WPF下的仿QQ图片查看器 wpf图片查看器,支持鼠标滚动缩放拖拽 实现效果 主要参考的WPF下的仿QQ图片查看器,原博主只给出了部分代码. 没有完成的部分 1.右下角缩略图是原图不是缩略图 ...
- Qt 简易图片播放器
一.前言 使用 Qt 制作了一个简单的图片播放器,点击 "浏览按钮" 浏览图片所在目录,目录中的所有图片缩小图标和名称会显示在左侧的图片列表中,点击列表中的图片项,可以在右侧区域的 ...
- 简易音乐播放器主界面设计 - .NET CORE(C#) WPF开发
微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言, 如果对您有所帮助:欢迎赞赏. 简易音乐播放器主界面设计 - .NET CORE(C#) WPF开发 阅读导航 本文背景 代码 ...
- wpf图片查看器,支持鼠标滚动缩放拖拽
最近项目需要,要用到一个图片查看器,类似于windows自带的图片查看器那样,鼠标滚动可以缩放,可以拖拽图片,于是就写了这个简单的图片查看器. 前台代码: <Window x:Class=&qu ...
- WPF技术触屏上的应用系列(四): 3D效果图片播放器(图片立体轮放、图片立体轮播、图片倒影立体滚动)效果实现
原文:WPF技术触屏上的应用系列(四): 3D效果图片播放器(图片立体轮放.图片立体轮播.图片倒影立体滚动)效果实现 去年某客户单位要做个大屏触屏应用,要对档案资源进行展示之用.客户端是Window7 ...
- powerpoint取色器有什么用|ppt取色器使用教程
在使用powerpoint过程中常常发现一些功能我们很少用到,其实是自己不会用的原因,关于powerpoint取色器有什么用呢?接下来我们一起来学一下ppt取色器使用教程. powerpoint取色器 ...
- TakeColor 屏幕取色器 8.0 中文绿色版
软件名称: TakeColor 屏幕取色器软件语言: 简体中文授权方式: 免费软件运行环境: Win8 / Win7 / Vista / WinXP软件大小: 210KB图片预览: 软件简介:使用方便 ...
- Arava: 用 swing 写一个取色器
备哥以前发我了一个小工具,TakeColor 取色器.来复刻一个吧. 分析一下:顶部是菜单,左侧框显示当前鼠标所在的颜色(下面显示当前坐标和颜色值,默认RGB),中间框显示鼠标周围区域,右侧显示取色的 ...
- wpf下的图片放大缩小
WPF下实现图片的放大缩小移动 在windows 7里面有自带的图片查看器,这个软件可以打开一张图片然后以鼠标在图片中的焦点为原点来进行缩放,并且放大后可以随意拖动.下面我们在WPF中实现这个功能 ...
随机推荐
- SmtpClient发送邮件时附件名称乱码
在用户环境发现一个现象,使用System.Net.Mail.SmtpClient发送邮件,当附件名包含中文且长度较长时,最终的邮件里附件名会乱掉,写个简单的测试程序: var mail = new M ...
- Python 容器使用的 5 个技巧和 2 个误区
"容器"这两个字很少被 Python 技术文章提起.一看到"容器",大家想到的多是那头蓝色小鲸鱼:Docker,但这篇文章和它没有任何关系.本文里的容器,是 P ...
- startsWith(),endsWith()判断当前字符串是否是以给定字符串开始或结尾的
package seday01;/** * boolean startsWith(String str) * boolean endsWith(String str) * 判断当前字符串是否是以给定字 ...
- 这可能最简单的一种PS图片特效,零基础小白教程
不少小伙伴都想学习PS,可是又觉得PS很难,学了一段时间却还是做不出什么惊艳的效果,没关系!小编今天就来教大家做一个超级简单的图片特效,就算是小白也能轻松学会!我们先来看看图片效果~ 想知道怎么做吗? ...
- jpa分页
法一(本地sql查询,注意表名啥的都用数据库中的名称,适用于特定数据库的查询) public interface UserRepository extends JpaRepository<Use ...
- PHP fread 文件系统函数
定义和用法 fread - 读取文件(可安全用于二进制文件) 版本支持 PHP4 PHP5 PHP7 支持 支持 支持 语法 fread ( resource $handle , int $lengt ...
- 被公司的垃圾XG人事系统吓尿了
OA要尝试设置单点登录,拿现有的HR系统尝试,结果不知道HR系统的加密方式和验证地址,于是乎找HR厂商——厦门XG软件实施人员.结果那个技术人员支支吾吾不肯给我,搞得非常的烦. 真奇怪了,不开源的软件 ...
- (一)创建新的react native 应用程序
最近开始学习ReactNative了,首先了解下ReactNative http://wiki.jikexueyuan.com/project/react-native/GettingStarted. ...
- Toast实现源码解析
说明 本篇文章用于介绍Android中Toast的实现原理.和简单实现一个自定义的Toast. Toast实现 一般常用Toast格式为: Toast.makeText(context,"t ...
- Android Monkey使用
Monkey 是什么? Android SDK自带的压力测试工具,也是一个命令行工具.它向系统发送伪随机的用户事件流(如按键输入,触摸屏输入,手势输入等),实现对正在开发的应用程序进行压力测试. (1 ...