技术开发 频道

实现图片压缩、添加文字或图片水印

 【IT168技术文档】在web程序开发中,相信很多同行们都碰到过图片压缩,生成缩略图的操作,比如产品的图片,会员的照片等等功能。

 为了满足此类操作今天给大家介绍ImageUtility类,该类几乎实现了常规网站开发图片处理的功能,比如按大小生成缩略图,指定位置和大小裁剪,以前给图片添加 文字或图片水印等。此外此类生成的文件格式均为.jpg格式,如果想要.GIF或PNG的透明效果的朋友,就不要再向下看了。

 老样子,先上图:

 所有详细的介绍在代里的注释里已经标识的很清楚了,在此不在多说。

 代码:

 1using System;

 2using System.Collections.Generic;

 3using System.Text;

 4using System.Drawing;

 5using System.Drawing.Drawing2D;

 6using System.Drawing.Imaging;

 7using System.IO;

 8

 9namespace Loskiv.Utility

 10{

 11    /// <summary>

 12    /// 名称:图片处理常用操作类

 13    /// 作者:loskiv

 14    /// 创建时间:2009-07-07

 15    /// </summary>

 16    public class ImageUtility

 17    {

 18

 19        /// <summary>

 20        /// 获取指定mimeType的ImageCodecInfo

 21        /// </summary>

 22        private static ImageCodecInfo GetImageCodecInfo(string mimeType)

 23        {

 24            ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();

 25            foreach (ImageCodecInfo ici in CodecInfo)

 26            {

 27                if (ici.MimeType == mimeType) return ici;

 28            }

 29            return null;

 30        }

 31

 32        /// <summary>

 33        ///  获取inputStream中的Bitmap对象

 34        /// </summary>

 35        public static Bitmap GetBitmapFromStream(Stream inputStream)

 36        {

 37            Bitmap bitmap = new Bitmap(inputStream);

 38            return bitmap;

 39        }

 40

 41        /// <summary>

 42        /// 将Bitmap对象压缩为JPG图片类型

 43        /// </summary>

 44        /// </summary>

 45        /// <param name="bmp">源bitmap对象</param>

 46        /// <param name="saveFilePath">目标图片的存储地址</param>

 47        /// <param name="quality">压缩质量,越大照片越清晰,推荐80</param>

 48        public static void CompressAsJPG(Bitmap bmp, string saveFilePath, int quality)

 49        {

 50            EncoderParameter p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); ;

 51            EncoderParameters ps = new EncoderParameters(1);

 52            ps.Param[0] = p;

 53            bmp.Save(saveFilePath, GetImageCodecInfo("image/jpeg"), ps);

 54            bmp.Dispose();

 55        }

 56

 57        /// <summary>

 58        /// 将inputStream中的对象压缩为JPG图片类型

 59        /// </summary>

 60        /// <param name="inputStream">源Stream对象</param>

 61        /// <param name="saveFilePath">目标图片的存储地址</param>

 62        /// <param name="quality">压缩质量,越大照片越清晰,推荐80</param>

 63        public static void CompressAsJPG(Stream inputStream, string saveFilePath, int quality)

 64        {

 65            Bitmap bmp = GetBitmapFromStream(inputStream);

 66            CompressAsJPG(bmp, saveFilePath, quality);

 67        }

 68

 69

 70        /// <summary>

 71        /// 生成缩略图(JPG 格式)

 72        /// </summary>

 73        /// <param name="inputStream">包含图片的Stream</param>

 74        /// <param name="saveFilePath">目标图片的存储地址</param>

 75        /// <param name="width">缩略图的宽</param>

 76        /// <param name="height">缩略图的高</param>

 77        public static void ThumbAsJPG(Stream inputStream, string saveFilePath, int width, int height)

 78        {

 79            Image image = Image.FromStream(inputStream);

 80            if (image.Width == width && image.Height == height)

 81            {

 82                CompressAsJPG(inputStream, saveFilePath, 80);

 83            }

 84            int tWidth, tHeight, tLeft, tTop;

 85            double fScale = (double)height / (double)width;

 86            if (((double)image.Width * fScale) > (double)image.Height)

 87            {

 88                tWidth = width;

 89                tHeight = (int)((double)image.Height * (double)tWidth / (double)image.Width);

 90                tLeft = 0;

 91                tTop = (height - tHeight) / 2;

 92            }

 93            else

 94            {

 95                tHeight = height;

 96                tWidth = (int)((double)image.Width * (double)tHeight / (double)image.Height);

 97                tLeft = (width - tWidth) / 2;

 98                tTop = 0;

 99            }

 100            if (tLeft < 0) tLeft = 0;

 101            if (tTop < 0) tTop = 0;

 102

 103            Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);

 104            Graphics graphics = Graphics.FromImage(bitmap);

 105

 106            //可以在这里设置填充背景颜色

 107            graphics.Clear(Color.White);

 108            graphics.DrawImage(image, new Rectangle(tLeft, tTop, tWidth, tHeight));

 109            image.Dispose();

 110            try

 111            {

 112                CompressAsJPG(bitmap, saveFilePath, 80);

 113            }

 114            catch

 115            {

 116                ;

 117            }

 118            finally

 119            {

 120                bitmap.Dispose();

 121                graphics.Dispose();

 122            }

 123        }

 124

 125        /// <summary>

 126        /// 将Bitmap对象裁剪为指定JPG文件

 127        /// </summary>

 128        /// <param name="bmp">源bmp对象</param>

 129        /// <param name="saveFilePath">目标图片的存储地址</param>

 130        /// <param name="x">开始坐标x,单位:像素</param>

 131        /// <param name="y">开始坐标y,单位:像素</param>

 132        /// <param name="width">宽度:像素</param>

 133        /// <param name="height">高度:像素</param>

 134        public static void CutAsJPG(Bitmap bmp, string saveFilePath, int x, int y, int width, int height)

 135        {

 136            int bmpW = bmp.Width;

 137            int bmpH = bmp.Height;

 138

 139            if (x >= bmpW || y >= bmpH)

 140            {

 141                CompressAsJPG(bmp, saveFilePath, 80);

 142                return;

 143            }

 144

 145            if (x + width > bmpW)

 146            {

 147                width = bmpW - x;

 148            }

 149

 150            if (y + height > bmpH)

 151            {

 152                height = bmpH - y;

 153            }

 154

 155            Bitmap bmpOut = new Bitmap(width, height, PixelFormat.Format24bppRgb);

 156            Graphics g = Graphics.FromImage(bmpOut);

 157            g.DrawImage(bmp, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);

 158            g.Dispose();

 159            bmp.Dispose();

 160            CompressAsJPG(bmpOut, saveFilePath, 80);

 161        }

 162

 163        /// <summary>

 164        /// 将Stream中的对象裁剪为指定JPG文件

 165        /// </summary>

 166        /// <param name="inputStream">源bmp对象</param>

 167        /// <param name="saveFilePath">目标图片的存储地址</param>

 168        /// <param name="x">开始坐标x,单位:像素</param>

 169        /// <param name="y">开始坐标y,单位:像素</param>

 170        /// <param name="width">宽度:像素</param>

 171        /// <param name="height">高度:像素</param>

 172        public static void CutAsJPG(Stream inputStream, string saveFilePath, int x, int y, int width, int height)

 173        {

 174            Bitmap bmp = GetBitmapFromStream(inputStream);

 175            CutAsJPG(bmp, saveFilePath, x, y, width, height);

 176        }

 177

 178

 179        图片水印操作

 467

 468    }

 469}

 470

查看原文地址

0
相关文章