图片上传+缩略图

            HttpPostedFileBase file = Request.Files[0];

            ResultMsg msg = new ResultMsg();

            if (file != null)
            {

                if (file.ContentLength > 5242880)  //判断图片大小
                {
                    msg.Code = 0;
                    msg.Msg = "上传头像不允许大于5M";
                }
                else
                {

                    var suff = file.FileName.Substring(file.FileName.IndexOf('.')); // 获取图片后缀名

                    suff = suff.ToLower();

                    string[] imgFormat = { ".jpg", ".png", ".bmp" };   //判断图片格式
                    var isImgFormat = false;

                    for (int i = 0; i < imgFormat.Length; i++)
                    {
                        if (suff == imgFormat[i])
                        {
                            isImgFormat = true;
                            break;
                        }
                    }

                    if (!isImgFormat)   //判断图片格式
                    {
                        msg.Code = 0;
                        msg.Msg = "图片不符合头像指定格式!";

                    }
                    else
                    {
                        try
                        {

                            Parallel.Invoke(() =>
                            {
                                string UploadImgSavePath = @"/Images/Original/";  //原图存放路径

                                file.SaveAs(Server.MapPath(UploadImgSavePath + DateTime.Now.ToString("yyyyMMddHHmmssffff") + "_Original" + suff));

                                msg.Data = UploadImgSavePath + DateTime.Now.ToString("yyyyMMddHHmmssffff") + "_Original" + suff;



                            }, () =>
                            {
                                string ThumbnailPath = @"/Images/Thumbnail/";     //缩略图存放路径

                                int ThumbnaiWidth = 100;   //缩略宽度
                                int ThumbnaiHeight = 100;  //缩略高度
                                Image img = Image.FromStream(file.InputStream);

                                if (img.Width <= ThumbnaiWidth && img.Height <= ThumbnaiHeight) //如果指定浓缩图的宽高大于原图就直接保存原图
                                    file.SaveAs(Server.MapPath(ThumbnailPath + DateTime.Now.ToString("yyyyMMddHHmmssffff") + "_Thumbnail" + suff));
                                else
                                {
                                    Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(() => { return false; });
                                    Image reduceImage = img.GetThumbnailImage(ThumbnaiWidth, ThumbnaiHeight, callb, IntPtr.Zero);
                                    reduceImage.Save(Server.MapPath(ThumbnailPath + DateTime.Now.ToString("yyyyMMddHHmmssffff") + "_Thumbnail" + suff));

                                }

                                msg.Data = ThumbnailPath + DateTime.Now.ToString("yyyyMMddHHmmssffff") + "_Thumbnail" + suff;
                            });

                            msg.Code = 1;
                            msg.Msg = "上传成功";
                        }
                        catch (Exception ex)
                        {
                            msg.Code = 0;
                            msg.Msg = "错误:" + ex.Message;
                        }
                    }
                }
            }
            else
            {
                msg.Code = 0;
                msg.Msg = "图片丢失了,请重新尝试!";
            }
            return msg.SendMsg(msg);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值