miércoles, 16 de marzo de 2011

Obtener Color de un Pixel de un UIElement en Silverlight (Windows Phone 7)

Hay diferentes requerimientos en los que nos podria ser útil leer un pixel de un elemento grafico en Silverlight o en aplicaciones Windows Phone 7, en seguida coloco una función para obtener el color, de un pixel dado el par ordenado X,Y y el UIElement.


/// <summary>
        /// Get a Pixel Color from UIElement
        /// </summary>
        /// <param name="UIObject">UI Element</param>
        /// <param name="X">X Axis magnitude</param>
        /// <param name="Y">Y Axis magnitude</param>
        /// <returns></returns>
        public static Color GetPixelColor(UIElement UIObject, double X, double Y) {
            var bmp = new WriteableBitmap(UIObject, new MatrixTransform());
            int index=(int)(bmp.PixelWidth * Y + X);
            Color color ;
            if (index < bmp.Pixels.Length)
            {
                int c = bmp.Pixels[index];
                color = Color.FromArgb((byte)(c >> 24), (byte)(c >> 16), (byte)(c >> 8), (byte)(c));
            }
            else {
                color = Colors.Black;
            }

            return color;
        }

Transacciones Fiori

  /UI2/CACHE Register service for UI2 cache use /UI2/CACHE_DEL Delete cache entries /UI2/CHIP Chip Registration /UI2/CUST Customizing of UI ...