Click here to Skip to main content
15,886,724 members
Articles / Desktop Programming / WPF

WPF: Windows 7 like Button Color Hot Tracking

,
Rate me:
Please Sign up or sign in to vote.
4.26/5 (11 votes)
28 Oct 2009CPOL1 min read 50.2K   1K   42  
A nice idea that you can apply to your code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Collections;
using System.Drawing;
using System.IO;

namespace ColorHotTrackButton
{
    /// <summary>
    /// Converter writen by Rudi, just modified a bit
    /// </summary>
    public class IconToAvgColorBrushConverter : IValueConverter
    {
        /// <summary>
        /// The converter method
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Verify if the value is null, if it is return a transparent brush
            if (value == null)
            {
                return System.Windows.Media.Brushes.Transparent;
            }

            // Instanciate a MemoryStream to acomodate the bitmap
            MemoryStream stream = new MemoryStream();

            // verify if the value is from the espected Type
            if (value is RenderTargetBitmap)
            {
                // Load the rendered visual element from the render to a BitmapFrame
                BitmapFrame frame = BitmapFrame.Create(value as RenderTargetBitmap);
                // Instanciate the Encoder for the image and add the frame to it
                System.Windows.Media.Imaging.BmpBitmapEncoder e = new BmpBitmapEncoder();
                e.Frames.Add(frame);
                // Save the data to the stream object
                e.Save(stream);
            }

            try
            {
                using (Bitmap bitmap = new Bitmap(stream))
                {
                    // Create an Array to acomodate the colors from the image
                    ArrayList colors = new ArrayList();

                    // simple loop to load each color to the array
                    for (int x = 0; x < bitmap.Width; x++)
                    {
                        for (int y = 0; y < bitmap.Height; y++)
                        {
                            System.Drawing.Color pixel = bitmap.GetPixel(x, y);
                            // verify if the color is transparent because a color of #00000000 would darker the brush
                            if (pixel.A > 0x00)
                                colors.Add(pixel);
                        }
                    }

                    // Using linq to get the average color RGB bytes
                    byte r = (byte)Math.Floor(colors.Cast<System.Drawing.Color>().Average(c => c.R));
                    byte g = (byte)Math.Floor(colors.Cast<System.Drawing.Color>().Average(c => c.G));
                    byte b = (byte)Math.Floor(colors.Cast<System.Drawing.Color>().Average(c => c.B));

                    // Instanciate and initialize the LinearGradientBrush that will be returned as the result of the operation
                    LinearGradientBrush brush = new LinearGradientBrush();
                    brush.EndPoint = new System.Windows.Point(0.5, 1.0);
                    brush.StartPoint = new System.Windows.Point(0.5, 0.0);
                    brush.GradientStops.Add(new GradientStop(System.Windows.Media.Color.FromArgb(0x00, r, g, b), 0.00));
                    brush.GradientStops.Add(new GradientStop(System.Windows.Media.Color.FromArgb(0xFF, r, g, b), 1.00));
                    return brush;
                }
            }
            catch (Exception)
            {
                // If any error occours return a Tranparent brush
                return System.Windows.Media.Brushes.Transparent;
            }
        }

        /// <summary>
        /// Just ignored cause it is not supposed to do anything
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Written By
Architect
Brazil Brazil
Senior Software Architect from Brazil.

Comments and Discussions