开发者

showing a generic error occured in GDI+ whle uploading image

I am using this code for uploading image. I have given write permission to the folder where image will be stored. Following is my code:

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

    Dim smemberid As Integer
    Dim photo开发者_如何转开发id 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


What do you mean by generic error?

In any event, I see a couple of issues with your button click event:

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

The statement flupload.FileName.GetType.toString = "jpg" shouldn't even compile. GetType() returns the type of the object (in this case, System.String).

If it even did compile, the statement would always fail, regardless of what FileName was. I think what you're looking for is this:

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

I'm not sure why you're going through all the permutations you are in the rest of the event handler for the button click, as saving the file should be as simple as calling flupload.SaveAs(<path>).

Also, you are exposing your app to SQL Injection Attacks with the following code:

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)

You should be using parameterized queries.

Read (thoroughly) FileUpload Class - it has everything you need to get started, and has example code as well.


This error can be caused by many things.

I got this error message a few times, each time its been because of a permissions issue or because I've made a mistake with the spelling of a folder or the folder being written to cannot be found. make sure that sub directories can also be written to, and that the correct process has access to the folder. if you debug the code where does the exception occur?

you may also want to look at A generic error occurred in GDI+ for other things to look at.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