在点获取画布颜色 [英] Get Canvas Color at Point

查看:25
本文介绍了在点获取画布颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我先解释一下我要做什么.我正在尝试创建一个颜色选择器控件,类似于右侧的其中一个:http://demos.Telerik.com/silverlight/Themesgenerator/但是我想自己创建它来学习.

Let me first explain what I am trying to do. I am trying to create a Color picker Control like one of these on the right side: http://demos.telerik.com/silverlight/Themesgenerator/ However I want to create it myself for Learning.

目前我已经在 xaml 中进行了某种布局,并且我使用了带有 LinearGradientBrush 背景的 Canvas.现在我在尝试确定特定点的颜色时被卡住了.有什么好的方法可以找到这个吗?.. 我想点击我的画布并获取该特定点的 ARGB.任何帮助将不胜感激.

At the moment I have made some sort of layout in xaml, and I use a Canvas with a LinearGradientBrush background. Now I am stuck while trying to decide which Color is at the specific Point. Is there any good way of finding this?.. I want to click at my canvas and get the ARGB of that specific Point. Any help would be appreciated.

推荐答案

我找到了解决方案!如果有人需要它就在这里!

I found the solution! Here it is if anyone need it!

[DllImport("gdi32")]
private static extern int GetPixel(int hdc, int nXPos, int nYPos);

[DllImport("user32")]
private static extern int GetWindowDC(int hwnd);

[DllImport("user32")]
private static extern int ReleaseDC(int hWnd, int hDC);

private static SolidColorBrush GetPixelColor(Point point)
{
    int lDC = GetWindowDC(0);
    int intColor = GetPixel(lDC, (int)point.X, (int)point.Y);

    // Release the DC after getting the Color.
    ReleaseDC(0, lDC);

    byte a = (byte)( ( intColor >> 0x18 ) & 0xffL );
    byte b = (byte)((intColor >> 0x10) & 0xffL);
    byte g = (byte)((intColor >> 8) & 0xffL);
    byte r = (byte)(intColor & 0xffL);
    Color color = Color.FromRgb(r, g, b);
    return new SolidColorBrush(color);
}

我这样调用这个方法:

SolidColorBrush solidcolor = GetPixelColor(RightColorPanel.PointToScreen(point));

Color color = Color.FromArgb(solidcolor.Color.A,
                             solidcolor.Color.R,
                             solidcolor.Color.G,
                             solidcolor.Color.B);

LinearGradientBrush brush = new LinearGradientBrush();
brush.StartPoint = new Point(0, 0);
brush.EndPoint = new Point(1, 0);
brush.GradientStops.Add(new GradientStop(Colors.White, 0.0));
brush.GradientStops.Add(new GradientStop(color, 1));

MainColorPanel.Background = brush;

其中 point 是我的 RightColorPanel 的特定点,我将颜色保持在该点!这真的很棒!

Where point is the Specific Point of my RightColorPanel that I keep my Colors at! This works realy great!

这篇关于在点获取画布颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