Answered by:
help resizing an uploaded image and saving it. Getting "a generic GDI error occured" message

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 IntegerUpLoadTriggerv = 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 StringfileExtension = 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) ThenFileOK =
True End If Next ElseFileOK =
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.ImageFormatHttpContext.Current.Session(
"CurrentFileExtention") = fileExtension Dim ContentType As String = ""HttpContext.Current.Session(
"CurrentImageFormat") = ImFormat If (bitO.Width > MaxPictureSize) And (MaxPictureSize <> 0) Then Dim pixel As System.Drawing.GraphicsUnit = System.Drawing.GraphicsUnit.PixeloriginalBounds = 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 objectg.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 objectg.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.PixeloriginalBounds = 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 objectg2.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 objectg2.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. ElseHttpContext.Current.Session(
"thumbnailImage") = bitO ' Notify the user that the file was uploaded successfully. End Ifgoodv =
True Elseerrorit =
"Not Correct File Type" End If Elseerrorit =
"File Too Big" End If Elseerrorit =
"No file to download" End If Catch ex As Exceptionerrorit = ex.Message
End Try Elseerrorit =
"Some Mjor Error" End Ifthis 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 TryCurrentPicID = 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) ThenDirectory.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 Selecti.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) ThenDirectory.CreateDirectory(dirPath)
End IffileName = CurrentPicID.ToString + fileExtension
fileUrl = dirUrl + "ThumbNails/" + fileNamei.Save(dirPath + fileName, ImFormat)
i.Dispose()
Return "good" Catch ex As Exception Return ex.Message End Try Else Return "Bad session" End IfI 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
-
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, February 19, 2009 8:57 PM