A generic error occurred in GDI+

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sandeepg
    New Member
    • Feb 2010
    • 1

    A generic error occurred in GDI+

    Dear Peter

    I am facing problem in saving JPEG file as follows as

    Code:
    System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
       At System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
       At System.Drawing.Image.Save(String filename, ImageFormat format).
    Error is occuring at line
    ISave.Save(Temp FileName, ImageFormat.Jpe g)

    I have tried a lot to remove this error, pls suggest me ways to remove the error as this is very urgent at my workplace

    Following is the code where this error occurs

    Code:
    Try
            Dim iProperty As Integer = (PropertyID)
            Dim TempFileName As String 
                    TempFileName = txtSelImagePath.Text
            Dim iOriginal As Image
                    IOriginal = Image.FromFile(TempFileName)
            Dim ifOriginal As ImageFormat = iOriginal.RawFormat
            'Get the property item
            Dim propItem As PropertyItem = iOriginal.GetPropertyItem(iProperty)
            'Set the property item
            Dim OriginalValue As String
            'Get the string value of the property
            Dim sPropValue As String = System.Text.Encoding.UTF8.GetString(propItem.Value)
            OriginalValue = sPropValue
            'Change the value
            SPropValue = sPropValue.Replace(OriginalValue, NewValue)
            'Set the value on the property
            PropItem.Value = System.Text.Encoding.UTF8.GetBytes(sPropValue)
            'Set the property on the image
            IOriginal.SetPropertyItem(propItem)
            'Store the list of properties that exist on the image
            Dim alPropertyItems As New ArrayList()
            For Each pi As PropertyItem In iOriginal.PropertyItems
                    AlPropertyItems.Add(pi)
            Next
            'Create temp image
            Dim iSave As Image = New Bitmap(iOriginal.Width, iOriginal.Height)
            'Copy the original image over to the temp image
            Dim gSave As Graphics = Graphics.FromImage(iSave)
            'If you check iSave at this point, it does not have any EXIF properties -
            'only the image gets recreated
            GSave.DrawImage(iOriginal, 0, 0, iOriginal.Width, iOriginal.Height)
            'Get rid of the locks on the original image
            GSave.Dispose()
            IOriginal.Dispose()
    
            'Copy the original EXIF properties to the new image
            For Each pi As PropertyItem In alPropertyItems
                ISave.SetPropertyItem(pi)
            Next
            'Save the temp image over the original image
            ISave.Save(TempFileName, ImageFormat.Jpeg)
            ISave.Dispose()
    
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try



    Thanks & Regards
    Sandeep
    Last edited by tlhintoq; Feb 1 '10, 01:35 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    Working...