现在的位置: 首页 > 综合 > 正文

上传图片的通用类

2014年02月12日 ⁄ 综合 ⁄ 共 31936字 ⁄ 字号 评论关闭

 using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Threading;
/// <summary>
/// UploadFile 的摘要说明
/// </summary>
namespace HangHaiControls.photo
{
    public class microUploadFile
    {
        private string mServerPath;//存储路径,例如:d:/Upload/
        private string mShowPath;//   '存储及显示时的目录,例如:album 此目录一起存储到数据库的文件名称中
        //'上两个连接在一起为存储的顶级目录,其下再建立年月日目录来存储
        //'如果没有设置mServerPath,
        // '则对mShowPath 进行httpcontext.current.server.path得到绝对目录,如果不存在则创建此目录
        private string mSavePath;//   '存储目录

        private int mAccessMaxLength;//'允许的文件最大长度(单位:KB)例如:200
        private string mAccessFileType;  //  '允许的图片类型:例如:"image/pjeg|image/gif"
        private string mFileTitle;

        #region " 属性"
        public string ServerPath
        {
            get
            {
                return mServerPath;
            }
            set
            {
                mServerPath = value;
            }
        }
        public string ShowPath
        {
            get
            {
                return mShowPath;
            }
            set
            {
                mShowPath = value;
            }
        }
        public string SavePath
        {
            get
            {
                return mSavePath;
            }
        }
        public int AccessMaxLength
        {
            get
            {
                return mAccessMaxLength;
            }
            set
            {
                mAccessMaxLength = value;
            }
        }
        public string AccessFileType
        {
            get
            {
                return mAccessFileType;
            }
            set
            {
                mAccessFileType = value;
            }
        }
        public string FileTitle
        {
            get
            {
                return mFileTitle;
            }
        }
        #endregion

        #region " 构造函数 参数初始化"
        public microUploadFile()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        public microUploadFile(string myServerPath, string myShowPath)
        {
            mServerPath = myServerPath;
            mShowPath = myShowPath;
            if (myServerPath != "" && myShowPath != "")
            {
                if (myServerPath.LastIndexOf("//") > 0)
                {
                    mSavePath = myServerPath + myShowPath;
                }
                else
                {
                    mSavePath = myServerPath + "//" + myShowPath;
                }
            }
            else if (myServerPath == "" && myShowPath != "")
            {
                mSavePath = HttpContext.Current.Server.MapPath(myShowPath);
            }
            else
            {
                throw new Exception("显示路径:myShowPath 不可为空");
            }
        }
        public microUploadFile(string myServerPath, string myShowPath, int myAccessMaxLength, string myAccessFileTypes)
        {
            mServerPath = myServerPath;
            mShowPath = myShowPath;
            if (myServerPath != "" && myShowPath != "")
            {
                if (myServerPath.LastIndexOf("//") > 0)
                {
                    mSavePath = myServerPath + myShowPath;
                }
                else
                {
                    mSavePath = myServerPath + "//" + myShowPath;
                }
            }
            else if (myServerPath == "" && myShowPath != "")
            {

                mSavePath = HttpContext.Current.Server.MapPath(myShowPath);
            }
            else
            {
                throw new Exception("显示路径:myShowPath 不可为空");
            }
            mAccessMaxLength = myAccessMaxLength;
            mAccessFileType = myAccessFileTypes;
        }
        #endregion
        //生成并返回相对于存储文件夹的 /年/月/日/  路径

        public string RelativePath()
        {
            if (!System.IO.Directory.Exists(mSavePath))
            {
                System.IO.Directory.CreateDirectory(mSavePath);
            }
            string strTemp;
            strTemp = "/" + DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.ToShortDateString() + "/";
            //HttpContext.Current.Response.Write(mSavePath + strTemp);
            if (!System.IO.Directory.Exists(mSavePath + strTemp))
            {
                System.IO.Directory.CreateDirectory(mSavePath + strTemp);
            }
            return strTemp;
        }

