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
}
}
南京酷得软件