【问题标题】:Get Canvas Color at Point在点处获取画布颜色
【发布时间】:2016-12-10 18:24:22
【问题描述】:

让我先解释一下我要做什么。 我正在尝试在右侧创建一个颜色选择器控件,例如其中一个:http://demos.telerik.com/silverlight/Themesgenerator/ 但是我想自己创建它以供学习。

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

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

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

    [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 的特定点,我将颜色保留在其中! 这真的很棒!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 2019-03-13
      相关资源
      最近更新 更多