コード例 #1
0
ファイル: uploadimage.aspx.cs プロジェクト: elfmedy/2013
        //上传图片
        protected void imageUpload_tj(object sender, EventArgs e)
        {
            UploadFile imageObj = new UploadFile();
            imageObj.MaxFileSize = 1024;  //最多只能上传1M的文件大小
            imageObj.FileType = "jpg|jepg|bmp";
            string imagePath = Server.MapPath("~/tmpupload/");

            //上传图片并保存
            imageObj.UploadFileGo(imagePath, imageUploader);
            if (imageObj.UploadState == false)
            {
                uploadInfo.Text = "";
                uploadInfo.Text = "上传失败:" + imageObj.UploadInfo;
            }
            else
            {
                uploadInfo.Text = "Tips:上传图片的格式为jpg,jepg,bmp,小于1M";
                imageToModify.ImageUrl = "tmpupload/" + imageObj.NewFileName;
                //限制图片的高度和宽度
                System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/tmpupload/") + imageObj.NewFileName);
                int height = img.Height;
                int width = img.Width;
                if (width >= height)
                {
                    imageToModify.Style["width"] = "640px";
                    imageToModify.Style["height"] = (640.0 * height / width).ToString() + "px";
                }
                else
                {
                    imageToModify.Style["width"] = (640.0 * width / height).ToString() + "px";
                    imageToModify.Style["height"] = "640px";
                }
                hiddenHW.Value = width.ToString() + "," + height.ToString();
                hiddenFileName.Value = imageObj.NewFileName;
                modify.Style["display"] = "block";  //最后才显示图片
            }
        }