zoukankan      html  css  js  c++  java
  • .net 图片上传

      /// <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 "没有选择图片";
            }
        }
  • 相关阅读:
    sql server 的存储过程
    vue SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
    数据结构 基本概念和术语
    vue v-show指令
    vue v-model :
    vue 指令
    vue 挂载点 实例 模板
    vue(1) 第一个例子
    【BZOJ1150】[CTSC2007]数据备份Backup 双向链表+堆(模拟费用流)
    【BZOJ1109】[POI2007]堆积木Klo 二维偏序
  • 原文地址:https://www.cnblogs.com/y0umer/p/3839005.html
Copyright © 2011-2022 走看看