ImageClass

using System;
using System.Collections;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

public class ImageClass
{
    public ImageClass()
    { }

#region 缩略图
    /// <summary>
    /// 生成缩略图
    /// </summary>
    /// <param name="originalImagePath">源图路径(物理路径)</param>
    /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
    /// <param name="width">缩略图宽度</param>
    /// <param name="height">缩略图高度</param>
    /// <param name="mode">生成缩略图的方式</param>   
    public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
    {
        System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

int towidth = width;
        int toheight = height;

int x = 0;
        int y = 0;
        int ow = originalImage.Width;
        int oh = originalImage.Height;

switch (mode)
        {
            case "HW":  //指定高宽缩放(可能变形)               
                break;
            case "W":   //指定宽,高按比例                   
                toheight = originalImage.Height * width / originalImage.Width;
                break;
            case "H":   //指定高,宽按比例
                towidth = originalImage.Width * height / originalImage.Height;
                break;
            case "Cut": //指定高宽裁减(不变形)               
                if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                {
                    oh = originalImage.Height;
                    ow = originalImage.Height * towidth / toheight;
                    y = 0;
                    x = (originalImage.Width - ow) / 2;
                }
                else
                {
                    ow = originalImage.Width;
                    oh = originalImage.Width * height / towidth;
                    x = 0;
                    y = (originalImage.Height - oh) / 2;
                }
                break;
            default:
                break;
        }

//新建一个bmp图片
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

//新建一个画板
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

//设置高质量插值法
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

//设置高质量,低速度呈现平滑程度
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//清空画布并以透明背景色填充
        g.Clear(System.Drawing.Color.Transparent);

//在指定位置并且按指定大小绘制原图片的指定部分
        g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);

try
        {
            //以jpg格式保存缩略图
            bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (System.Exception e)
        {
            throw e;
        }
        finally
        {
            originalImage.Dispose();
            bitmap.Dispose();
            g.Dispose();
        }
    }
    #endregion

#region 图片水印
    /// <summary>
    /// 图片水印处理方法
    /// </summary>
    /// <param name="path">需要加载水印的图片路径(绝对路径)</param>
    /// <param name="waterpath">水印图片(绝对路径)</param>
    /// <param name="location">水印位置(传送正确的代码)</param>
    public static string ImageWatermark(string path, string waterpath, string location)
    {
        string kz_name = Path.GetExtension(path);
        if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg")
        {
            DateTime time = DateTime.Now;
            string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString();
            Image img = Bitmap.FromFile(path);
            Image waterimg = Image.FromFile(waterpath);
            Graphics g = Graphics.FromImage(img);
            ArrayList loca = GetLocation(location, img, waterimg);
            g.DrawImage(waterimg, new Rectangle(int.Parse(loca[0].ToString()), int.Parse(loca[1].ToString()), waterimg.Width, waterimg.Height));
            waterimg.Dispose();
            g.Dispose();
            string newpath = Path.GetDirectoryName(path) + filename + kz_name;
            img.Save(newpath);
            img.Dispose();
            File.Copy(newpath, path, true);
            if (File.Exists(newpath))
            {
                File.Delete(newpath);
            }
        }
        return path;
    }

/// <summary>
    /// 图片水印位置处理方法
    /// </summary>
    /// <param name="location">水印位置</param>
    /// <param name="img">需要添加水印的图片</param>
    /// <param name="waterimg">水印图片</param>
    private static ArrayList GetLocation(string location, Image img, Image waterimg)
    {
        ArrayList loca = new ArrayList();
        int x = 0;
        int y = 0;

if (location == "LT")
        {
            x = 10;
            y = 10;
        }
        else if (location == "T")
        {
            x = img.Width / 2 - waterimg.Width / 2;
            y = img.Height - waterimg.Height;
        }
        else if (location == "RT")
        {
            x = img.Width - waterimg.Width;
            y = 10;
        }
        else if (location == "LC")
        {
            x = 10;
            y = img.Height / 2 - waterimg.Height / 2;
        }
        else if (location == "C")
        {
            x = img.Width / 2 - waterimg.Width / 2;
            y = img.Height / 2 - waterimg.Height / 2;
        }
        else if (location == "RC")
        {
            x = img.Width - waterimg.Width;
            y = img.Height / 2 - waterimg.Height / 2;
        }
        else if (location == "LB")
        {
            x = 10;
            y = img.Height - waterimg.Height;
        }
        else if (location == "B")
        {
            x = img.Width / 2 - waterimg.Width / 2;
            y = img.Height - waterimg.Height;
        }
        else
        {
            x = img.Width - waterimg.Width;
            y = img.Height - waterimg.Height;
        }
        loca.Add(x);
        loca.Add(y);
        return loca;
    }
    #endregion

#region 文字水印
    /// <summary>
    /// 文字水印处理方法
    /// </summary>
    /// <param name="path">图片路径(绝对路径)</param>
    /// <param name="size">字体大小</param>
    /// <param name="letter">水印文字</param>
    /// <param name="color">颜色</param>
    /// <param name="location">水印位置</param>
    public static string LetterWatermark(string path, int size, string letter, Color color, string location)
    {
        #region

string kz_name = Path.GetExtension(path);
        if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg")
        {
            DateTime time = DateTime.Now;
            string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString();
            Image img = Bitmap.FromFile(path);
            Graphics gs = Graphics.FromImage(img);
            ArrayList loca = GetLocation(location, img, size, letter.Length);
            Font font = new Font("宋体", size);
            Brush br = new SolidBrush(color);
            gs.DrawString(letter, font, br, float.Parse(loca[0].ToString()), float.Parse(loca[1].ToString()));
            gs.Dispose();
            string newpath = Path.GetDirectoryName(path) + filename + kz_name;
            img.Save(newpath);
            img.Dispose();
            File.Copy(newpath, path, true);
            if (File.Exists(newpath))
            {
                File.Delete(newpath);
            }
        }
        return path;

#endregion
    }

/// <summary>
    /// 文字水印位置的方法
    /// </summary>
    /// <param name="location">位置代码</param>
    /// <param name="img">图片对象</param>
    /// <param name="width">宽(当水印类型为文字时,传过来的就是字体的大小)</param>
    /// <param name="height">高(当水印类型为文字时,传过来的就是字符的长度)</param>
    private static ArrayList GetLocation(string location, Image img, int width, int height)
    {
        #region

ArrayList loca = new ArrayList();  //定义数组存储位置
        float x = 10;
        float y = 10;

if (location == "LT")
        {
            loca.Add(x);
            loca.Add(y);
        }
        else if (location == "T")
        {
            x = img.Width / 2 - (width * height) / 2;
            loca.Add(x);
            loca.Add(y);
        }
        else if (location == "RT")
        {
            x = img.Width - width * height;
        }
        else if (location == "LC")
        {
            y = img.Height / 2;
        }
        else if (location == "C")
        {
            x = img.Width / 2 - (width * height) / 2;
            y = img.Height / 2;
        }
        else if (location == "RC")
        {
            x = img.Width - height;
            y = img.Height / 2;
        }
        else if (location == "LB")
        {
            y = img.Height - width - 5;
        }
        else if (location == "B")
        {
            x = img.Width / 2 - (width * height) / 2;
            y = img.Height - width - 5;
        }
        else
        {
            x = img.Width - width * height;
            y = img.Height - width - 5;
        }
        loca.Add(x);
        loca.Add(y);
        return loca;

#endregion
    }
    #endregion

#region 调整光暗
    /// <summary>
    /// 调整光暗
    /// </summary>
    /// <param name="mybm">原始图片</param>
    /// <param name="width">原始图片的长度</param>
    /// <param name="height">原始图片的高度</param>
    /// <param name="val">增加或减少的光暗值</param>
    public Bitmap LDPic(Bitmap mybm, int width, int height, int val)
    {
        Bitmap bm = new Bitmap(width, height);//初始化一个记录经过处理后的图片对象
        int x, y, resultR, resultG, resultB;//x、y是循环次数,后面三个是记录红绿蓝三个值的
        Color pixel;
        for (x = 0; x < width; x++)
        {
            for (y = 0; y < height; y++)
            {
                pixel = mybm.GetPixel(x, y);//获取当前像素的值
                resultR = pixel.R + val;//检查红色值会不会超出[0, 255]
                resultG = pixel.G + val;//检查绿色值会不会超出[0, 255]
                resultB = pixel.B + val;//检查蓝色值会不会超出[0, 255]
                bm.SetPixel(x, y, Color.FromArgb(resultR, resultG, resultB));//绘图
            }
        }
        return bm;
    }
    #endregion

#region 反色处理
    /// <summary>
    /// 反色处理
    /// </summary>
    /// <param name="mybm">原始图片</param>
    /// <param name="width">原始图片的长度</param>
    /// <param name="height">原始图片的高度</param>
    public Bitmap RePic(Bitmap mybm, int width, int height)
    {
        Bitmap bm = new Bitmap(width, height);//初始化一个记录处理后的图片的对象
        int x, y, resultR, resultG, resultB;
        Color pixel;
        for (x = 0; x < width; x++)
        {
            for (y = 0; y < height; y++)
            {
                pixel = mybm.GetPixel(x, y);//获取当前坐标的像素值
                resultR = 255 - pixel.R;//反红
                resultG = 255 - pixel.G;//反绿
                resultB = 255 - pixel.B;//反蓝
                bm.SetPixel(x, y, Color.FromArgb(resultR, resultG, resultB));//绘图
            }
        }
        return bm;
    }
    #endregion

#region 浮雕处理
    /// <summary>
    /// 浮雕处理
    /// </summary>
    /// <param name="oldBitmap">原始图片</param>
    /// <param name="Width">原始图片的长度</param>
    /// <param name="Height">原始图片的高度</param>
    public Bitmap FD(Bitmap oldBitmap, int Width, int Height)
    {
        Bitmap newBitmap = new Bitmap(Width, Height);
        Color color1, color2;
        for (int x = 0; x < Width - 1; x++)
        {
            for (int y = 0; y < Height - 1; y++)
            {
                int r = 0, g = 0, b = 0;
                color1 = oldBitmap.GetPixel(x, y);
                color2 = oldBitmap.GetPixel(x + 1, y + 1);
                r = Math.Abs(color1.R - color2.R + 128);
                g = Math.Abs(color1.G - color2.G + 128);
                b = Math.Abs(color1.B - color2.B + 128);
                if (r > 255) r = 255;
                if (r < 0) r = 0;
                if (g > 255) g = 255;
                if (g < 0) g = 0;
                if (b > 255) b = 255;
                if (b < 0) b = 0;
                newBitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
            }
        }
        return newBitmap;
    }
    #endregion

#region 拉伸图片
    /// <summary>
    /// 拉伸图片
    /// </summary>
    /// <param name="bmp">原始图片</param>
    /// <param name="newW">新的宽度</param>
    /// <param name="newH">新的高度</param>
    public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH)
    {
        try
        {
            Bitmap bap = new Bitmap(newW, newH);
            Graphics g = Graphics.FromImage(bap);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(bap, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bap.Width, bap.Height), GraphicsUnit.Pixel);
            g.Dispose();
            return bap;
        }
        catch
        {
            return null;
        }
    }
    #endregion

