上传图片及生成小图片功能源码

if (this.FileUpload1.HasFile)
        {
            if (this.ddlAlbumList.Items.Count != 0)
            {
                int albumId = int.Parse(this.ddlAlbumList.SelectedValue);
                string file = "photos//" + userid + "//" + albumId + "//";

                string fillname = this.FileUpload1.PostedFile.FileName.ToLower();
                string type = fillname.Substring(fillname.LastIndexOf(".") + 1).ToString();
                if (type == "jpeg" || type == "jpg" || type == "gif" || type == "png")
                {
                    int size = Convert.ToInt32(this.FileUpload1.PostedFile.ContentLength) / 1024;
                    if (size < imgMaxLength)
                    {
                        if (!Directory.Exists(Server.MapPath(file)))
                        {
                            Directory.CreateDirectory(Server.MapPath(file));
                        }

                        try
                        {
                            if (type == "jpg")
                            {
                                type = "jpeg";
                            }
                            string path = file + Guid.NewGuid().ToString();
                            string url = path + "." + type;
                            string url2 = path + ".jpg";
                            this.FileUpload1.SaveAs(this.Server.MapPath(url));
                            System.Drawing.Image imgoutput1 = Bitmap.FromFile(this.Server.MapPath(url));

                            int imgWidth = 0;
                            int gifwidth, gifheight;
                            if (imgoutput1.Width < imgoutput1.Height)
                            {
                                imgWidth = 86;
                                gifheight = imgWidth;
                                gifwidth = (int)(double)imgoutput1.Width * imgWidth / imgoutput1.Height;
                            }
                            else
                            {
                                imgWidth = 96;
                                gifwidth = imgWidth;
                                gifheight = (int)(double)imgoutput1.Height * imgWidth / imgoutput1.Width;
                            }

                            System.Drawing.Image imgoutput2 = imgoutput1.GetThumbnailImage(gifwidth, gifheight, null, IntPtr.Zero);
                            imgoutput2.Save(this.Server.MapPath(path + ".jpg"), ImageFormat.Jpeg);

                            string name = this.txtName.Value.Trim();
                            string descirption = this.txtDescription.Value.Trim();
                            DateTime dt = Convert.ToDateTime(System.DateTime.Now);

                            if (mydb.UploadImage(albumId, name, descirption, url,url2, dt))
                            {
                                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>AlertMsg1('信息提示','上传相片成功!','',260,160);parent.frames('myiframe').document.location.reload()</script>");
                            }
                            else
                            {
                                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>AlertMsg1('信息提示','上传相片失败!','',260,160);</script>");
                            }
                        }
                        catch
                        {
                            return;
                        }
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>AlertMsg1('信息提示','图片太大,请缩小后上传!','',260,160);</script>");
                    }
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>AlertMsg1('信息提示','请上传jpg、jpeg、gif、png格式相片!','',260,160);</script>");
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>AlertMsg1('信息提示','请创建相册!','',260,160);</script>");
            }
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>AlertMsg1('信息提示','请选择上传文件!','',260,160);</script>");
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值