C#上传图片并且缩放图片代码

本文介绍了一个ASP.NET Web应用程序中的文件上传功能实现方法。包括如何生成唯一文件名、保存不同尺寸的图片到指定目录,并提供了错误处理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Drawing;

using System.Drawing.Imaging;

namespace UpLoadFile
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


        }


        private static Object ForCreateGUIDLock = new Object();
        public static string CreateGUID()
        {
            lock (ForCreateGUIDLock)
            {
                return Guid.NewGuid().ToString("N");
            }
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                string filePath = FileUpload1.PostedFile.FileName;
               
                string fileName = CreateGUID() + filePath.Substring(filePath.LastIndexOf("."));

Server.MapPath("~/fileUpload");//ConfigurationManager.AppSettings["fileUploadPath"];
                //"E:\\uploadfile";//Server.MapPath("~/Picture");


                string sourcePath = path + "\\source\\"; //源文件路径ConfigurationManager.AppSettings["source"];//
                string middlePath = path + "\\middle\\";   //中号的图片路径ConfigurationManager.AppSettings["middel"];//
                string smallPath = path + "\\small\\";     //小图片路径ConfigurationManager.AppSettings["small"];//


                string year = DateTime.Now.Year.ToString();
                string mouth = DateTime.Now.Month.ToString();
                string day = DateTime.Now.Day.ToString();


                sourcePath = sourcePath + year + "\\" + mouth + "\\" + day + "\\";
                middlePath = middlePath + year + "\\" + mouth + "\\" + day + "\\";
                smallPath = smallPath + year + "\\" + mouth + "\\" + day + "\\";


                if (!Directory.Exists(path))
                {  //判断是否存在uploadfile路径
                    Directory.CreateDirectory(path);
                }
                if (!Directory.Exists(sourcePath))    //判断 、创建原图片路径
                {
                    Directory.CreateDirectory(sourcePath);
                }
                
                //上传原图片
                FileUpload1.SaveAs(sourcePath + fileName);


                //上传小图片
                UploadImage(fileName, sourcePath, smallPath, 100, 150);


                //上传中图片
                UploadImage(fileName, sourcePath, middlePath, 400, 400);


                Response.Write("<script>alert('上传文件成功!')</script>");
            }
            catch (Exception)
            {
                Response.Write("<script>alert('上传文件失败!')</script>"); 
            }

     }


        private void UploadImage(string fileName, string sourcePath, string path, int newWidth, int newHeight)
        {
           


            //得到小图片Image对象
            System.Drawing.Image smallImage = GetNewImage(sourcePath + fileName, newWidth, newHeight);


            if (!Directory.Exists(path)) //判断、创建小图片的存放路径
            {
                Directory.CreateDirectory(path);
            }
            smallImage.Save(path + fileName);
        }




        /// <summary>
        /// 对图片进行处理,返回一个Image类别的对象
        /// </summary>
        /// <param name="oldImgPath">原图片路径</param>
        /// <param name="newWidth">新图片宽度</param>
        /// <param name="newHeight">新图片高度</param>
        /// <returns></returns>
        private System.Drawing.Image GetNewImage(string oldImgPath, int newWidth, int newHeight)
        {
            System.Drawing.Image newImage = null;
            System.Drawing.Image oldImage = null;
            oldImage = System.Drawing.Image.FromFile(oldImgPath);  //加载原图片
            newImage = oldImage.GetThumbnailImage(newWidth, newHeight,
                new System.Drawing.Image.GetThumbnailImageAbort(IsTrue), IntPtr.Zero); //对原图片进行缩放
            return newImage;
        }


        /// <summary>
        /// 在Image类别对图片进行缩放的时候,需要一个返回bool类别的委托
        /// </summary>
        /// <returns></returns>

    private bool IsTrue() {
            return true;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值