首页  ·  知识 ·  编程语言
C#图像处理类
网友  博客园 http://www.cnblogs.com/zhjzwl/  .NET  编辑:德仔   图片来源:网络
/// 锐化效果 /// lt;/summarygt; nb
 /// 锐化效果
    /// </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();
        }
 
本文作者:网友 来源:博客园 http://www.cnblogs.com/zhjzwl/
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读