///
<summary>
/// 图像处理类,需引用System.IO和System.Drawing
/// </summary>
public static class PicProcesser
{
/// <summary>
/// 在指定位置生成,保存缩略图
/// </summary>
/// <param name="orgImgFilePath"> 原图地址 </param>
/// <param name="smallFilePath"> 目标图地址 </param>
/// <param name="width"> 缩略图宽 </param>
/// <param name="height"> 缩略图高 </param>
/// <param name="oWidth"> 原图宽 </param>
/// <param name="oHeight"> 原图高 </param>
/// <exception cref="ApplicationException"> 处理时产生的异常:源文件不存在,或无权限 </exception>
public static void GetThumbnailImage( string orgImgFilePath, string smallFilePath, int width, int height, out int oWidth, out int oHeight)
{
Bitmap sourcePic;
try
{
sourcePic = new Bitmap(orgImgFilePath);
}
catch (ArgumentException ex)
{
throw new ApplicationException( " 源文件异常,可能是不存在。 " , ex);
}
oWidth = sourcePic.Width;
oHeight = sourcePic.Height;
int newWidth = oWidth;
int newHeight = oHeight;
float percent;
if (oWidth > width)
{
newWidth = width;
percent = float .Parse(newWidth.ToString()) / oWidth;
newHeight = ( int )(oHeight * percent);
}
else if (oHeight > height)
{
newHeight = height;
percent = float .Parse(newHeight.ToString()) / oHeight;
newWidth = ( int )(newWidth * percent);
}
Bitmap newPic = new Bitmap(newWidth, newHeight);
Graphics newGraphics = Graphics.FromImage(newPic);
newGraphics.DrawImage(sourcePic, 0 , 0 , newWidth, newHeight);
try
{
newPic.Save(smallFilePath);
}
catch (ApplicationException ex)
{
throw new ApplicationException( " 保存缩略图异常。 " , ex);
}
finally
{
newGraphics.Dispose();
newPic.Dispose();
sourcePic.Dispose();
}
}
/// <summary>
/// 漫射效果
/// </summary>
/// <param name="orgImgFilePath"> 源图地址 </param>
/// <param name="destImgFilePath"> 目标图地址 </param>
/// <param name="stride"> 图片扫描宽度(0为自动获取) </param>
public static void EffectDiffuse( string orgImgFilePath, string destImgFilePath, int stride)
{
Bitmap sourcePic;
try
{
sourcePic = new Bitmap(orgImgFilePath);
}
catch (ArgumentException ex)
{
throw new ApplicationException( " 源文件异常,可能是不存在。 " , ex);
}
int w, h;
w = sourcePic.Width;
h = sourcePic.Height;
Random random = new Random();
Bitmap newPic = new Bitmap(w, h);
if (stride == 0 )
{
// lock in memeory
Rectangle rect = new Rectangle( 0 , 0 , sourcePic.Width, sourcePic.Height);
System.Drawing.Imaging.BitmapData bmpData = sourcePic.LockBits(rect,
System.Drawing.Imaging.ImageLockMode.ReadWrite,
sourcePic.PixelFormat);
stride = bmpData.Stride;
// unlock
sourcePic.UnlockBits(bmpData);
}
for ( int x = 0 ; x < w; x ++ )
{
for ( int y = 0 ; y < h; y ++ )
{
int newX;
int newY;
newX = x + ( int )(random.NextDouble() * stride);
newY = y + ( int )(random.NextDouble() * stride);
newPic.SetPixel(x, y, sourcePic.GetPixel(newX >= w ? w - 1 : newX, newY >= h ? h - 1 : newY));
}
}
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 EffectEpia( 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 r = ( int )( 0.393 * color.R + 0.769 * color.G + 0.189 * color.B);
int g = ( int )( 0.349 * color.R + 0.686 * color.G + 0.168 * color.B);
int b = ( int )( 0.272 * color.R + 0.534 * color.G + 0.131 * color.B);
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>
/// <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();
}
}
}
/// 图像处理类,需引用System.IO和System.Drawing
/// </summary>
public static class PicProcesser
{
/// <summary>
/// 在指定位置生成,保存缩略图
/// </summary>
/// <param name="orgImgFilePath"> 原图地址 </param>
/// <param name="smallFilePath"> 目标图地址 </param>
/// <param name="width"> 缩略图宽 </param>
/// <param name="height"> 缩略图高 </param>
/// <param name="oWidth"> 原图宽 </param>
/// <param name="oHeight"> 原图高 </param>
/// <exception cref="ApplicationException"> 处理时产生的异常:源文件不存在,或无权限 </exception>
public static void GetThumbnailImage( string orgImgFilePath, string smallFilePath, int width, int height, out int oWidth, out int oHeight)
{
Bitmap sourcePic;
try
{
sourcePic = new Bitmap(orgImgFilePath);
}
catch (ArgumentException ex)
{
throw new ApplicationException( " 源文件异常,可能是不存在。 " , ex);
}
oWidth = sourcePic.Width;
oHeight = sourcePic.Height;
int newWidth = oWidth;
int newHeight = oHeight;
float percent;
if (oWidth > width)
{
newWidth = width;
percent = float .Parse(newWidth.ToString()) / oWidth;
newHeight = ( int )(oHeight * percent);
}
else if (oHeight > height)
{
newHeight = height;
percent = float .Parse(newHeight.ToString()) / oHeight;
newWidth = ( int )(newWidth * percent);
}
Bitmap newPic = new Bitmap(newWidth, newHeight);
Graphics newGraphics = Graphics.FromImage(newPic);
newGraphics.DrawImage(sourcePic, 0 , 0 , newWidth, newHeight);
try
{
newPic.Save(smallFilePath);
}
catch (ApplicationException ex)
{
throw new ApplicationException( " 保存缩略图异常。 " , ex);
}
finally
{
newGraphics.Dispose();
newPic.Dispose();
sourcePic.Dispose();
}
}
/// <summary>
/// 漫射效果
/// </summary>
/// <param name="orgImgFilePath"> 源图地址 </param>
/// <param name="destImgFilePath"> 目标图地址 </param>
/// <param name="stride"> 图片扫描宽度(0为自动获取) </param>
public static void EffectDiffuse( string orgImgFilePath, string destImgFilePath, int stride)
{
Bitmap sourcePic;
try
{
sourcePic = new Bitmap(orgImgFilePath);
}
catch (ArgumentException ex)
{
throw new ApplicationException( " 源文件异常,可能是不存在。 " , ex);
}
int w, h;
w = sourcePic.Width;
h = sourcePic.Height;
Random random = new Random();
Bitmap newPic = new Bitmap(w, h);
if (stride == 0 )
{
// lock in memeory
Rectangle rect = new Rectangle( 0 , 0 , sourcePic.Width, sourcePic.Height);
System.Drawing.Imaging.BitmapData bmpData = sourcePic.LockBits(rect,
System.Drawing.Imaging.ImageLockMode.ReadWrite,
sourcePic.PixelFormat);
stride = bmpData.Stride;
// unlock
sourcePic.UnlockBits(bmpData);
}
for ( int x = 0 ; x < w; x ++ )
{
for ( int y = 0 ; y < h; y ++ )
{
int newX;
int newY;
newX = x + ( int )(random.NextDouble() * stride);
newY = y + ( int )(random.NextDouble() * stride);
newPic.SetPixel(x, y, sourcePic.GetPixel(newX >= w ? w - 1 : newX, newY >= h ? h - 1 : newY));
}
}
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 EffectEpia( 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 r = ( int )( 0.393 * color.R + 0.769 * color.G + 0.189 * color.B);
int g = ( int )( 0.349 * color.R + 0.686 * color.G + 0.168 * color.B);
int b = ( int )( 0.272 * color.R + 0.534 * color.G + 0.131 * color.B);
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>
/// <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();
}
}
}