#region 滤色处理
    /// <summary>
    /// 滤色处理
    /// </summary>
    /// <param name="mybm">原始图片</param>
    /// <param name="width">原始图片的长度</param>
    /// <param name="height">原始图片的高度</param>
    public Bitmap FilPic(Bitmap mybm, int width, int height)
    {
        Bitmap bm = new Bitmap(width, height);//初始化一个记录滤色效果的图片对象
        int x, y;
        Color pixel;

for (x = 0; x < width; x++)
        {
            for (y = 0; y < height; y++)
            {
                pixel = mybm.GetPixel(x, y);//获取当前坐标的像素值
                bm.SetPixel(x, y, Color.FromArgb(0, pixel.G, pixel.B));//绘图
            }
        }
        return bm;
    }
    #endregion

#region 左右翻转
    /// <summary>
    /// 左右翻转
    /// </summary>
    /// <param name="mybm">原始图片</param>
    /// <param name="width">原始图片的长度</param>
    /// <param name="height">原始图片的高度</param>
    public Bitmap RevPicLR(Bitmap mybm, int width, int height)
    {
        Bitmap bm = new Bitmap(width, height);
        int x, y, z; //x,y是循环次数,z是用来记录像素点的x坐标的变化的
        Color pixel;
        for (y = height - 1; y >= 0; y--)
        {
            for (x = width - 1, z = 0; x >= 0; x--)
            {
                pixel = mybm.GetPixel(x, y);//获取当前像素的值
                bm.SetPixel(z++, y, Color.FromArgb(pixel.R, pixel.G, pixel.B));//绘图
            }
        }
        return bm;
    }
    #endregion

