''' <summary>
''' 上传图片
''' </summary>
''' <param name="f">上传控件ID</param>
''' <param name="UploadPhotoSize">上传大小(字节)</param>
''' <param name="FileDirectory">文件存放目录</param>
''' <param name="IsCreateSmail">是否创建小图</param>
''' <param name="AllowUploadFileType">允许上传的文件类型(格式为.jpg,.gif,.bmp)</param>
''' <param name="SmallImageWidth">小图宽度(没有为0)</param>
''' <param name="SmallImageHeight">小图高度(没有为0)</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function UploadImg(ByVal f As HttpPostedFile, ByVal UploadPhotoSize As Integer, ByVal FileDirectory As String, ByVal IsCreateSmail As Boolean, ByVal AllowUploadFileType As String, ByVal SmallImageWidth As Integer, ByVal SmallImageHeight As Integer, ByVal IsSmail As Boolean) As String
Dim FileName As String
Try
Dim Fn, ExtFn, FP As String
'Dim f As HttpPostedFile = fileUpload.PostedFile
If f Is Nothing Or f.ContentLength < 10 Then
Return ""
End If
'Dim UploadPhotoSize As Int32 = System.Configuration.ConfigurationManager.AppSettings("UploadPhotoSize")
If f.ContentLength > UploadPhotoSize Then
Instar.Comm.WebPageUtil.ShowMsg("文件大小超过限制(当前最大限制:" & Privilege.GetSize(UploadPhotoSize), HttpContext.Current.Response)
Return ""
End If
Fn = System.IO.Path.GetFileNameWithoutExtension(f.FileName)
ExtFn = System.IO.Path.GetExtension(f.FileName).ToLower()
If IsSmail Then
FP = "Up_S" & Instar.Comm.CreateTimeStamp()
Else
FP = "Up_" & Instar.Comm.CreateTimeStamp()
End If
Dim img1 As System.Drawing.Image
If AllowUploadFileType.IndexOf(ExtFn.ToLower()) = -1 Then
Instar.Comm.WebPageUtil.ShowMsg("不允许上传此类型的文件!(允许上传的:" + AllowUploadFileType + ")", HttpContext.Current.Response)
Return ""
End If
If ExtFn = ".jpg" Or ExtFn = ".jpeg" Or ExtFn = ".bmp" Or ExtFn = ".gif" Then
img1 = System.Drawing.Image.FromStream(f.InputStream)
If IsCreateSmail Then
Dim Scaling As Decimal = img1.Width / img1.Height
Dim FixWidth, FixHeight As Integer
FixWidth = SmallImageWidth
FixHeight = FixWidth / Scaling
If FixHeight > SmallImageHeight Then
FixHeight = SmallImageHeight
FixWidth = FixHeight * Scaling
End If
Dim img2 As System.Drawing.Image = img1.GetThumbnailImage(FixWidth, FixHeight, Nothing, New IntPtr(0))
FileName = HttpContext.Current.Server.MapPath(FileDirectory & "/" & FP & "S.jpg")
img2.Save(FileName, Instar.Comm.ImageUtil.ImageCodecInfo(), Instar.Comm.ImageUtil.HighQuitlyJpeg(88))
End If
If ExtFn <> ".gif" Then 'gif不使用流保存,以免破坏动画
img1.Save(HttpContext.Current.Server.MapPath(FileDirectory & "/" & FP & ExtFn), Instar.Comm.ImageUtil.ImageCodecInfo(), Instar.Comm.ImageUtil.HighQuitlyJpeg(88))
Else
img1.Save(HttpContext.Current.Server.MapPath(FileDirectory & "/" & FP & ExtFn))
End If
Else
f.SaveAs(HttpContext.Current.Server.MapPath(FileDirectory & "/" & FP & ExtFn))
End If
FileName = FP & ExtFn
Catch ex As Exception
Instar.Comm.WebPageUtil.ShowMsg("对不起,上传图片出现问题(问题描述是:" & ex.Message & "),请联系网站管理人员,谢谢!", HttpContext.Current.Response)
Return ""
End Try
Return FileName
End Function
Public Shared Function GetSize(ByVal BitStr As Long) As String
If BitStr <= 1024 Then
Return BitStr & "字节"
End If
If BitStr <= 1024 * 1024 Then
Return Math.Round(BitStr / 1024, 2).ToString() & "KB"
End If
Return Math.Round(BitStr / 1024 / 1024, 2).ToString() & "MB"
End Function