zoukankan      html  css  js  c++  java
  • 图片管理类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

        }
    }


  • 相关阅读:
    BigDecimal保留几位小数方法及其八种舍入模式
    JAVA 将浮点类型的字符串转换成整数类型 (出现转换异常)
    登录linux服务器,显示为 -bash-4.2$
    linux创建用户并授权
    Linux 磁盘挂载
    ping与 curl与telnet 使用
    Docker运行参容器参数说明
    Docker挂载安装Nginx
    Docker学习
    Linux下 文件存放规范
  • 原文地址:https://www.cnblogs.com/kevinGao/p/2238886.html
Copyright © 2011-2022 走看看