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

        }

    }

}

转载于:https://www.cnblogs.com/zhjzwl/archive/2009/02/27/1399634.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值