上传图片并按用户选择形成缩略图

整合~抄袭~嘿嘿 我就是借鉴。借鉴这个词实在是太伟大了哦!

看了网上很多上传文件的帖子,现在整合出我上传图片类,

放在这就是怕我以后用到又不好找,如果有人喜欢就随便拿吧

 

using System;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

 


/// <summary>
/// FileUpload 的摘要说明
/// </summary>
public class FileUpload
{
    private string Mode = "";
    private string SavePath = "";
    private string ThumbExtension = "";
    private int ThumbWidth = 0;
    private int ThumbHight = 0;
  

    #region 1、上传图片并修改成缩略图
    /// <summary>
    /// 上传图片平修改成固定大小的缩略图
    /// </summary>
    /// <param name="UpImage">HtmlInputFile控件</param>
    /// <param name="SavePath">上传文件被保存在服务器的路径</param>
    /// <param name="ThumbExtension">缩略图前缀</param>
    /// <param name="ThumbWidth">设定缩略宽度</param>
    /// <param name="intThumbHeight">设定缩略高度</param>
    /// <param name="Mode">缩略模式:固定大小;固定宽度;固定高度</param>
    /// <returns></returns>

    public string UpLoadImage(HtmlInputFile UpImage, string SavePath, string ThumbExtension, int ThumbWidth, int intThumbHeight, string Mode)
    {
        string Filename = "";
        string ThumbFile = "";

        //上传原图
        //判断是否选择文件
        if (UpImage.PostedFile != null)
        {
            HttpPostedFile myFile = UpImage.PostedFile;
            int nFileLen = myFile.ContentLength;
            if (nFileLen == 0)
            {
                return "没有选择上传图片";
            }

            //获取UpImage选择文件的扩展名
            string extendName = System.IO.Path.GetExtension(myFile.FileName).ToLower();
            //判断是否为图片格式
            if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png")
            {
                return "图片格式不正确";
            }

            byte[] myData = new Byte[nFileLen];
            myFile.InputStream.Read(myData, 0, nFileLen);
            Filename = System.IO.Path.GetFileName(myFile.FileName);
            int file_append = 0;

            //检查当前文件夹下是否有同名图片,有则在文件名+1
            while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(SavePath + Filename)))
            {
                file_append++;
                Filename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
                    + file_append.ToString() + extendName;
            }

            System.IO.FileStream newFile
                = new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(SavePath + Filename),
                System.IO.FileMode.Create, System.IO.FileAccess.Write);
            newFile.Write(myData, 0, myData.Length);
            newFile.Close();


           


            try
            {
                //原图加载
                using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(SavePath + Filename)))
                {
                    //原图宽度和高度
                    int width = sourceImage.Width;
                    int height = sourceImage.Height;
                    int smallWidth;
                    int smallHeight;

                    switch (Mode)
                    {
                        case "HW"://指定高宽缩放(可能变形)               
                            break;
                        case "W"://指定宽,高按比例                   
                            intThumbHeight = height * ThumbWidth / width;
                            break;
                        case "H"://指定高,宽按比例
                            ThumbWidth = width * intThumbHeight / height;
                            break;
                        //case "Cut"://指定高宽裁减(不变形)               
                        //    if ((double)width / (double)height > (double)ThumbWidth / (double)intThumbHeight)
                        //    {
                        //        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;
                    }
                    //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽  和 原图的高/缩略图的高)
                    if (((decimal)width) / height <= ((decimal)ThumbWidth) / intThumbHeight)
                    {
                        smallWidth = ThumbWidth;
                        smallHeight = ThumbWidth * height / width;
                    }
                    else
                    {
                        smallWidth = intThumbHeight * width / height;
                        smallHeight = intThumbHeight;
                    }


                    //判断缩略图在当前文件夹下是否同名称文件存在
                    file_append = 0;
                    ThumbFile = ThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + extendName;


                    while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(SavePath + ThumbFile)))
                    {
                        file_append++;
                        ThumbFile = ThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) +
                            file_append.ToString() + extendName;
                    }
                    //缩略图保存的绝对路径
                    string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(SavePath) + ThumbFile;


                    //新建一个图板,以最小等比例压缩大小绘制原图
                    using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight))
                    {
                        //绘制中间图
                        using (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(Color.Black);
                            g.DrawImage(
                            sourceImage,
                            new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight),
                            new System.Drawing.Rectangle(0, 0, width, height),
                            System.Drawing.GraphicsUnit.Pixel
                                        );
                        }
                        //新建一个图板,以缩略图大小绘制中间图
                        using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(ThumbWidth, intThumbHeight))
                        {
                            //绘制缩略图
                            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1))
                            {
                                //高清,平滑
                                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                                g.Clear(Color.Black);
                                int lwidth = (smallWidth - ThumbWidth) / 2;
                                int bheight = (smallHeight - intThumbHeight) / 2;
                                g.DrawImage(bitmap, new Rectangle(0, 0, ThumbWidth, intThumbHeight), lwidth, bheight, ThumbWidth, intThumbHeight, GraphicsUnit.Pixel);
                                g.Dispose();
                                bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                            }
                        }
                    }
                }
            }
            catch
            {
                //出错则删除
                System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(SavePath + Filename));
                return "图片格式不正确";
            }
            //返回缩略图名称
            return ThumbFile;
        }
        return "没有选择图片";
    }
    #endregion
    public  FileUpload()
    {
    }
 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值