.net 图片上传

简介:   ///       /// asp.net上传图片并生成缩略图      ///       /// HtmlInputFile控件      /// 保存的路径,些为相对服务器路径的下的文件夹      /// 缩略图的thumb      //...
  /// <summary> 
    /// asp.net上传图片并生成缩略图 
    /// </summary> 
    /// <param name="upImage">HtmlInputFile控件</param> 
    /// <param name="sSavePath">保存的路径,些为相对服务器路径的下的文件夹</param> 
    /// <param name="sThumbExtension">缩略图的thumb</param> 
    /// <param name="intThumbWidth">生成缩略图的宽度</param> 
    /// <param name="intThumbHeight">生成缩略图的高度</param> 
    /// <returns>缩略图名称</returns> 
    public string UpLoadImage(FileUpload upImage, string savePath, string sThumbExtension, int intThumbWidth, int intThumbHeight)
    {
        if (upImage.HasFile)
        {
            if (upImage.PostedFile.ContentLength > 0 && upImage.PostedFile.ContentLength / 1024 < 1024)
            {
                string imgType = upImage.PostedFile.ContentType.ToString(); // 图片类型
                if (imgType.Equals("image/pjpeg") || imgType.Equals("image/gif") || imgType.Equals("image/png"))
                {
                    //取得上传文件的扩展名
                    string strExt = upImage.PostedFile.FileName.Substring(upImage.PostedFile.FileName.LastIndexOf("."));
                    // 自定义名称保存
                    string newImaName = (sThumbExtension + strExt).ToString();
                    // 保存到指定目录
                    string newsavePath = Server.MapPath(savePath);

                    System.Drawing.Image img = System.Drawing.Image.FromFile(upImage.PostedFile.FileName);
                    if (img.Width < intThumbWidth && img.Height < intThumbHeight)
                    {
                        return "图片的宽度和高度不符合(" + intThumbWidth + "*" + intThumbHeight+")";
                    }
                    else
                    {
                        upImage.PostedFile.SaveAs(newsavePath + newImaName);
                        return "上传图片成功.";
                    }

                  
                    //try
                    //{
                    //    //原图加载
                    //    using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(upImage.PostedFile.FileName))
                    //    {
                    //        //原图宽度和高度
                    //        int width = sourceImage.Width;
                    //        int height = sourceImage.Height;
                    //        int smallWidth;
                    //        int smallHeight;
                    //        //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽  和 原图的高/缩略图的高)
                    //        if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
                    //        {
                    //            smallWidth = intThumbWidth;
                    //            smallHeight = intThumbWidth * height / width;
                    //        }
                    //        else
                    //        {
                    //            smallWidth = intThumbHeight * width / height;
                    //            smallHeight = intThumbHeight;
                    //        }

                    //        // 新建一个图板,以最小等比例压缩大小绘制原图
                    //        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(intThumbWidth, 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 - intThumbWidth) / 2;
                    //                    int bheight = (smallHeight - intThumbHeight) / 2;
                    //                    g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);
                    //                    g.Dispose();
                    //                    //缩略图保存的绝对路径
                    //                    bitmap1.Save(newsavePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                    //                }
                    //            }
                    //        }

                    //    }
                    //}
                    //catch(Exception ex)
                    //{
                    //   return ex.Message.ToString();
                    //}
                  
                }
                else
                {
                    return "图片类型不正确.";
                }

            }
            else
            {
                return "请选择不大于1M的图片.";
            }
        }
        else
        {
            return "没有选择图片";
        }
    }
目录
相关文章
|
开发框架 前端开发 JavaScript
ASP.NET MVC使用Layui选择多图片上传
ASP.NET MVC使用Layui选择多图片上传
327 0
ASP.NET MVC使用Layui选择多图片上传
|
Web App开发 存储 NoSQL
Asp.Net Core Web Api图片上传(一)集成MongoDB存储实例教程
Asp.Net Core Web Api图片上传及MongoDB存储实例教程(一) 图片或者文件上传相信大家在开发中应该都会用到吧,有的时候还要对图片生成缩略图。那么如何在Asp.Net Core Web Api实现图片上传存储以及生成缩略图呢?今天我就使用MongoDB作为图片存储,然后使用SixLabors作为图片处理,通过一个Asp.
1404 0
|
.NET Shell 数据安全/隐私保护
ASP.NET 图片上传工具类 upload image简单好用功能齐全
原文:ASP.NET 图片上传工具类 upload image简单好用功能齐全 使用方法: UploadImage ui = new UploadImage(); /***可选参数***/ ui.
1192 0
|
JavaScript 前端开发 .NET
ASP.NET MVC 4.0 CKEditor jquery 文章管理 图片上传 解决方案
文章修改,新增业务的C#代码 [HttpPost] [ValidateInput(false)] public ActionResult AddNew(FormCollection values) { var db...
1195 0
下一篇
无影云桌面