zoukankan      html  css  js  c++  java
  • Silverlight 图片灰化、亮度、对比度调整 南京酷得软件

    using System;
    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.codersoft.cn 专业开发: 气象软件、监狱网上购物系统、两法衔接平台
  • 相关阅读:
    阿里巴巴2018秋招面经之前端岗(1~5面)
    面试分享:2018阿里巴巴前端面试总结(题目+答案 30题)
    2018年各大互联网前端面试题三(阿里)
    阿里巴巴2016前端工程师面试题
    2016 阿里校招笔试前端题目,你还记得吗?
    2018阿里前端校招分享(笔试篇)
    2017 阿里校招前端笔试题小结
    阿里2018校招编程题
    ko.js学习一
    简单的3d变换
  • 原文地址:https://www.cnblogs.com/sucsy/p/3099258.html
Copyright © 2011-2022 走看看