#region 上下翻转
    /// <summary>
    /// 上下翻转
    /// </summary>
    /// <param name="mybm">原始图片</param>
    /// <param name="width">原始图片的长度</param>
    /// <param name="height">原始图片的高度</param>
    public Bitmap RevPicUD(Bitmap mybm, int width, int height)
    {
        Bitmap bm = new Bitmap(width, height);
        int x, y, z;
        Color pixel;
        for (x = 0; x < width; x++)
        {
            for (y = height - 1, z = 0; y >= 0; y--)
            {
                pixel = mybm.GetPixel(x, y);//获取当前像素的值
                bm.SetPixel(x, z++, Color.FromArgb(pixel.R, pixel.G, pixel.B));//绘图
            }
        }
        return bm;
    }
    #endregion

#region 压缩图片
    /// <summary>
    /// 压缩到指定尺寸
    /// </summary>
    /// <param name="oldfile">原文件</param>
    /// <param name="newfile">新文件</param>
    public bool Compress(string oldfile, string newfile)
    {
        try
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(oldfile);
            System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
            Size newSize = new Size(100, 125);
            Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
            Graphics g = Graphics.FromImage(outBmp);
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
            g.Dispose();
            EncoderParameters encoderParams = new EncoderParameters();
            long[] quality = new long[1];
            quality[0] = 100;
            EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
            encoderParams.Param[0] = encoderParam;
            ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo jpegICI = null;
            for (int x = 0; x < arrayICI.Length; x++)
                if (arrayICI[x].FormatDescription.Equals("JPEG"))
                {
                    jpegICI = arrayICI[x]; //设置JPEG编码
                    break;
                }
            img.Dispose();
            if (jpegICI != null) outBmp.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);
            outBmp.Dispose();
            return true;
        }
        catch
        {
            return false;
        }
    }
    #endregion

