locked
help resizing an uploaded image and saving it. Getting "a generic GDI error occured" message RRS feed

  • Question

  • User-319159958 posted

    I have an app which uploads a picture, sizes it and stores it in the session while it is being displayed and until the user decides to keep it or not.

     

    then it calls another sunction to save the picture that is kept in the session variable but at that point I get an error on my live version.

    the error is" A generic GDI error occured"

    That error only occurs on my live version, meaning that on my development version in visual studio the error does not happen.

    also the eror seem to happen only with cetain file, and not necessarely the big ones.

    Why is this happening? below is the code.

    this is the procedure that receives the uploaded file, resizes it and stores it in the session:

    Dim upl As FileUpload = PreviousPage.Fileup ' = CType(PreviousPage.FindControl("filUpload"), FileUpload)

    Dim goodv As Boolean

    Dim errorit As String

    If upl.PostedFile IsNot Nothing AndAlso upl.PostedFile.ContentLength > 0 Then

    Dim FileOK As Boolean

    Dim UpLoadTriggerv As Integer

    UpLoadTriggerv = 1

    Try

    Dim fileSize As Integer = upl.PostedFile.ContentLength

    ' Allow only files less than 2,100,000 bytes (approximately 2 MB) to be uploaded.

    If Not ((fileSize > MaxFileSize) And (MaxFileSize <> 0)) Then

    Dim fileExtension As String

    fileExtension = System.IO.Path. _

    GetExtension(upl.FileName).ToLower()

    Dim allowedExtensions As String() = {".jpg", ".jpeg", ".png", ".gif"}

    If allowedExtensions.Length > 0 Then

    For i As Integer = 0 To allowedExtensions.Length - 1

    If fileExtension = allowedExtensions(i) Then

    FileOK = True

    End If

    Next

    Else

    FileOK = True

    End If

    If FileOK Then

    Dim bitO As System.Drawing.Image = System.Drawing.Image.FromStream(upl.FileContent)

    Dim originalBounds As RectangleF

    If Not (CSng(bitO.Width) > (CSng(bitO.Height) * 1.99)) Or (CSng(bitO.Height) > CSng((bitO.Width) * 1.99)) Then

    Dim ImFormat As System.Drawing.Imaging.ImageFormat

    HttpContext.Current.Session("CurrentFileExtention") = fileExtension

    Dim ContentType As String = ""

     

    HttpContext.Current.Session(
    "CurrentcontentType") = ContentType

    HttpContext.Current.Session("CurrentImageFormat") = ImFormat

    If (bitO.Width > MaxPictureSize) And (MaxPictureSize <> 0) Then

    Dim pixel As System.Drawing.GraphicsUnit = System.Drawing.GraphicsUnit.Pixel

    originalBounds = bitO.GetBounds(pixel)

    'Calculate the thumbnail size (apply the formulas that you want, for this

    'example I simply divide by 4)

    Dim ResizedImageBounds As New RectangleF(0, 0, MaxPictureSize, originalBounds.Height / (originalBounds.Width / MaxPictureSize))

    Dim ResizedImage As New Bitmap(CInt(ResizedImageBounds.Width), CInt(ResizedImageBounds.Height))

    'Get a graphics object for the bitmap

    Dim g As Graphics = Graphics.FromImage(ResizedImage)

    'Apply high quality properties for the graphics object

    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality

    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic

    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality

    'Draw the original image scaled into the graphics object

    g.DrawImage(bitO, ResizedImageBounds, originalBounds, GraphicsUnit.Pixel)

    'Dispose the graphics object (free memory)

    g.Dispose()

    HttpContext.Current.Session(
    "CurrentImage") = ResizedImage

    Else

    HttpContext.Current.Session("CurrentImage") = bitO

    End If

    If (bitO.Width > 100) Then

    Dim pixel As System.Drawing.GraphicsUnit = System.Drawing.GraphicsUnit.Pixel

    originalBounds = bitO.GetBounds(pixel)

    Dim thumbnailBounds As New RectangleF(0, 0, 100, originalBounds.Height / (originalBounds.Width / 100))

    Dim thumbnailImage As New Bitmap(CInt(thumbnailBounds.Width), CInt(thumbnailBounds.Height))

    'Get a graphics object for the bitmap

    Dim g2 As Graphics = Graphics.FromImage(thumbnailImage)

    'Apply high quality properties for the graphics object

    g2.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality

    g2.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic

    g2.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality

    'Draw the original image scaled into the graphics object

    g2.DrawImage(bitO, thumbnailBounds, originalBounds, GraphicsUnit.Pixel)

    'Dispose the graphics object (free memory)

    g2.Dispose()

    HttpContext.Current.Session("thumbnailImage") = thumbnailImage ' Notify the user that the file was uploaded successfully.

    Else

    HttpContext.Current.Session("thumbnailImage") = bitO ' Notify the user that the file was uploaded successfully.

    End If

    goodv = True

    Else

    errorit = "Not Correct File Type"

    End If

    Else

    errorit = "File Too Big"

    End If

    Else

    errorit = "No file to download"

    End If

    Catch ex As Exception

    errorit = ex.Message

    End Try

    Else

    errorit = "Some Mjor Error"

    End If

     

    this is the procedure that then takes it from the session to save it to disk:

    Dim FilePathAddedValue, FilePathAddedValue2nd As String

    Dim MaxPictureSize As Long

    Dim ThumbNailSize As Long = 100

    Dim CurrentPicID As Long

    Dim ProgramedPath As String

    Dim dirPath As String

    Dim fileName As String

    Dim fileUrl As String

    Dim fileExtension As String

    If Not IsNothing(Session("CurrentImage")) Then

    Try

     

    ' Append the name of the uploaded file to the path.

    CurrentPicID = BCWeed.Weedbook.BLL.Albums.Photo.GetNewPhotoID(AlbumID, FrameID)

    ProgramedPath = BCWeed.Weedbook.Globals.Settings.albums.PhotoAlbumDirectory

    Dim CurrentPicSavePath As String = "images/" + ProgramedPath '"OriginalPic/" 'Globals.Settings.Pagents.PagentDirectory

    Dim dirUrl As String = "~/" + CurrentPicSavePath 'TryCast(page__1, BCWeed.Weedbook.UI.BasePage).BaseUrl + CurrentPicSavePath 'page__1.BaseUrl + CurrentPicSavePath

    Dim i As System.Drawing.Image = Session("CurrentImage")

    dirPath = Server.MapPath(dirUrl + "OriginalPic/")

    ' if not already present, create a directory named "images/"

    If Not Directory.Exists(dirPath) Then

    Directory.CreateDirectory(dirPath)

    End If

    ' save the file under the specified folder

    fileExtension = Session("CurrentFileExtention")

    fileName = CurrentPicID.ToString + fileExtension

    fileUrl = dirUrl +
    "OriginalPic/" + fileName

    Dim ImFormat As System.Drawing.Imaging.ImageFormat

    Dim ContentType As String = ""

    Select Case fileExtension

    Case ".jpg"

    ImFormat = System.Drawing.Imaging.ImageFormat.Jpeg

    ContentType = "image/JPEG"

    Case ".jpeg"

    ImFormat = System.Drawing.Imaging.ImageFormat.Jpeg

    ContentType = "image/JPEG"

    Case ".png"

    ImFormat = System.Drawing.Imaging.ImageFormat.Png

    ContentType = "image/PNG"

    Case ".gif"

    ImFormat = System.Drawing.Imaging.ImageFormat.Gif

    ContentType = "image/GIF"

    End Select

    i.Save(dirPath + fileName, ImFormat)

    BCWeed.Weedbook.BLL.Albums.Photo.UpdateNewPhoto(AlbumID, FrameID, CurrentPicID, Title, description, Sdescription, fileName)

    'Repeat for the thumnail

    i = Session("thumbnailImage")

    dirPath = HttpContext.Current.Server.MapPath(dirUrl + "ThumbNails/")

    ' if not already present, create a directory named /Uploads/<CurrentUserName>

    If Not Directory.Exists(dirPath) Then

    Directory.CreateDirectory(dirPath)

    End If

    fileName = CurrentPicID.ToString + fileExtension

    fileUrl = dirUrl +
    "ThumbNails/" + fileName

    i.Save(dirPath + fileName, ImFormat)

    i.Dispose()

    Return "good"

    Catch ex As Exception

    Return ex.Message

    End Try

    Else

    Return "Bad session"

    End If

     

    I tried saving it both ways like that:

    i.Save(dirPath + fileName, ImFormat)

    and like that

    i.Save(dirPath + fileName)

     

    what Am I doing wrong?

    Thursday, February 19, 2009 4:28 PM

Answers