Question A generic error occurred in GDI+.

sansalk

Member
Joined
Sep 28, 2011
Messages
8
Programming Experience
1-3
I am geting this error. I was trying do it many ways but coudnt find a solution please help me
In this code i am triying to save resized image for network folder. its works first time onece i tryto update the picture its giving "A generic error occurred in GDI+." error
Plz find the attachment
Private Function ResiszeImage(ByVal strFilepath As String, ByVal strEMPNo As String)
'At the top of your code

Dim bm As New Bitmap(strFilepath)
Dim width As Integer = PicBoxPickers.Width
Dim height As Integer = PicBoxPickers.Height
Dim thumb As New Bitmap(width, height)
Dim FilePath As String = "\\192.168.1.41\media\photo\systemphotos\"
Dim UserAccount As String = "TDL-Sanjaya"
Dim oFolderInfo As IO.DirectoryInfo = New IO.DirectoryInfo(FilePath)
Dim oFolderAcl As New DirectorySecurity(FilePath, System.Security.AccessControl.AccessControlSections.Access)
oFolderAcl.AddAccessRule(New FileSystemAccessRule(UserAccount, _
FileSystemRights.FullControl, _
AccessControlType.Allow))
oFolderInfo.SetAccessControl(oFolderAcl)
oFolderAcl = Nothing
oFolderInfo = Nothing
If Directory.Exists("\\192.168.1.41\media\photo\systemphotos\") = False Then
Call Directory.CreateDirectory("\\192.168.1.41\media\photo\systemphotos\")
End If
PicBoxPickers.Image = Nothing

Dim g As Graphics = Graphics.FromImage(thumb)
Try
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(bm, New Rectangle(0, 0, width, height), New Rectangle(0, 0, bm.Width, bm.Height), GraphicsUnit.Pixel)
g.Dispose()
bm.Dispose()
strFilepath = "\\192.168.1.41\media\photo\systemphotos\" + strEMPNo + ".jpg"
lblFilePath.Text = strFilepath

PicBoxPickers.Image = thumb
PicBoxPickers.Image.Save(strFilepath, ImageFormat.Jpeg) '
thumb.Dispose()
Return True
Catch ex As Exception
Return False
End Try
End Function
 

Attachments

  • Capture.JPG
    Capture.JPG
    84.1 KB · Views: 32
Yes I have permission for the folder, and even i can save pic by using same code for first time but when i was trying to do overwrite(update same) pic
this error comes out..
 
It sounds like the file is still open. You may have to set the image property of the picturebox control to Nothing for the control to release it. It is also possible that you may have to dispose the image property or even the control.
 
sansalk said:
PicBoxPickers.Image = thumb

thumb.Dispose()
You can't dispose the image that is used by the control. It will normally result in an ArgumentException (Parameter is not valid) at the moment control tries to refresh, but since you here get an ExternalException you are probably using a COM component.
PicBoxPickers.Image = Nothing
This is also not a good idea, dispose the image object before releasing last reference to it, if not you effectively have a memory leak and it could take a long time until finalizer releases unmanaged GDI resources for it.
 
Back
Top