【问题标题】:showing a generic error occured in GDI+ whle uploading image显示上传图像时 GDI+ 中发生一般错误
【发布时间】:2011-07-30 07:39:50
【问题描述】:

我正在使用此代码上传图片。我已授予将存储图像的文件夹的写入权限。以下是我的代码:

Dim con As New System.Data.SqlClient.SqlConnection("Data Source=Biplob-PC\SQLEXPRESS; database =a;Integrated Security=True")

    Dim smemberid As Integer
    Dim photoid As Integer
    Sub bindphoto()
        'What directory are we interested in?
        Dim mycommand As New SqlCommand("SELECT * FROM Photo WHERE MemberID = '" & smemberid & "' ORDER BY PhotoID", con)
        con.Open()
        dlFileList.DataSource = mycommand.ExecuteReader
        dlFileList.DataBind()
        con.Close()
    End Sub

    Sub memberid()
        Dim cmd As New SqlCommand("SELECT MemberID From Memberlist WHERE UserName = '" & Session("uName") & "'", con)
        Dim r As SqlDataReader
        con.Open()
        r = cmd.ExecuteReader
        If r.HasRows Then
            r.Read()
            smemberid = r("MemberID").ToString
        End If
        r.Close()
        con.Close()
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If flupload.HasFile = False Then
            Label1.Text = "Please select a picture from your computer"
            Exit Sub
        End If

        If flupload.FileName.GetType.ToString = "jpg" Then
            Label1.Text = "Hurrey"
            Exit Sub
        End If

        'Has the file been uploaded properly?
        If Not flupload.PostedFile Is Nothing Then
            'Save the filename if it has a filename and exists...

            Dim imageToBeResized As System.Drawing.Image = System.Drawing.Image.FromStream(flupload.PostedFile.InputStream)

            Dim imageHeight As Integer = imageToBeResized.Height
            Dim imageWidth As Integer = imageToBeResized.Width

            Dim maxHeight As Integer = 98
            Dim maxWidth As Integer = 98

            imageHeight = (imageHeight * maxWidth) / imageWidth
            imageWidth = maxWidth
            Try
                If flupload.PostedFile.FileName.Trim().Length > 0 And _
                flupload.PostedFile.ContentLength > 0 Then
                    photoid = (New Random).Next
                    Dim objstream As Stream = flupload.PostedFile.InputStream
                    Dim objimage As System.Drawing.Image = System.Drawing.Image.FromStream(objstream)
                    If objimage.RawFormat.Equals(ImageFormat.Gif) Or objimage.RawFormat.Equals(ImageFormat.Jpeg) Or objimage.RawFormat.Equals(ImageFormat.Png) Then
                        Dim strBaseDir As New DirectoryInfo((Request.PhysicalApplicationPath) + "images\gallery\")


                        If imageHeight > maxHeight Then

                            imageWidth = (imageWidth * maxHeight) / imageHeight

                            imageHeight = maxHeight

                        End If


                        Dim bitmap As New Bitmap(imageToBeResized, imageWidth, imageHeight)

                        Dim stream As System.IO.MemoryStream = New MemoryStream()

                        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
                        stream.Position = 0

                        Dim strFileName As String = _
                        Path.GetFileName(flupload.PostedFile.FileName)
                        bitmap.Save(((Request.PhysicalApplicationPath) + "images\gallery\thumbs\") & photoid & ".jpg")
                        bigimage()
                        'File has been saved!
                        Dim mycommand As New SqlCommand("Insert chairperson (memberid,name,period,achieve,imageurl,other) Values ( '" & smemberid & "','" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & photoid & "','" & TextBox4.Text & "' )", con)
                        con.Open()
                        mycommand.ExecuteNonQuery()
                        con.Close()

                        Label1.Text = "File has been successfully uploaded"


                    Else
                        Label1.Text = "Sorry, File format not supported."
                    End If

                End If
            Catch ex As Exception
                Label1.Text = ex.Message
            End Try
        Else
            Label1.Text = "<hr /><p>Enter a filename to upload!"
        End If

    End Sub
    Sub bigimage()
        Dim imageToBeResized As System.Drawing.Image = System.Drawing.Image.FromStream(flupload.PostedFile.InputStream)

        Dim imageHeight As Integer = imageToBeResized.Height
        Dim imageWidth As Integer = imageToBeResized.Width

        Dim maxHeight As Integer = 450
        Dim maxWidth As Integer = 450

        imageHeight = (imageHeight * maxWidth) / imageWidth
        imageWidth = maxWidth
        Dim objstream As Stream = flupload.PostedFile.InputStream
        Dim objimage As System.Drawing.Image = System.Drawing.Image.FromStream(objstream)
        If objimage.RawFormat.Equals(ImageFormat.Gif) Or objimage.RawFormat.Equals(ImageFormat.Jpeg) Or objimage.RawFormat.Equals(ImageFormat.Png) Then
            Dim strBaseDir As New DirectoryInfo((Request.PhysicalApplicationPath) + "images\gallery\")


            If imageHeight > maxHeight Then

                imageWidth = (imageWidth * maxHeight) / imageHeight

                imageHeight = maxHeight

            End If

            Dim bitmap As New Bitmap(imageToBeResized, imageWidth, imageHeight)

            Dim stream As System.IO.MemoryStream = New MemoryStream()

            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)

            stream.Position = 0

            Dim strFileName As String = _
            Path.GetFileName(flupload.PostedFile.FileName)
            bitmap.Save(((Request.PhysicalApplicationPath) + "images\gallery\") & photoid & ".jpg")

        End If
    End Sub
    Sub deleteg(ByVal s As Object, ByVal f As DataListCommandEventArgs)
        Dim photographid As String
        photographid = dlFileList.DataKeys.Item(f.Item.ItemIndex).ToString
        Dim mycommand As New SqlCommand("DELETE FROM Photo WHERE PhotoID = '" & photographid & "'", con)
        con.Open()
        mycommand.ExecuteNonQuery()
        con.Close()
        bindphoto()
        Label1.Text = "File has been deleted succefully"
    End Sub

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    一般错误是什么意思?

    无论如何,我发现您的按钮点击事件存在一些问题:

    If flupload.FileName.GetType.ToString = "jpg" Then
        Label1.Text = "Hurrey"
        Exit Sub
    End If 
    

    语句flupload.FileName.GetType.toString = "jpg" 甚至不应该编译。 GetType() 返回对象的类型(在本例中为 System.String)。

    即使它编译了,该语句总是会失败,不管 FileName 是什么。我认为您正在寻找的是:

    If flupload.FileName.EndsWith("jpg") Then
    

    我不确定您为什么要在按钮单击的其余事件处理程序中进行所有排列,因为保存文件应该像调用 flupload.SaveAs(&lt;path&gt;) 一样简单。

    此外,您正在使用以下代码将您的应用暴露给 SQL 注入攻击:

    Dim mycommand As New SqlCommand("Insert chairperson (memberid,name,period,achieve,imageurl,other) Values ( '" & smemberid & "','" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & photoid & "','" & TextBox4.Text & "' )", con)
    

    您应该使用参数化查询。

    阅读(彻底)FileUpload Class - 它包含您入门所需的一切,并且还有示例代码。

    【讨论】:

      【解决方案2】:

      此错误可能由多种原因引起。

      我多次收到此错误消息,每次都是因为权限问题,或者因为我在文件夹的拼写上有错误,或者找不到正在写入的文件夹。确保也可以写入子目录,并且正确的进程可以访问该文件夹。如果调试代码哪里会出现异常?

      您可能还想查看A generic error occurred in GDI+ 以了解其他要查看的内容。

      【讨论】:

        猜你喜欢
        • 2011-03-01
        • 2013-09-26
        • 1970-01-01
        • 2015-07-02
        • 2011-06-04
        • 2010-11-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多