/// <summary> /// dpi改变事件 /// </summary> private void numericUpDownDpi_ValueChanged(object sender, EventArgs e) { imgWidth = imgWidth * (int)numericUpDownDpi.Value / DPI; imgHeight = imgHeight * (int)numericUpDownDpi.Value / DPI; tbWidth.Text = ImgWidth.ToString(); tbHeight.Text = ImgHeight.ToString(); DPI = (int)numericUpDownDpi.Value; }
/// <summary> /// 默认构造函数 /// </summary> public ExportMapForm(int width = 0, int height = 0) { InitializeComponent(); imgWidth = width; imgHeight = height; tbWidth.Text = ImgWidth.ToString(); tbHeight.Text = ImgHeight.ToString(); tbExportPath.Text = filePath; }
/// <summary> /// 通用图片上传类 /// </summary> /// <param name="postedFile">HttpPostedFile控件</param> /// <param name="savePath">保存路径【sys.config配置路径】</param> /// <param name="finame">返回文件名</param> /// <param name="fisize">返回文件大小</param> /// <returns>【-1,上传失败,0-系统配置错误,1-上传图片成功,2-格式错误,3-超过文件上传大小,4-未上传文件】</returns> public static int FileSaveAs(HttpPostedFile postedFile, string savePath, ref string finame, ref int fisize) { try { if (string.IsNullOrEmpty(postedFile.FileName)) { return(4); } var rd = new Random(); int rdInt = rd.Next(1000, 9999); //重命名名称 string newfileName = DateTime.Now.Year + DateTime.Now.Month.ToString() + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond + rdInt; //获取上传文件的扩展名 string sEx = Path.GetExtension(postedFile.FileName); if (!CheckValidExt(AllowFormat, sEx)) { return(2); //格式错误 } //获取上传文件的大小 int postFileSize = postedFile.ContentLength / 1024; if (postFileSize > AllowSize) { return(3); //超过文件上传大小 } if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } string fullPath = savePath + newfileName + sEx; postedFile.SaveAs(fullPath); var bmp = new Bitmap(fullPath); int realWidth = bmp.Width; int realHeight = bmp.Height; bmp.Dispose(); #region 检测图片宽度限制 if (MinWidth > 0) { if (realWidth < MinWidth) { return(-1); } } #endregion #region 监测图片宽度是否超过600,超过的话,自动压缩到600 if (_limitWidth && realWidth > MaxWidth) { int mWidth = MaxWidth; int mHeight = mWidth * realHeight / realWidth; string tempFile = savePath + Guid.NewGuid() + sEx; File.Move(fullPath, tempFile); CreateSmallPhoto(tempFile, mWidth, mHeight, fullPath, "", ""); File.Delete(tempFile); } #endregion #region 压缩图片存储尺寸 if (sEx != null) { if (sEx.ToLower() != ".gif") { CompressPhoto(fullPath, 100); } #endregion finame = newfileName + sEx; } fisize = postFileSize; //生成缩略图片高宽 if (string.IsNullOrEmpty(ImgWidth)) { return(1); } string[] oWidthArray = ImgWidth.Split(','); string[] oHeightArray = ImgHeight.Split(','); if (oWidthArray.Length != oHeightArray.Length) { return(0); //系统配置错误 } for (int i = 0; i < oWidthArray.Length; i++) { if (Convert.ToInt32(oWidthArray[i]) <= 0 || Convert.ToInt32(oHeightArray[i]) <= 0) { continue; } string sImg = savePath + newfileName + "_" + i + sEx; if (CutImage) { CreateSmallPhoto(fullPath, Convert.ToInt32(oWidthArray[i]), Convert.ToInt32(oHeightArray[i]), sImg, "", ""); } else { CreateSmallPhoto(fullPath, Convert.ToInt32(oWidthArray[i]), Convert.ToInt32(oHeightArray[i]), sImg, "", "", CutMode.CutNo); } } #region 给大图添加水印 if (!string.IsNullOrEmpty(PicWater)) { AttachPng(PicWater, fullPath); } else if (!string.IsNullOrEmpty(WordWater)) { AttachText(WordWater, fullPath); } #endregion return(1); } catch { return(-1); } }
protected void btnUpLoad_Click(object sender, EventArgs e) { if (!fp.HasFile) { lb.Text = "请先选择文件"; return; } string fileurl = ""; if (IsSaveToOSS) { BFService.BFResult bfresult; BFService.BigFileService bs = new BFService.BigFileService(CarEnum.BigServiceSysID); if (ImgWidth == 0 || ImgHeight == 0) { bfresult = bs.UpLoadALL(HFID.Value, fp.FileName, fp.PostedFile.ContentType.ToString(), fp.FileBytes); } else { bfresult = bs.UpLoadALL(HFID.Value, fp.FileName, fp.PostedFile.ContentType.ToString(), fp.FileBytes, ImgWidth.ToString(), ImgHeight.ToString()); } if (bfresult.code == 0) { //失败! lb.ForeColor = Color.Red; lb.Text = bfresult.message; return; } else { fileurl = "http://big.tourzj.com/bfinfo/GetFile/" + bfresult.message; HFID.Value = fileurl; FileView.NavigateUrl = fileurl; } } else { string date = DateTime.Now.ToString("yyMMdd"); string newPath = Server.MapPath(upLoadPath + date + "/"); if (!Directory.Exists(newPath)) { System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(newPath); di.Create(); } string fnOrg = date + "/" + Guid.NewGuid().ToString() + fp.FileName.Substring(fp.FileName.LastIndexOf(".")); fp.SaveAs(Server.MapPath(upLoadPath) + fnOrg); HFID.Value = fnOrg; fileurl = upLoadPath + fnOrg; FileView.NavigateUrl = fileurl; } lb.Text = "上传成功"; lb.ForeColor = Color.Black; if (ShowImage) { ImgShow.ImageUrl = FileView.NavigateUrl; ImgShow.Visible = true; } else { ImgShow.Visible = false; } divView.Visible = true; //获得文件名,并且传递给调用的窗口 if (OnAfterUpLoad != null) { OnAfterUpLoad(fileurl, fp.FileName); } }