图片管理类ImgCmdUtils

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Web;

namespace Common.Utilities
{
    /// <summary>
        /// 图片管理类
        /// </summary>
    public class ImgCmdUtils
    {
        #region 等比缩放



        /// <summary>  

        /// 图片等比缩放  

        /// </summary>  

        /// <remarks>吴剑 2011-01-21</remarks>  

        /// <param name="postedFile">原图HttpPostedFile对象</param>  

        /// <param name="savePath">缩略图存放地址</param>  

        /// <param name="targetWidth">指定的最大宽度</param>  

        /// <param name="targetHeight">指定的最大高度</param>  

        /// <param name="watermarkText">水印文字(为""表示不使用水印)</param>  

        /// <param name="watermarkImage">水印图片路径(为""表示不使用水印)</param>  

        public static void ZoomAuto(HttpPostedFileBase postedFile, string savePath, System.Double targetWidth, System.Double targetHeight, string watermarkText, string watermarkImage)
        {

            //创建目录  



            //原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息)  

            System.Drawing.Image initImage = System.Drawing.Image.FromStream(postedFile.InputStream, true);



            //原图宽高均小于模版,不作处理,直接保存  

            if (initImage.Width <= targetWidth && initImage.Height <= targetHeight)
            {

                //文字水印  

                if (watermarkText != "")
                {

                    using (System.Drawing.Graphics gWater = System.Drawing.Graphics.FromImage(initImage))
                    {

                        System.Drawing.Font fontWater = new Font("黑体", 30);

                        System.Drawing.Brush brushWater = new SolidBrush(Color.Red);

                        gWater.DrawString(watermarkText, fontWater, brushWater, 10, 10);

                        gWater.Dispose();

                    }

                }



                //透明图片水印  

                if (watermarkImage != "")
                {

                    if (File.Exists(watermarkImage))
                    {

                        //获取水印图片  

                        using (System.Drawing.Image wrImage = System.Drawing.Image.FromFile(watermarkImage))
                        {

                            //水印绘制条件:原始图片宽高均大于或等于水印图片  

                            if (initImage.Width >= wrImage.Width && initImage.Height >= wrImage.Height)
                            {

                                Graphics gWater = Graphics.FromImage(initImage);



                                //透明属性  

                                ImageAttributes imgAttributes = new ImageAttributes();

                                ColorMap colorMap = new ColorMap();

                                colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);

                                colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);

                                ColorMap[] remapTable = { colorMap };

                                imgAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);



                                float[][] colorMatrixElements = {   

                                   new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},  

                                   new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},  

                                   new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},  

                                   new float[] {0.0f,  0.0f,  0.0f,  0.8f, 0.0f},//透明度:0.5  

                                   new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}  

                                };



                                ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);

                                imgAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

                                gWater.DrawImage(wrImage,new Rectangle(initImage.Width, initImage.Height/2 , wrImage.Width, wrImage.Height/2), 0, 0, wrImage.Width, wrImage.Height, GraphicsUnit.Pixel, imgAttributes);



                                gWater.Dispose();

                            }

                            wrImage.Dispose();

                        }

                    }

                }



                //保存  

                initImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);

            }

            else
            {

                //缩略图宽、高计算  

                double newWidth = initImage.Width;

                double newHeight = initImage.Height;



                //宽大于高或宽等于高(横图或正方)  
                if (initImage.Width >= targetWidth && initImage.Width >= targetHeight)
                {

                    //如果宽大于模版  

                   

                        //宽按模版,高按比例缩放  

                        newWidth = targetWidth;

                        newHeight = targetHeight;

                }else if (initImage.Width > initImage.Height || initImage.Width == initImage.Height)
                {

                    //如果宽大于模版  

                    if (initImage.Width > targetWidth)
                    {

                        //宽按模版,高按比例缩放  

                        newWidth = targetWidth;

                        newHeight = initImage.Height;

                    }

                }

                //高大于宽(竖图)  

                else
                {

                    //如果高大于模版  

                    if (initImage.Height > targetHeight)
                    {

                        //高按模版,宽按比例缩放  

                        newHeight = targetHeight;

                        newWidth = initImage.Width  ;

                    }

                }



                //生成新图  

                //新建一个bmp图片  

                System.Drawing.Image newImage = new System.Drawing.Bitmap((int)newWidth, (int)newHeight);

                //新建一个画板  

                System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage);



                //设置质量  

                newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;



                //置背景色  

                newG.Clear(Color.White);

                //画图  

                newG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, newImage.Width, newImage.Height), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel);



                //文字水印  

                if (watermarkText != "")
                {

                    using (System.Drawing.Graphics gWater = System.Drawing.Graphics.FromImage(newImage))
                    {

                        System.Drawing.Font fontWater = new Font("宋体", 30);

                        System.Drawing.Brush brushWater = new SolidBrush(Color.Red);

                        gWater.DrawString(watermarkText, fontWater, brushWater, 10,10);

                        gWater.Dispose();

                    }

                }



                //透明图片水印  

                if (watermarkImage != "")
                {

                    if (File.Exists(watermarkImage))
                    {

                        //获取水印图片  

                        using (System.Drawing.Image wrImage = System.Drawing.Image.FromFile(watermarkImage))
                        {

                            //水印绘制条件:原始图片宽高均大于或等于水印图片  

                            if (newImage.Width >= wrImage.Width && newImage.Height >= wrImage.Height)
                            {

                                Graphics gWater = Graphics.FromImage(newImage);



                                //透明属性  

                                ImageAttributes imgAttributes = new ImageAttributes();

                                ColorMap colorMap = new ColorMap();

                                colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);

                                colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);

                                ColorMap[] remapTable = { colorMap };

                                imgAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);



                                float[][] colorMatrixElements = {   

                                   new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},  

                                   new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},  

                                   new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},  

                                   new float[] {0.0f,  0.0f,  0.0f,  1f, 0.0f},//透明度:0.5  

                                   new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}  

                                };



                                ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);

                                imgAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

                                gWater.DrawImage(wrImage, new Rectangle(newImage.Width - wrImage.Width, newImage.Height - wrImage.Height, wrImage.Width, wrImage.Height), 0, 0, wrImage.Width, wrImage.Height, GraphicsUnit.Pixel, imgAttributes);

                                gWater.Dispose();

                            }

                            wrImage.Dispose();

                        }

                    }

                }



                //保存缩略图  

                newImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);



                //释放资源  

                newG.Dispose();

                newImage.Dispose();

                initImage.Dispose();

            }

        }



        #endregion



        #region 其它

        /// <summary>  

        /// 判断文件类型是否为WEB格式图片  

        /// (注:JPG,GIF,BMP,PNG)  

        /// </summary>  

        /// <param name="contentType">HttpPostedFile.ContentType</param>  

        /// <returns></returns>  

        public static bool IsWebImage(string contentType)
        {

            if (contentType == "image/pjpeg" || contentType == "image/jpeg" || contentType == "image/gif" || contentType == "image/bmp" || contentType == "image/png")
            {

                return true;

            }

            else
            {

                return false;

            }

        }


        #endregion

    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值