Share:

Get Color From Pixel C# WPF | Saatody | Amit Padhiyar

Today I will show you, How to get pixel color from particular UIElement. Here we need two main class(es). RenderTargetBitmap and CroppedBitmap.

MainWindow.xaml

<Window x:Class="Pixel.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Pixel"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="300"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <Border Name="Picker" Grid.Row="0" BorderBrush="#000000" BorderThickness="1">
            <Border.Background>
                <LinearGradientBrush>
                    <GradientStop Color="#000000" Offset="0.0"/>
                    <GradientStop Color="#FF0000" Offset="0.1"/>
                    <GradientStop Color="#0FF000" Offset="0.2"/>
                    <GradientStop Color="#00FF00" Offset="0.3"/>
                    <GradientStop Color="#000FF0" Offset="0.4"/>
                    <GradientStop Color="#0000FF" Offset="0.5"/>
                    <GradientStop Color="#F0000F" Offset="0.6"/>
                    <GradientStop Color="#FFFFFF" Offset="0.7"/>
                    <GradientStop Color="#222222" Offset="0.8"/>
                    <GradientStop Color="#00FFFF" Offset="0.9"/>
                    <GradientStop Color="#FFFF00" Offset="1.0"/>
                </LinearGradientBrush>
            </Border.Background>
        </Border>
        <Border Name="SelectedColor" Grid.Row="1" Margin="0,5,0,0"/>
        <Label Name="RGB" Grid.Row="2" Margin="0,5,0,0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
    </Grid>
</Window>
If you want choose color from ImageBrush (image) instead of  LinearGradientBrush then change UIElement background. Logic will remain same. See the below code.
<Border Name="Picker" Grid.Row="0" BorderBrush="#000000" BorderThickness="1">
    <Border.Background>
        <ImageBrush ImageSource="Pixel\Resources\18.png" Stretch="UniformToFill"/>
    </Border.Background>
</Border>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Pixel
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Picker.MouseMove += (sender, e) =>
            {
                try
                {
                    Point P = e.GetPosition(this);
                    double X = P.X;
                    double Y = P.Y;
                    RenderTargetBitmap Render = new RenderTargetBitmap((int)Picker.ActualWidth, (int)Picker.ActualHeight, 96, 96, PixelFormats.Default);
                    Render.Render(Picker);

                    CroppedBitmap Cropped = new CroppedBitmap(Render, new Int32Rect((int)X, (int)Y, 1, 1));
                    byte[] Pixels = new byte[4];
                    Cropped.CopyPixels(Pixels, 4, 0);

                    SelectedColor.Background = new SolidColorBrush(Color.FromArgb(Pixels[3], Pixels[2], Pixels[1], Pixels[0]));
                    RGB.Content = "RGB(" + Pixels[2] + "," + Pixels[1] + "," + Pixels[0] + ")";
                }
                catch(Exception Ex)
                {
                    System.Console.WriteLine("Ex: " + Ex);
                }
            };
        }
    }
}

Output Of LinearGradientBrush

Output Of ImageBrush

You can also get color brush from VisualBrush. But problem is you can't get RGB.
double X = 0.5; // Range 0.0 to 1.0
double Y = 0.5; // Range 0.0 to 1.0

VisualBrush Brush = new VisualBrush();
Brush.Visual = ColorPanel; // Grid, StackPanel or any other UIElement as a color scale.  
Brush.ViewboxUnits = BrushMappingMode.RelativeToBoundingBox;
Brush.Viewbox = new Rect(X, Y, 0.0000001, 0.0000001);

// This way you can get Brush.
Brush MyColor = Brush;

// The problem is that you can't convert VisualBrush to any other brush like a SolidColorBrush.
// It will give you an exception.
SolidColorBrush SCBrush = (SolidColorBrush)MyColor;
If you try to convert VisualBrush to SolidColorBrush to get RGB color format then it will give you an exception.

Comments

Popular posts from this blog

Create AutoScroll V1_0_0 for WPF C# | Saatody | Amit Padhiyar

Basic Audio Operations With MP3 And Wave Files Using NAudio C#