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 wpf_Color_Selector._UserControls
{
/// <summary>
/// Interaction logic for
UcColorSelector.xaml
/// </summary>
public partial class UcColorSelector : UserControl
{
public UcColorSelector()
{
InitializeComponent();
}
private void btnSelect_Click(object sender, RoutedEventArgs e)
{
//select_Color();
}
private void slider_Color_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
//------------<
slider_Color_ValueChanged() >------------
//*change Cube from White to Black
if (rectColor_Background != null)
{
double imgOpacity = slider_Color.Value;
imgCubeWhite.Opacity =
imgOpacity;
}
//------------</
slider_Color_ValueChanged() >------------
}
private void imgCubeWhite_MouseMove(object sender, MouseEventArgs e)
{
//------------<
imgCubeWhite_MouseMove() >------------
select_Color(sender, e);
//------------</
imgCubeWhite_MouseMove() >------------
}
private void select_Color(object sender, MouseEventArgs e)
{
try
{
BitmapSource visual_BitmapSource =
get_BitmapSource_of_Element( imgCubeWhite
);
CroppedBitmap cb = new CroppedBitmap(visual_BitmapSource, new Int32Rect((int)Mouse.GetPosition(imgCubeWhite).X, (int)Mouse.GetPosition(imgCubeWhite).Y, 1, 1));
byte[] pixels = new byte[4];
try
{
cb.CopyPixels(pixels, 4,
0);
}
catch (Exception)
{
//error
}
rectSelected.Fill = new SolidColorBrush(Color.FromRgb(pixels[2], pixels[1],
pixels[0]));
}
catch (Exception)
{
//not much we can do
}
}
public BitmapSource get_BitmapSource_of_Element(FrameworkElement element)
{
//-------------<
get_BitmapSource_of_Element() >------------
//< check >
if (element == null){return null;}
//</ check >
//< init >
double dpi = 96;
Double width = element.ActualWidth;
Double height = element.ActualHeight;
//</ init >
RenderTargetBitmap bitmap_of_Element = null;
if(bitmap_of_Element==null)
{
try
{
//< create empty Bitmap of element
>
bitmap_of_Element = new RenderTargetBitmap((int) width , (int)height, dpi, dpi, PixelFormats.Default );
//</ create empty Bitmap of element
>
//----< render area into bitmap
>----
DrawingVisual visual_area = new DrawingVisual();
using (DrawingContext dc = visual_area.RenderOpen())
{
VisualBrush visual_brush = new VisualBrush(element );
dc.DrawRectangle(visual_brush, null, new Rect(new Point(), new Size(width, height)));
}
//< render >
bitmap_of_Element.Render(visual_area);
//</ render >
//----</ render area into bitmap
>----
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
return bitmap_of_Element;
//-------------</ get_BitmapSource_of_Element()
>------------
}
}
}
|