#region 图片灰度化
    public Color Gray(Color c)
    {
        int rgb = Convert.ToInt32((double)(((0.3 * c.R) + (0.59 * c.G)) + (0.11 * c.B)));
        return Color.FromArgb(rgb, rgb, rgb);
    }
    #endregion

#region 转换为黑白图片
    /// <summary>
    /// 转换为黑白图片
    /// </summary>
    /// <param name="mybt">要进行处理的图片</param>
    /// <param name="width">图片的长度</param>
    /// <param name="height">图片的高度</param>
    public Bitmap BWPic(Bitmap mybm, int width, int height)
    {
        Bitmap bm = new Bitmap(width, height);
        int x, y, result; //x,y是循环次数,result是记录处理后的像素值
        Color pixel;
        for (x = 0; x < width; x++)
        {
            for (y = 0; y < height; y++)
            {
                pixel = mybm.GetPixel(x, y);//获取当前坐标的像素值
                result = (pixel.R + pixel.G + pixel.B) / 3;//取红绿蓝三色的平均值
                bm.SetPixel(x, y, Color.FromArgb(result, result, result));
            }
        }
        return bm;
    }
    #endregion

#region 获取图片中的各帧
    /// <summary>
    /// 获取图片中的各帧
    /// </summary>
    /// <param name="pPath">图片路径</param>
    /// <param name="pSavePath">保存路径</param>
    public void GetFrames(string pPath, string pSavedPath)
    {
        Image gif = Image.FromFile(pPath);
        FrameDimension fd = new FrameDimension(gif.FrameDimensionsList[0]);
        int count = gif.GetFrameCount(fd); //获取帧数(gif图片可能包含多帧,其它格式图片一般仅一帧)
        for (int i = 0; i < count; i++)    //以Jpeg格式保存各帧
        {
            gif.SelectActiveFrame(fd, i);
            gif.Save(pSavedPath + "\\frame_" + i + ".jpg", ImageFormat.Jpeg);
        }
    }
    #endregion
}

时间: 2024-11-07 17:21:51

ImageClass的相关文章

微信应用号开发教程

——第一篇 微信应用号(小程序,「应用号」的新称呼)终于来了! 目前还处于内测阶段,微信只邀请了部分企业参与封测.想必大家都关心应用号的最终形态到底是什么样子?怎样将一个「服务号」改造成为「小程序」? 我们暂时以一款简单的第三方工具的实例,来演示一下开发过程吧.(公司的项目保密还不能分享代码和截图.小白是边加班边偷偷给大家写教程.感谢「名片盒」团队提供他们的服务号来动这个手术,所以小白的教程就用「名片盒」的公众号滚动更新发布吧??) OK,为了让大家尽快看到这份教程,小白注定要熬夜了!今晚开始更