        //判断上传文件的格式是否为允许的图片格式
        //如果没有限制图片格式,则返回true
        public bool ValideFileType(FileUpload UpImage)
        {
            if (mAccessFileType != "" && mAccessFileType != null)
            {
                if (mAccessFileType.IndexOf(UpImage.PostedFile.ContentType) >= 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
        //httpostfile file filetype
        public bool ValideFileType(HttpPostedFile UpImage)
        {
            if (mAccessFileType != "" && mAccessFileType != null)
            {
                if (mAccessFileType.IndexOf(UpImage.ContentType) >= 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
        //判断上传文件的大小:大于0小于等于maxLength为true
        //如果没有限制大小,则返回 true
        public bool ValidateFileLength(FileUpload UpImage)
        {
            if (mAccessMaxLength > 0)
            {
                if (UpImage.PostedFile.ContentLength <= mAccessMaxLength * 1024)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
        //check httppostfile length
        public bool ValidateFileLength(HttpPostedFile UpImage)
        {
            if (mAccessMaxLength > 0)
            {
                if (UpImage.ContentLength <= mAccessMaxLength * 1024)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
        //'根据上传文件得到文件的类型扩展名:.gif .jpg等
        public string getExtensisionName(FileUpload UpImage)
        {
            switch (UpImage.PostedFile.ContentType)
            {
                case "image/pjpeg":
                    return ".jpg";
                case "image/gif":
                    return ".gif";
                case "image/png":
                    return ".png";
                case "image/bmp":
                    return ".bmp";
                default:
                    throw new Exception("错误提示:无法识别文件的类型");
            }
        }
        //check httppostedfile's type :gif,jpg
        public string getExtensisionName(HttpPostedFile UpImage)
        {
            switch (UpImage.ContentType)
            {
                case "image/pjpeg":
                    return ".jpg";
                case "image/gif":
                    return ".gif";
                case "image/png":
                    return ".png";
                case "image/bmp":
                    return ".bmp";
                default:
                    throw new Exception("错误提示:无法识别文件的类型");
            }
        }
        #region "System.Drawing.Imaging.ImageCodecInfo GetEncoderInfo(string mime)"
        private System.Drawing.Imaging.ImageCodecInfo GetEncoderInfo(string mimeType)
        {

            System.Drawing.Imaging.ImageCodecInfo[] encoders;
            encoders = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
            for (int j = 0; j < encoders.Length; j++)
            {
                //  ' HttpContext.Current.Response.Write("<li>" + encoders[j].MimeType.ToString());
                if (encoders[j].MimeType == mimeType)
                {
                    return encoders[j];
                }
            }
            return null;
        }
        #endregion

        //根据FileUpload组件得到的数据 直接存储原文件 并返回相对路径的文件名

        public string Save(FileUpload UpImage)
        {
            bool isImage;
            string myName;
            if (this.ValideFileType(UpImage))
            {
                if (this.ValidateFileLength(UpImage))
                {
                    mFileTitle = UpImage.PostedFile.FileName.Substring(UpImage.PostedFile.FileName.LastIndexOf("//") + 1, UpImage.PostedFile.FileName.LastIndexOf(".") - UpImage.PostedFile.FileName.LastIndexOf("//") - 1);
                    isImage = true;
                    myName = this.RelativePath() + Guid.NewGuid().ToString().Replace("-", "") + this.getExtensisionName(UpImage);
                    // HttpContext.Current.Response.Write(mSavePath + myName);
                    // HttpContext.Current.Response.End();
                    UpImage.PostedFile.SaveAs(mSavePath + myName);//  '保存原图片
                    return mShowPath + myName;
                }
                else
                {
                    throw new Exception("上传文件的大小不符合要求:最大" + mAccessMaxLength.ToString() + "KB");
                }
            }
            else
            {
                throw new Exception("上传文件的类型不符合要求:" + mAccessFileType + "");
            }
        }
        //save httppostedfile
        public string Save(HttpPostedFile UpImage)
        {
            bool isImage;
            string myName;
            if (this.ValideFileType(UpImage))
            {
                if (this.ValidateFileLength(UpImage))
                {
                    mFileTitle = UpImage.FileName.Substring(UpImage.FileName.LastIndexOf("//") + 1, UpImage.FileName.LastIndexOf(".") - UpImage.FileName.LastIndexOf("//") - 1);
                    isImage = true;
                    myName = this.RelativePath() + Guid.NewGuid().ToString().Replace("-", "") + this.getExtensisionName(UpImage);
                    // HttpContext.Current.Response.Write(mSavePath + myName);
                    // HttpContext.Current.Response.End();
                    UpImage.SaveAs(mSavePath + myName);//  '保存原图片
                    return mShowPath + myName;
                }
                else
                {
                    throw new Exception("上传文件的大小不符合要求:最大" + mAccessMaxLength.ToString() + "KB");
                }
            }
            else
            {
                throw new Exception("上传文件的类型不符合要求:" + mAccessFileType + "");
            }
        }

        //'根据FileUpload组件得到的数据 原图大小加上图片logo 并返回相对路径的文件名

        public string Save(FileUpload UpImage, int myQuality, string LogoImageServerName)
        {
            bool isImage;
            string myName;
            if (this.ValideFileType(UpImage))
            {
                if (this.ValidateFileLength(UpImage))
                {
                    mFileTitle = UpImage.PostedFile.FileName.Substring(UpImage.PostedFile.FileName.LastIndexOf("//") + 1, UpImage.PostedFile.FileName.LastIndexOf(".") - UpImage.PostedFile.FileName.LastIndexOf("//") - 1);
                    isImage = true;
                    System.Drawing.Image image;
                    image = System.Drawing.Image.FromStream(UpImage.PostedFile.InputStream);
                    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format48bppRgb);
                    System.Drawing.Graphics gp = System.Drawing.Graphics.FromImage(bmp);
                    gp.Clear(System.Drawing.Color.White);
                    gp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    gp.DrawImage(image, 0, 0, image.Width, image.Height);
                    System.Drawing.Image logo = System.Drawing.Image.FromFile(LogoImageServerName);
                    if (image.Width > logo.Width && image.Height > logo.Height)
                    {
                        gp.DrawImage(logo, new System.Drawing.Rectangle(0, image.Height - logo.Height, logo.Width, logo.Height), 0, 0, logo.Width, logo.Height, System.Drawing.GraphicsUnit.Pixel);
                    }
                    System.Drawing.Imaging.EncoderParameters objEncoderParameters = new System.Drawing.Imaging.EncoderParameters(1);
                    System.Drawing.Imaging.Encoder objEncoder0 = System.Drawing.Imaging.Encoder.Quality;
                    int intQuality = myQuality;//Convet.Type(ConfigurationSettings.AppSettings("ImageQuality"), Integer)
                    System.Drawing.Imaging.EncoderParameter objEncoderParameter0 = new System.Drawing.Imaging.EncoderParameter(objEncoder0, intQuality);
                    objEncoderParameters.Param[0] = objEncoderParameter0;
                    string myMimeType;
                    switch (UpImage.PostedFile.ContentType)
                    {
                        case "image/pjpeg":
                            myMimeType = "image/jpeg";
                            break;
                        case "image/gif":
                            myMimeType = "image/gif";
                            break;
                        default:
                            myMimeType = "image/jpeg";
                            break;
                    }
                    myName = this.RelativePath() + Guid.NewGuid().ToString().Replace("-", "") + this.getExtensisionName(UpImage);
                    bmp.Save(mSavePath + myName, this.GetEncoderInfo(myMimeType), objEncoderParameters);
                    objEncoderParameters.Dispose();
                    objEncoderParameter0.Dispose();
                    GC.SuppressFinalize(objEncoderParameters);
                    GC.SuppressFinalize(objEncoderParameter0);
                    image.Dispose();
                    gp.Dispose();
                    bmp.Dispose();
                    GC.SuppressFinalize(image);
                    GC.SuppressFinalize(gp);
                    GC.SuppressFinalize(bmp);
                    return mShowPath + myName;

                }
                else
                    throw new Exception("上传文件的大小不符合要求:最大" + mAccessMaxLength.ToString() + "KB");
            }
            else
            {
                throw new Exception("上传文件的类型不符合要求:" + mAccessFileType + "");
            }
        }
        //img add logo
        public string ImgSaveLogo(string imageName, string LogoImageServerName)
        {
            System.Drawing.Image image;
            image = System.Drawing.Image.FromFile(imageName);
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format48bppRgb);
            System.Drawing.Graphics gp = System.Drawing.Graphics.FromImage(bmp);
            gp.Clear(System.Drawing.Color.White);
            gp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            gp.DrawImage(image, 0, 0, image.Width, image.Height);
            System.Drawing.Image logo = System.Drawing.Image.FromFile(LogoImageServerName);
            if (image.Width > logo.Width && image.Height > logo.Height)
            {
                gp.DrawImage(logo, new System.Drawing.Rectangle(0, image.Height - logo.Height, logo.Width, logo.Height), 0, 0, logo.Width, logo.Height, System.Drawing.GraphicsUnit.Pixel);
            }
            // System.Drawing.Imaging.EncoderParameters objEncoderParameters = new System.Drawing.Imaging.EncoderParameters(1);
            // System.Drawing.Imaging.Encoder objEncoder0 = System.Drawing.Imaging.Encoder.Quality;
            // int intQuality = myQuality;//Convet.Type(ConfigurationSettings.AppSettings("ImageQuality"), Integer)
            //System.Drawing.Imaging.EncoderParameter objEncoderParameter0 = new System.Drawing.Imaging.EncoderParameter(objEncoder0, intQuality);
            // objEncoderParameters.Param[0] = objEncoderParameter0;
            // string myMimeType = "image / jpeg";

            string myName = this.RelativePath() + Guid.NewGuid().ToString().Replace("-", "") + ".jpg";
            bmp.Save(mSavePath + myName, System.Drawing.Imaging.ImageFormat.Jpeg);
            //objEncoderParameters.Dispose();
            //objEncoderParameter0.Dispose();
            // GC.SuppressFinalize(objEncoderParameters);
            // GC.SuppressFinalize(objEncoderParameter0);
            image.Dispose();
            gp.Dispose();
            bmp.Dispose();
            GC.SuppressFinalize(image);
            GC.SuppressFinalize(gp);
            GC.SuppressFinalize(bmp);
            return mShowPath + myName;

        }
        // save thumbail add logo
        public string ImgThumbnailSaveLogo(string imageName, int ThumbnailWidth, int ThumbnailHeight, string LogoImageServerName)
        {
            System.Drawing.Image image;
            image = System.Drawing.Image.FromFile(imageName);
            int originalWidth;
            int originalHeight;
            int newWidth;
            int newHeight;
            originalWidth = image.Width;
            originalHeight = image.Height;
            if (originalWidth > originalHeight)
            {
                if (originalWidth > ThumbnailWidth)
                {
                    newWidth = ThumbnailWidth;
                }
                else
                {
                    newWidth = originalWidth;
                }
                newHeight = (int)((float)originalHeight / (float)originalWidth * (float)newWidth);
                if (newHeight > ThumbnailHeight)
                {
                    newHeight = ThumbnailHeight;
                }
            }
            else
            {
                if (originalHeight > ThumbnailHeight)
                {
                    newHeight = ThumbnailHeight;
                }
                else
                {
                    newHeight = originalHeight;
                }
                newWidth = (int)((float)originalWidth / (float)originalHeight * (float)newHeight);
                if (newWidth > ThumbnailWidth)
                {
                    newWidth = ThumbnailWidth;
                }

            }
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format48bppRgb);
            System.Drawing.Graphics gp = System.Drawing.Graphics.FromImage(bmp);
            gp.Clear(System.Drawing.Color.White);
            gp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            gp.DrawImage(image, 0, 0, newWidth, newHeight);
            System.Drawing.Image logo = System.Drawing.Image.FromFile(LogoImageServerName);
            if (newWidth > logo.Width && newHeight > logo.Height)
            {
                gp.DrawImage(logo, new System.Drawing.Rectangle(0, newHeight - logo.Height, logo.Width, logo.Height), 0, 0, logo.Width, logo.Height, System.Drawing.GraphicsUnit.Pixel);
            }
            // System.Drawing.Imaging.EncoderParameters objEncoderParameters = new System.Drawing.Imaging.EncoderParameters(1);
            // System.Drawing.Imaging.Encoder objEncoder0 = System.Drawing.Imaging.Encoder.Quality;
            // int intQuality = myQuality;//Convet.Type(ConfigurationSettings.AppSettings("ImageQuality"), Integer)
            //System.Drawing.Imaging.EncoderParameter objEncoderParameter0 = new System.Drawing.Imaging.EncoderParameter(objEncoder0, intQuality);
            // objEncoderParameters.Param[0] = objEncoderParameter0;
            // string myMimeType = "image / jpeg";

            string myName = this.RelativePath() + Guid.NewGuid().ToString().Replace("-", "") + ".jpg";
            bmp.Save(mSavePath + myName, System.Drawing.Imaging.ImageFormat.Jpeg);
            //objEncoderParameters.Dispose();
            //objEncoderParameter0.Dispose();
            // GC.SuppressFinalize(objEncoderParameters);
            // GC.SuppressFinalize(objEncoderParameter0);
            image.Dispose();
            gp.Dispose();
            bmp.Dispose();
            GC.SuppressFinalize(image);
            GC.SuppressFinalize(gp);
            GC.SuppressFinalize(bmp);
            return mShowPath + myName;

        }
        //save httpuploadfile  add logo
        public string Save(HttpPostedFile UpImage, int myQuality, string LogoImageServerName)
        {
            bool isImage;
            string myName;
            if (this.ValideFileType(UpImage))
            {
                if (this.ValidateFileLength(UpImage))
                {
                    mFileTitle = UpImage.FileName.Substring(UpImage.FileName.LastIndexOf("//") + 1, UpImage.FileName.LastIndexOf(".") - UpImage.FileName.LastIndexOf("//") - 1);
                    isImage = true;
                    System.Drawing.Image image;
                    image = System.Drawing.Image.FromStream(UpImage.InputStream);
                    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format48bppRgb);
                    System.Drawing.Graphics gp = System.Drawing.Graphics.FromImage(bmp);
                    gp.Clear(System.Drawing.Color.White);
                    gp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    gp.DrawImage(image, 0, 0, image.Width, image.Height);
                    System.Drawing.Image logo = System.Drawing.Image.FromFile(LogoImageServerName);
                    if (image.Width > logo.Width && image.Height > logo.Height)
                    {
                        // gp.DrawImage(logo, new System.Drawing.Point(0, image.Height - logo.Height));
                        gp.DrawImage(logo, new System.Drawing.Rectangle(0, image.Height - logo.Height, logo.Width, logo.Height), 0, 0, logo.Width, logo.Height, System.Drawing.GraphicsUnit.Pixel);
                    }
                    System.Drawing.Imaging.EncoderParameters objEncoderParameters = new System.Drawing.Imaging.EncoderParameters(1);
                    System.Drawing.Imaging.Encoder objEncoder0 = System.Drawing.Imaging.Encoder.Quality;
                    int intQuality = myQuality;//Convet.Type(ConfigurationSettings.AppSettings("ImageQuality"), Integer)
                    System.Drawing.Imaging.EncoderParameter objEncoderParameter0 = new System.Drawing.Imaging.EncoderParameter(objEncoder0, intQuality);
                    objEncoderParameters.Param[0] = objEncoderParameter0;
                    string myMimeType;
                    switch (UpImage.ContentType)
                    {
                        case "image/pjpeg":
                            myMimeType = "image/jpeg";
                            break;
                        case "image/gif":
                            myMimeType = "image/gif";
                            break;
                        default:
                            myMimeType = "image/jpeg";
                            break;
                    }
                    myName = this.RelativePath() + Guid.NewGuid().ToString().Replace("-", "") + this.getExtensisionName(UpImage);
                    bmp.Save(mSavePath + myName, this.GetEncoderInfo(myMimeType), objEncoderParameters);
                    objEncoderParameters.Dispose();
                    objEncoderParameter0.Dispose();
                    GC.SuppressFinalize(objEncoderParameters);
                    GC.SuppressFinalize(objEncoderParameter0);
                    image.Dispose();
                    gp.Dispose();
                    bmp.Dispose();
                    GC.SuppressFinalize(image);
                    GC.SuppressFinalize(gp);
                    GC.SuppressFinalize(bmp);
                    return mShowPath + myName;

                }
                else
                    throw new Exception("上传文件的大小不符合要求:最大" + mAccessMaxLength.ToString() + "KB");
            }
            else
            {
                throw new Exception("上传文件的类型不符合要求:" + mAccessFileType + "");
            }
        }
        ////在缩略的图片上生成一个给定的LOGO:此LOGO为一个给定的图片

        //根据FileUpload 生成缩略图

        public string SaveThumbnailImage(FileUpload UpImage, int ThumbnailWidth, int ThumbnailHeight, int Quality, string LogoImageServerName)
        {
            System.Drawing.Image image;
            image = System.Drawing.Image.FromStream(UpImage.PostedFile.InputStream);
            int originalWidth;
            int originalHeight;
            int newWidth;
            int newHeight;
            originalWidth = image.Width;
            originalHeight = image.Height;
            if (originalWidth > originalHeight)
            {
                if (originalWidth > ThumbnailWidth)
                {
                    newWidth = ThumbnailWidth;
                }
                else
                {
                    newWidth = originalWidth;
                }
                newHeight = (int)((float)originalHeight / (float)originalWidth * (float)newWidth);
                if (newHeight > ThumbnailHeight)
                {
                    newHeight = ThumbnailHeight;
                }
            }
            else
            {
                if (originalHeight > ThumbnailHeight)
                {
                    newHeight = ThumbnailHeight;
                }
                else
                {
                    newHeight = originalHeight;
                }
                newWidth = (int)((float)originalWidth / (float)originalHeight * (float)newHeight);
                if (newWidth > ThumbnailWidth)
                {
                    newWidth = ThumbnailWidth;
                }

            }
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format48bppRgb);
            System.Drawing.Graphics gp = System.Drawing.Graphics.FromImage(bmp);
            gp.Clear(System.Drawing.Color.White);
            gp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            gp.DrawImage(image, 0, 0, newWidth, newHeight);
            System.Drawing.Image logo = System.Drawing.Image.FromFile(LogoImageServerName);
            if (newWidth > logo.Width && newHeight > logo.Height)
            {
                gp.DrawImage(logo, new System.Drawing.Rectangle(0, newHeight - logo.Height, logo.Width, logo.Height), 0, 0, logo.Width, logo.Height, System.Drawing.GraphicsUnit.Pixel);
            }
            System.Drawing.Imaging.EncoderParameters objEncoderParameters = new System.Drawing.Imaging.EncoderParameters(1);
            System.Drawing.Imaging.Encoder objEncoder0 = System.Drawing.Imaging.Encoder.Quality;
            int intQuality = Quality;
            System.Drawing.Imaging.EncoderParameter objEncoderParameter0 = new System.Drawing.Imaging.EncoderParameter(objEncoder0, intQuality);
            objEncoderParameters.Param[0] = objEncoderParameter0;
            string myMimeType;
            switch (UpImage.PostedFile.ContentType)
            {
                case "image/pjpeg":
                    myMimeType = "image/jpeg";
                    break;
                case "image/gif":
                    myMimeType = "image/gif";
                    break;
                default:
                    myMimeType = "image/jpeg";
                    break;
            }
            string myName = this.RelativePath() + Guid.NewGuid().ToString().Replace("-", "") + ThumbnailWidth.ToString() + ThumbnailHeight.ToString() + this.getExtensisionName(UpImage);
            bmp.Save(mSavePath + myName, GetEncoderInfo(myMimeType), objEncoderParameters);
            objEncoderParameters.Dispose();
            objEncoderParameter0.Dispose();
            GC.SuppressFinalize(objEncoderParameters);
            GC.SuppressFinalize(objEncoderParameter0);
            image.Dispose();
            gp.Dispose();
            bmp.Dispose();
            GC.SuppressFinalize(image);
            GC.SuppressFinalize(gp);
            GC.SuppressFinalize(bmp);
            return mShowPath + myName;
        }
        //thumbnail image httppostedimage add logo
        public string SaveThumbnailImage(HttpPostedFile UpImage, int ThumbnailWidth, int ThumbnailHeight, int Quality, string LogoImageServerName)
        {
            System.Drawing.Image image;
            image = System.Drawing.Image.FromStream(UpImage.InputStream);
            int originalWidth;
            int originalHeight;
            int newWidth;
            int newHeight;
            originalWidth = image.Width;
            originalHeight = image.Height;
            if (originalWidth > originalHeight)
            {
                if (originalWidth > ThumbnailWidth)
                {
                    newWidth = ThumbnailWidth;
                }
                else
                {
                    newWidth = originalWidth;
                }
                newHeight = (int)((float)originalHeight / (float)originalWidth * (float)newWidth);
                if (newHeight > ThumbnailHeight)
                {
                    newHeight = ThumbnailHeight;
                }
            }
            else
            {
                if (originalHeight > ThumbnailHeight)
                {
                    newHeight = ThumbnailHeight;
                }
                else
                {
                    newHeight = originalHeight;
                }
                newWidth = (int)((float)originalWidth / (float)originalHeight * (float)newHeight);
                if (newWidth > ThumbnailWidth)
                {
                    newWidth = ThumbnailWidth;
                }

            }
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format48bppRgb);
            System.Drawing.Graphics gp = System.Drawing.Graphics.FromImage(bmp);
            gp.Clear(System.Drawing.Color.White);
            gp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            gp.DrawImage(image, 0, 0, newWidth, newHeight);
            System.Drawing.Image logo = System.Drawing.Image.FromFile(LogoImageServerName);
            if (newWidth > logo.Width && newHeight > logo.Height)
            {
                //gp.DrawImage(logo, new System.Drawing.Point(0, newHeight - logo.Height));
                // System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(filename1));//原图
                //System.Drawing.Image newimage = System.Drawing.Image.FromFile(Server.MapPath("3.png"));//此对象为被加的水印图
                //Graphics g = Graphics.FromImage(image);
                gp.DrawImage(logo, new System.Drawing.Rectangle(0, newHeight - logo.Height, logo.Width, logo.Height), 0, 0, logo.Width, logo.Height, System.Drawing.GraphicsUnit.Pixel);
            }
            System.Drawing.Imaging.EncoderParameters objEncoderParameters = new System.Drawing.Imaging.EncoderParameters(1);
            System.Drawing.Imaging.Encoder objEncoder0 = System.Drawing.Imaging.Encoder.Quality;
            int intQuality = Quality;
            System.Drawing.Imaging.EncoderParameter objEncoderParameter0 = new System.Drawing.Imaging.EncoderParameter(objEncoder0, intQuality);
            objEncoderParameters.Param[0] = objEncoderParameter0;
            string myMimeType;
            switch (UpImage.ContentType)
            {
                case "image/pjpeg":
                    myMimeType = "image/jpeg";
                    break;
                case "image/gif":
                    myMimeType = "image/gif";
                    break;
                default:
                    myMimeType = "image/jpeg";
                    break;
            }
            string myName = this.RelativePath() + Guid.NewGuid().ToString().Replace("-", "") + ThumbnailWidth.ToString() + ThumbnailHeight.ToString() + this.getExtensisionName(UpImage);
            bmp.Save(mSavePath + myName, GetEncoderInfo(myMimeType), objEncoderParameters);
            objEncoderParameters.Dispose();
            objEncoderParameter0.Dispose();
            GC.SuppressFinalize(objEncoderParameters);
            GC.SuppressFinalize(objEncoderParameter0);
            image.Dispose();
            gp.Dispose();
            bmp.Dispose();
            GC.SuppressFinalize(image);
            GC.SuppressFinalize(gp);
            GC.SuppressFinalize(bmp);
            return mShowPath + myName;
        }
        public void DelPhoto(string path)
        {
            if (System.IO.File.Exists(System.Web.HttpContext.Current.Request.MapPath(path)))
            {
                System.IO.File.Delete(System.Web.HttpContext.Current.Request.MapPath(path));
            }
        }
        //create page    ///path是物理路径
        public void creatPage(string Url, string WebPage)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Url);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream resStream = response.GetResponseStream();
                StreamReader sr = new StreamReader(resStream, System.Text.Encoding.UTF8);
                string html = sr.ReadToEnd();
                System.Text.StringBuilder sb = new System.Text.StringBuilder(html);
                resStream.Close();
                sr.Close();
                System.IO.StreamWriter sw;
                sw = new System.IO.StreamWriter(WebPage, false, System.Text.Encoding.UTF8);
                sw.WriteLine(sb.ToString());
                sw.Close();

            }
            catch (Exception message)
            {
                throw message;
            }
        }
        public float getImageBL(FileUpload Fu)
        {
            //判断图片的长和宽
            int FileLength = Fu.PostedFile.ContentLength;//取得数据的长度
            Byte[] FileByteArray = new byte[FileLength];//图象文件临时存储到Byte数组里
            System.IO.Stream StreamObject = Fu.PostedFile.InputStream;//建立数据流对象
            StreamObject.Read(FileByteArray, 0, FileLength);
            //建立图片对象
            System.Drawing.Image MyImage = System.Drawing.Image.FromStream(StreamObject);
            return (float)MyImage.Width / (float)MyImage.Height;
        }
    }
}

 
 

抱歉!评论已关闭.