473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

A generic error occurred in GDI+.

!NoItAll
297 100+
Got a tough problem I need to solve...
I am getting the following error whenever I create a copy of a TIF image and try to save it.

A generic error occurred in GDI+.

Here is my code
Expand|Select|Wrap|Line Numbers
  1.        If My.Computer.FileSystem.FileExists(Filename) Then
  2.  
  3.             Dim MyImage As Image = Image.FromFile(Filename)
  4.             Dim Prop As System.Drawing.Imaging.PropertyItem
  5.             Dim enc As New System.Text.ASCIIEncoding()
  6.             Dim I As Integer = 0
  7.  
  8.             For Each Prop In MyImage.PropertyItems
  9.                 If Prop.Id = 34849 Or I = (MyImage.PropertyItems.Count - 1) Then
  10.                     Exit For
  11.                     'Dim a As String = enc.GetString(Prop.Value)
  12.                 End If
  13.                 I += 1
  14.  
  15.             Next
  16.  
  17.             Prop = MyImage.PropertyItems(I)
  18.             Dim sMetaData As New String("<wxcXMLMarker>")
  19.             Prop.Type = Prop.Type
  20.             Prop.Len = CInt(sMetaData.Length + 1)
  21.             Prop.Id = CInt(34849)
  22.             Prop.Value = StringToByte(sMetaData)
  23.             MyImage.SetPropertyItem(Prop)
  24.  
  25.             If My.Computer.FileSystem.FileExists("e:\test.tif") Then
  26.                 My.Computer.FileSystem.DeleteFile("e:\test.tif")
  27.             End If
  28.             'Dim MyImageCopy As Image = MyImage.Clone
  29.             'MyImage.Dispose()
  30.             MyImage.Save("e:\test.tif", System.Drawing.Imaging.ImageFormat.Tiff)
  31.             MyImage.Dispose()
  32.         Else
  33.             Throw New ApplicationException("File Does Not Exist")
  34.         End If
  35.  
As you can see I did try to clone the image in case there were some locks on the original one. When I tried cloning I did change the MyImage.Save method to MyImageCopy.Save - but still the same error:

A generic error occurred in GDI+.

Grrrr...

Here is the last few items on the stack trace...

Expand|Select|Wrap|Line Numbers
  1.    at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
  2.    at System.Drawing.Image.Save(String filename, ImageFormat format)
  3.    at WXC.SceneOpener.GetTIF(String Filename) in C:\Documents and Settings\ddesjardins\My Documents\Visual Studio 2010\Projects\SceneOpener\SceneOpener\SceneOpener.vb:line 261
  4.  
Notice that there was a second call to the save method looking for encoder parameters. This must be happening internally - and perhaps there are some perameters that are null -...


Basically I need to read the image in - replace some meta data (metadata with ID 34849) which will always be in the images my code is dealing with.
Any help would be greatly appreciated!
May 3 '11 #1
3 10266
!NoItAll
297 100+
OK - I think I see the problem - but I'm still not sure how to fix it.
It turns out to be that the TIFF files being generated by our application (they are just large thumbnails) are putting some garbage into the horizontalresolution and verticalresolution properties. Unfortunately these are readonly properties and I can not see how to reset them to something legal (like 72 and 72).
Any ideas...
May 3 '11 #2
!NoItAll
297 100+
Yup - I wound up having to recreate the image with proper settings for horizontalresolution and verticalresolution.
Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Function fixit(byVal original as Bitmap) as Bitmap
  3. Dim newImage As New Bitmap(original, original.Width, original.Height)
  4.         newImage.SetResolution(72, 72)   '<-- this is where I set the proper V&H Resolution
  5.         Dim g As Graphics = Graphics.FromImage(newImage)
  6.         Dim rect As System.Drawing.Rectangle
  7.         rect.Height = original.Height
  8.         rect.Width = original.Width
  9.         g.DrawImageUnscaledAndClipped(original, rect)
  10.  
  11.         g.Dispose()
  12.         Return newImage
  13. End function
  14.  
all better now...
May 8 '11 #3
I am also faced this error. This happens when we load an image with .fromfile function. It keeps a lock on that. I solved this issue by using streams. My application needed an image to be loaded in a pic box and user will draw something on that and save back to file.

Loading to pic box
Expand|Select|Wrap|Line Numbers
  1. Dim img As Byte(), ms As MemoryStream
  2. img = System.IO.File.ReadAllBytes(FilenameWithFullPath)
  3.                         ms = New System.IO.MemoryStream(img)
  4.                         picCS.Image = Image.FromStream(ms)
Saving back to file
Expand|Select|Wrap|Line Numbers
  1. Dim ImageStream As MemoryStream, ImageData As Byte()ImageStream = New MemoryStream()
  2.                             picCS.Image.Save(ImageStream, ImageFormat.Jpeg)
  3.                             picCS.Image.Dispose()
  4.                             ImageData = ImageStream.ToArray
  5.                             ImageStream.Dispose()
  6.  
  7. System.IO.File.WriteAllBytes(filepath, ImageData)
  8.  
Aug 30 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Alphonse Giambrone | last post by:
I am currently reading 'Programming The Web with Visual Basic .NET' and have so far found it to be excellent. Downloaded all the code from Apress and working in chapter 4, I get the error shown...
2
by: Tim::.. | last post by:
Hi can someone please tell me why I keep getting the following error and how I might fix it ...::ERROR::. Exception Details: System.Runtime.InteropServices.ExternalException: A generic error...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
0
by: news.microsoft.com | last post by:
Hi guys, This text looks long and complicate but it is not, and I really really need some help here. For the last 24hs I'm trying to resolve this issue (last night I dreamed
0
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
I have a bitmap which I have retreived through the WebBrowser DrawToBitmap method into a Bitmap. When I try to save the bitmap I get "ExternalException occurred A generic error occurred in...
7
by: j4richard | last post by:
Help please, I am getting this "Unhandled Exception has occurred in your application" " A Generic error occurred in GDI+" See the end of this message for details on...
1
by: Jeff | last post by:
hi asp.net 2.0 I'm experimenting with GDI+ in asp.net and get an "A generic error occurred in GDI+." exception. Below is my code, I've marked a line with "<<<<-- here". It's here the...
3
by: uday1302 | last post by:
Hi Dear, Here I am trying to upload a photo. protected void LoadImage() { string UserName = Session.ToString(); byte Data = Profile.GetImageData(UserName);...
1
by: sandeepg | last post by:
Dear Peter I am facing problem in saving JPEG file as follows as System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. At System.Drawing.Image.Save(String...
0
by: RobT | last post by:
So here's the problem- My app has an image in it in a picture box. I need the end user to be able to change it. So the code I added below should do that. However, I get the "A generic error...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.