首页 国内 国际 娱乐 体育 财经 科技 留学 健康 论坛 空间 汽车 文学 军事 爱墙 游戏 艺术 星座
短信 明星 数码 手机 美食 家居 股票 教育 公益 公益 旅游 彩票 商城 校园 奇闻 女性 房产 时尚

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(255255255);

                
else

                    newColor 
= Color.FromArgb(000);

                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();

        }

    }

}

posted @ 2009-02-27 16:09  程序执着追随者  阅读(2092)  评论(0编辑  收藏  举报
友情链接
你的位置
你的位置
你的位置
你的位置
你的位置
你的位置
你的位置
你的位置
你的位置
你的位置
你的位置
你的位置
你的位置
你的位置