I am facing problem in saving JPEG file as follows as
Expand|Select|Wrap|Line Numbers
- 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).
ISave.Save(TempFileName, ImageFormat.Jpeg)
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
Expand|Select|Wrap|Line Numbers
- 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