Silverlight 图片灰化、亮度、对比度调整

using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace SFire.Framework.Media
{
    /// <summary>
    /// 图片效果处理类
    /// 创建者:sucsy
    /// 创建日期:2012-2-27
    /// </summary>
    public class ImageConverter
    {
        #region 图片灰化
        public static BitmapSource GetGrayImage(BitmapSource source)
        {
            return GetGrayImage(source, 1);
        }

        public static BitmapSource GetGrayImage(BitmapSource source,double p)
        {
            WriteableBitmap wb = new WriteableBitmap(source);
            for (int y = 0; y < wb.PixelHeight; y++)
            {
                for (int x = 0; x < wb.PixelWidth; x++)
                {
                    int pixel = wb.Pixels[y * wb.PixelWidth + x];
                    byte[] dd = BitConverter.GetBytes(pixel);
                    double R = dd[2];
                    double G = dd[1];
                    double B = dd[0];
                    byte gray = (byte)(0.299 * R * p + 0.587 * G * p + 0.114 * B * p);
                    dd[0] = dd[1] = dd[2] = gray;

                    wb.Pixels[y * wb.PixelWidth + x] = BitConverter.ToInt32(dd, 0);
                }
            }

            return wb;
        }

        public static BitmapSource GetGrayImage(BitmapSource source, double p1,double p2,double p3)
        {
            WriteableBitmap wb = new WriteableBitmap(source);
            for (int y = 0; y < wb.PixelHeight; y++)
            {
                for (int x = 0; x < wb.PixelWidth; x++)
                {
                    int pixel = wb.Pixels[y * wb.PixelWidth + x];
                    byte[] dd = BitConverter.GetBytes(pixel);
                    double R = dd[2];
                    double G = dd[1];
                    double B = dd[0];
                    byte gray = (byte)(0.299 * R * p1 + 0.587 * G * p2 + 0.114 * B * p3);
                    dd[0] = dd[1] = dd[2] = gray;

                    wb.Pixels[y * wb.PixelWidth + x] = BitConverter.ToInt32(dd, 0);
                }
            }

            return wb;
        }
        #endregion

        #region 图片亮度调整
        public static BitmapSource GetBrightnessImage(BitmapSource source, int brightness)
        {
            if (brightness < -255) brightness = -255;

            if (brightness > 255) brightness = 255;

            WriteableBitmap wb = new WriteableBitmap(source);
            for (int y = 0; y < wb.PixelHeight; y++)
            {
                for (int x = 0; x < wb.PixelWidth; x++)
                {
                    int pixel = wb.Pixels[y * wb.PixelWidth + x];
                    byte[] dd = BitConverter.GetBytes(pixel);
                    int B = (int)dd[0] + brightness;
                    int G = (int)dd[1] + brightness;
                    int R = (int)dd[2] + brightness;
                    if (B < 0) B = 0;
                    if (B > 255) B = 255;
                    if (G < 0) G = 0;
                    if (G > 255) G = 255;
                    if (R < 0) R = 0;
                    if (R > 255) R = 255;
                    dd[0] = (byte)B;
                    dd[1] = (byte)G;
                    dd[2] = (byte)R;
                    wb.Pixels[y * wb.PixelWidth + x] = BitConverter.ToInt32(dd, 0);
                }
            }
            return wb;
        }
        #endregion

        #region 图片对比度调整
        public static BitmapSource GetContrastImage(BitmapSource source, double contrast)
        {
            if (contrast < -100) contrast = -100;
            if (contrast > 100) contrast = 100;
            contrast = (100.0 + contrast) / 100.0;
            contrast *= contrast;

            WriteableBitmap wb = new WriteableBitmap(source);
            for (int y = 0; y < wb.PixelHeight; y++)
            {
                for (int x = 0; x < wb.PixelWidth; x++)
                {
                    int pixel = wb.Pixels[y * wb.PixelWidth + x];
                    byte[] dd = BitConverter.GetBytes(pixel);
                    double pR = (double)dd[2] / 255.0;
                    pR -= 0.5;
                    pR *= contrast;
                    pR += 0.5;
                    pR *= 255;
                    if (pR < 0) pR = 0;
                    if (pR > 255) pR = 255;

                    double pG = (double)dd[1] / 255.0;
                    pG -= 0.5;
                    pG *= contrast;
                    pG += 0.5;
                    pG *= 255;
                    if (pG < 0) pG = 0;
                    if (pG > 255) pG = 255;

                    double pB = (double)dd[0] / 255.0;
                    pB -= 0.5;
                    pB *= contrast;
                    pB += 0.5;
                    pB *= 255;
                    if (pB < 0) pB = 0;
                    if (pB > 255) pB = 255;

                    dd[2] = (byte)pR;
                    dd[1] = (byte)pG;
                    dd[0] = (byte)pB;

                    wb.Pixels[y * wb.PixelWidth + x] = BitConverter.ToInt32(dd, 0);
                }
            }
            return wb;
        }
        #endregion

    }
}


南京酷得软件


转载自:http://www.cnblogs.com/sucsy/archive/2013/05/25.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值