C#图像处理类(使用此类可实现生成锐化效果、黑白效果和灰度效果)
/// 锐化效果
/// </summary>
/// <param name="orgImgFilePath">源图地址</param>
/// <param name="destImgFilePath">目标图地址</param>
/// <param name="depth">锐化度</param>
public static void EffectSharpen(string orgImgFilePath, string destImgFilePath, float depth)
{
Bitmap sourcePic;
try
{
sourcePic = new Bitmap(orgImgFilePath);
}
catch (ArgumentException ex)
{
throw new ApplicationException("源文件异常,可能是不存在。", ex);
}
int w, h;
w = sourcePic.Width;
h = sourcePic.Height;
Bitmap newPic = new Bitmap(w, h);
Color color;
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
//左上方像素色值
Color colorLT = sourcePic.GetPixel(x == 0 ? x : x - 1, y == 0 ? y : y - 1);
//原图当前像素
color = sourcePic.GetPixel(x, y);
int r = (int)(color.R + depth * (color.R - colorLT.R));
if (r < 0) r = 0;
int g = (int)(color.G + depth * (color.G - colorLT.G));
if (g < 0) g = 0;
int b = (int)(color.B + depth * (color.B - colorLT.B));
if (b < 0) b = 0;
Color newColor = Color.FromArgb(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b);
newPic.SetPixel(x, y, newColor);
}
}
try
{
newPic.Save(destImgFilePath);
}
catch (ApplicationException ex)
{
throw new ApplicationException("保存缩略图异常。", ex);
}
finally
{
newPic.Dispose();
sourcePic.Dispose();
}
}
/// <summary>
/// 黑白效果
/// </summary>
/// <param name="orgImgFilePath">源图地址</param>
/// <param name="destImgFilePath">目标图地址</param>
public static void EffectBlackWhite(string orgImgFilePath, string destImgFilePath)
{
Bitmap sourcePic;
try
{
sourcePic = new Bitmap(orgImgFilePath);
}
catch (ArgumentException ex)
{
throw new ApplicationException("源文件异常,可能是不存在。", ex);
}
int w, h;
w = sourcePic.Width;
h = sourcePic.Height;
Bitmap newPic = new Bitmap(w, h);
Color color;
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
Color newColor;
color = sourcePic.GetPixel(x, y);
int v = (color.R + color.G + color.B) / 3;
if (v > 255 / 2)
newColor = Color.FromArgb(255, 255, 255);
else
newColor = Color.FromArgb(0, 0, 0);
newPic.SetPixel(x, y, newColor);
}
}
try
{
newPic.Save(destImgFilePath);
}
catch (ApplicationException ex)
{
throw new ApplicationException("保存缩略图异常。", ex);
}
finally
{
newPic.Dispose();
sourcePic.Dispose();
}
}
/// <summary>
/// 灰度效果
/// </summary>
/// <param name="orgImgFilePath">源图地址</param>
/// <param name="destImgFilePath">目标图地址</param>
public static void EffectGray(string orgImgFilePath, string destImgFilePath)
{
Bitmap sourcePic;
try
{
sourcePic = new Bitmap(orgImgFilePath);
}
catch (ArgumentException ex)
{
throw new ApplicationException("源文件异常,可能是不存在。", ex);
}
int w, h;
w = sourcePic.Width;
h = sourcePic.Height;
Bitmap newPic = new Bitmap(w, h);
Color color;
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
color = sourcePic.GetPixel(x, y);
int gray = (int)(0.299 * color.R + 0.587 * color.G + 0.114 * color.B);
//Gary = (R * 77 + G * 151 + B * 28) >> 8;
////Gray = 0.299 * R + 0.587 * G + 0.114 * B
Color newColor = Color.FromArgb(gray, gray, gray);
newPic.SetPixel(x, y, newColor);
}
}
try
{
newPic.Save(destImgFilePath);
}
catch (ApplicationException ex)
{
throw new ApplicationException("保存缩略图异常。", ex);
}
finally
{
newPic.Dispose();
sourcePic.Dispose();
}
}
}
/// </summary>
/// <param name="orgImgFilePath">源图地址</param>
/// <param name="destImgFilePath">目标图地址</param>
/// <param name="depth">锐化度</param>
public static void EffectSharpen(string orgImgFilePath, string destImgFilePath, float depth)
{
Bitmap sourcePic;
try
{
sourcePic = new Bitmap(orgImgFilePath);
}
catch (ArgumentException ex)
{
throw new ApplicationException("源文件异常,可能是不存在。", ex);
}
int w, h;
w = sourcePic.Width;
h = sourcePic.Height;
Bitmap newPic = new Bitmap(w, h);
Color color;
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
//左上方像素色值
Color colorLT = sourcePic.GetPixel(x == 0 ? x : x - 1, y == 0 ? y : y - 1);
//原图当前像素
color = sourcePic.GetPixel(x, y);
int r = (int)(color.R + depth * (color.R - colorLT.R));
if (r < 0) r = 0;
int g = (int)(color.G + depth * (color.G - colorLT.G));
if (g < 0) g = 0;
int b = (int)(color.B + depth * (color.B - colorLT.B));
if (b < 0) b = 0;
Color newColor = Color.FromArgb(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b);
newPic.SetPixel(x, y, newColor);
}
}
try
{
newPic.Save(destImgFilePath);
}
catch (ApplicationException ex)
{
throw new ApplicationException("保存缩略图异常。", ex);
}
finally
{
newPic.Dispose();
sourcePic.Dispose();
}
}
/// <summary>
/// 黑白效果
/// </summary>
/// <param name="orgImgFilePath">源图地址</param>
/// <param name="destImgFilePath">目标图地址</param>
public static void EffectBlackWhite(string orgImgFilePath, string destImgFilePath)
{
Bitmap sourcePic;
try
{
sourcePic = new Bitmap(orgImgFilePath);
}
catch (ArgumentException ex)
{
throw new ApplicationException("源文件异常,可能是不存在。", ex);
}
int w, h;
w = sourcePic.Width;
h = sourcePic.Height;
Bitmap newPic = new Bitmap(w, h);
Color color;
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
Color newColor;
color = sourcePic.GetPixel(x, y);
int v = (color.R + color.G + color.B) / 3;
if (v > 255 / 2)
newColor = Color.FromArgb(255, 255, 255);
else
newColor = Color.FromArgb(0, 0, 0);
newPic.SetPixel(x, y, newColor);
}
}
try
{
newPic.Save(destImgFilePath);
}
catch (ApplicationException ex)
{
throw new ApplicationException("保存缩略图异常。", ex);
}
finally
{
newPic.Dispose();
sourcePic.Dispose();
}
}
/// <summary>
/// 灰度效果
/// </summary>
/// <param name="orgImgFilePath">源图地址</param>
/// <param name="destImgFilePath">目标图地址</param>
public static void EffectGray(string orgImgFilePath, string destImgFilePath)
{
Bitmap sourcePic;
try
{
sourcePic = new Bitmap(orgImgFilePath);
}
catch (ArgumentException ex)
{
throw new ApplicationException("源文件异常,可能是不存在。", ex);
}
int w, h;
w = sourcePic.Width;
h = sourcePic.Height;
Bitmap newPic = new Bitmap(w, h);
Color color;
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
color = sourcePic.GetPixel(x, y);
int gray = (int)(0.299 * color.R + 0.587 * color.G + 0.114 * color.B);
//Gary = (R * 77 + G * 151 + B * 28) >> 8;
////Gray = 0.299 * R + 0.587 * G + 0.114 * B
Color newColor = Color.FromArgb(gray, gray, gray);
newPic.SetPixel(x, y, newColor);
}
}
try
{
newPic.Save(destImgFilePath);
}
catch (ApplicationException ex)
{
throw new ApplicationException("保存缩略图异常。", ex);
}
finally
{
newPic.Dispose();
sourcePic.Dispose();
}
}
}