C#放缩、截取、合并图片并生成高质量新图的类

using System;using System.Drawing;using System.Drawing.Imaging;using System.Drawing.Drawing2D;using System.IO; namespace Framework{public class ImageClass{        public Image ResourceImage;        public int Width=0;        public int Height=0;     

Node.js 切近实战(八) 之Excel在线(文件权限)

最近美国又他妈的皮痒了,在南海找事,还说什么中国必须接受南海仲裁结果,我去你大爷的,你以为你是谁啊.说实话只要我们要决一死战的勇气,还管什么华盛顿航母,佛吉尼亚潜艇,大不了大家一起死,不,全世界一起死.怎么个死法,中国惹急了先给俄罗斯来几颗核弹,然后俄罗斯反击中国的同时,也会给欧洲扔几颗核弹,给美国扔很多核弹,然后欧洲英法会给其他国家扔核弹,美国给世界扔核弹,俄罗斯只给北冰洋扔就行了,中国给美国和太平洋扔就行了,这样世界就不复存在了. 今天我们来看一下文件权限管理,这个其实是对共享出去的文件的一

Node.js 切近实战(七) 之Excel在线(文件&文件组)

最近西安的天气真他妈的热,感觉还是青海的天气美,最高温28度.上周逛了青海湖,感觉还是意犹未尽,其实我还是很喜欢去一趟西藏的,但是考虑到花费也没人陪我,我暂时放弃这个念头.计划去一下重庆或者甘南,也许是现实的. OK,废话不多说,今天我们来看一下Excel在线部分的文件和文件组.首先我们来看一下页面,调一下胃口.俗话说无图无真相,先看图. 没错,还是Telerik Kendo UI,其实我面试的时候当听到别人说自己用的是EasyUI和ExtJs的时候,我就不那么上心,但是如果有人用的是Kendo

上传图片回显,头像截取 SWFload areaSelect插件

1 @{ 2 ViewBag.Title = "Index"; 3 Layout = "~/Views/Shared/_LayoutPage.cshtml"; 4 } 5 6 @section head{ 7 8 <script src="~/Content/SWFUpload/swfupload.js"></script> 9 <script src="~/Content/SWFUpload/handle

图片处理类

public class ImageClass { public ImageClass() { } #region 缩略图 /// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</p

js实现图片文件上传的心得

今天研究一下图片上存的一个实现方法,由于上周没写技术周记,这次一定要写好! 那么问题来了:PHP处理图片上传时要求JQ实现数据的交互,怎么办? 这里涉及到的一个logo上传的需求,根据radio选择是否更新logo,同时form表单将以js事件处理 <div class="comLogo sbox"> <div class="CLheader">公司logo:</div> <div class="ui-imgsel

Node.js 切近实战(二) 之图书管理系统(图书查询)

最近又当上了Master,负责带项目,有时候,遇到的问题我很郁闷.比如一个Story,需求中说的是将单个修改改为批量修改,举个例子,商品信息修改,之前是用一个商品id修改,但是现在改成多个商品id修改.我的意思是直接将文本框宽度高度加大,支持回车换行就行了,然后再将API修改为支持批量查询.这个界面上上面是一个Grid,下面是一个表单,选择Grid的数据后,会加载到下面表单.只能加载一条下去,就因为这个,有人提出如果加载一个下去,那么大个文本框只显示一个选中的商品id,视觉上无法接受.说是要将选

C# 生成图片缩略图

最近项目有部分需求,是关于图片操作部分的, 大致的功能就是图片的保存和展示.但是直接操作原图,程序运行效率太慢.而且如果传输数据量过大的话,可能直接导致调用WCF服务失败的问题. 为了解决这个问题,决定采用缩略图的方法.保存数据的时候保存原图和其缩略图.但主界面展示的时候只加载缩略图,点击缩略图后再显示其原图.这样就避免了一进入主界面就加载大量数据,提高了效率. 那么问题就来了,怎么将原图进行处理变成缩略图呢? 下面提供了一个图片处理的类,用来生成缩略图.对GetReducedImage函数进行