浏览(图片上传代码)

''' <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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值