Thread: [RESOLVED] A Generic Error Occurred In GDI+

1. [RESOLVED] A Generic Error Occurred In GDI+

Hi

Hope you all are doing great.

I am trying to do a comparison between to image files for some specific Pixl values. I have been successful in it however it runs in the first go ( first go: -that is uploading the image for the first time) and when i upload another image and try to run it again my code gives me an error " A Generic Error Occurred In GDI+".

below mention is my code.

Code:
Option Compare Binary
Option Explicit On
Option Infer On
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.IO

Public Class Form13
Dim BitmapO As Bitmap
Public Structure PixelStr
Public ColorO As Color  'Defines the pixel's color.
Public X As Integer     'Defines the pixel's horizontal position.
Public Y As Integer     'Defines the pixel's vertical position.
End Structure

Public Sub Main()
Dim Pixels As New List(Of PixelStr)
Dim pix1c1, pix1c2, pix1c3, pix2c1, pix2c2, pix2c3, A1, A2, A3 As Integer
Dim q As Integer
Dim files_image(BitmapO.Height * BitmapO.Width, 4) As Integer
For x As Integer = 0 To BitmapO.Width - 1
For y As Integer = 0 To BitmapO.Height - 1
Pixels.Add(New PixelStr With {.ColorO = BitmapO.GetPixel(x, y), .x = x, .y = y})
Next y
Next x
Using FileO As New System.IO.StreamWriter("D:\Codinates_circle.txt")
For Each Pixel As PixelStr In Pixels
With Pixel
files_image(q, 0) = .X
files_image(q, 1) = .Y
files_image(q, 2) = .ColorO.R
files_image(q, 3) = .ColorO.G
files_image(q, 4) = .ColorO.B
q = q + 1
End With
Next Pixel
End Using

Using Filet As New System.IO.StreamWriter("D:\Codinates_circle4.txt")
For j As Integer = 0 To BitmapO.Height * BitmapO.Width
If j < BitmapO.Height * BitmapO.Width - 50 Then
pix1c1 = files_image(j, 2)
pix1c2 = files_image(j, 3)
pix1c3 = files_image(j, 4)

End If

If pix1c1 = 183 or pix1c1=197 Then
BitmapO.SetPixel(files_image(j, 0), files_image(j, 1), Color.DarkRed)
Filet.WriteLine("{0};{1}", files_image(j, 0), files_image(j, 1))
End If
Next
End Using
Try
BitmapO.Save("D:\maw243.jpg")
Catch ex As Exception
MsgBox("Sorry ! We ran into an error.")
End Try
AI_Theme_Form4.Show()
AI_Theme_Form4.PictureBox2.BackgroundImage = Image.FromFile("D:\maw243.jpg")
AI_Theme_Form4.PictureBox2.BackgroundImageLayout = ImageLayout.Stretch
BitmapO.Dispose()
End Sub

Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
OpenFileDialog1.ShowDialog()
Try
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

I have tried to put everything in the code - it works but only in the first time execution but when i run it again it does not work "A Generic Error Occured in GDI+"

2. Re: A Generic Error Occurred In GDI+

You need to edit your code and format it with proper indentation. You can do that by selecting your code block in the editor, and clicking the # button above the text editor.

This code...

Code:
Dim img As New Bitmap("C:\Users\Paul\Desktop\it-could-be-you.jpg")
img.Save("C:\Users\Paul\Desktop\it-could-be-you.jpg", Drawing.Imaging.ImageFormat.Jpeg)
Results in the error, A generic error occurred in GDI+.

But this code...

Code:
Dim img As New Bitmap("C:\Users\Paul\Desktop\it-could-be-you.jpg")
Dim img2 As New Bitmap(img)
img.Dispose()
img2.Save("C:\Users\Paul\Desktop\it-could-be-you.jpg", Drawing.Imaging.ImageFormat.Jpeg)
Works without error because the file is no longer open. A file displayed in a PictureBox with Image.FromFile is still open

3. Re: A Generic Error Occurred In GDI+

Originally Posted by .paul.
You need to edit your code and format it with proper indentation. You can do that by selecting your code block in the editor, and clicking the # button above the text editor.

This code...

Code:
Dim img As New Bitmap("C:\Users\Paul\Desktop\it-could-be-you.jpg")
img.Save("C:\Users\Paul\Desktop\it-could-be-you.jpg", Drawing.Imaging.ImageFormat.Jpeg)
Results in the error, A generic error occurred in GDI+.

But this code...

Code:
Dim img As New Bitmap("C:\Users\Paul\Desktop\it-could-be-you.jpg")
Dim img2 As New Bitmap(img)
img.Dispose()
img2.Save("C:\Users\Paul\Desktop\it-could-be-you.jpg", Drawing.Imaging.ImageFormat.Jpeg)
Works without error because the file is no longer open. A file displayed in a PictureBox with Image.FromFile is still open

Ummmm I tried the way you told me but still not working.

I think you are missing a point that i am uploading an image via picture_box3 - the address of image file is in variable "add". see below

Code:
Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
OpenFileDialog1.ShowDialog()
Try
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

after that i am also editing the image basis on pixcel information - see below

Code:
If pix1c1 = 183 or pix1c1=197 Then
BitmapO.SetPixel(files_image(j, 0), files_image(j, 1), Color.DarkRed)
Filet.WriteLine("{0};{1}", files_image(j, 0), files_image(j, 1))
End If
Next
End Using
Try
BitmapO.Save("D:\maw243.jpg")
Catch ex As Exception
MsgBox("Sorry ! We ran into an error.")
End Try

And now when i applied your method it is showing the non-edited picture as uploaded not the one that is processed

Can you help me ? -be a bit more specific to what i am doing wrong and how to fix it

4. Re: A Generic Error Occurred In GDI+

As you suggested here is my code again with proper indentation >

Code:
Option Compare Binary
Option Explicit On
Option Infer On
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.IO

Public Class Form13
Dim BitmapO As Bitmap
Public Structure PixelStr
Public ColorO As Color 'Defines the pixel's color.
Public X As Integer 'Defines the pixel's horizontal position.
Public Y As Integer 'Defines the pixel's vertical position.
End Structure

Public Sub Main()
Dim Pixels As New List(Of PixelStr)
Dim pix1c1, pix1c2, pix1c3, pix2c1, pix2c2, pix2c3, A1, A2, A3 As Integer
Dim q As Integer
Dim files_image(BitmapO.Height * BitmapO.Width, 4) As Integer
For x As Integer = 0 To BitmapO.Width - 1
For y As Integer = 0 To BitmapO.Height - 1
Pixels.Add(New PixelStr With {.ColorO = BitmapO.GetPixel(x, y), .x = x, .y = y})
Next y
Next x
Using FileO As New System.IO.StreamWriter("D:\Codinates_circle.txt")
For Each Pixel As PixelStr In Pixels
With Pixel
files_image(q, 0) = .X
files_image(q, 1) = .Y
files_image(q, 2) = .ColorO.R
files_image(q, 3) = .ColorO.G
files_image(q, 4) = .ColorO.B
q = q + 1
End With
Next Pixel
End Using

Using Filet As New System.IO.StreamWriter("D:\Codinates_circle4.txt")
For j As Integer = 0 To BitmapO.Height * BitmapO.Width
If j < BitmapO.Height * BitmapO.Width - 50 Then
pix1c1 = files_image(j, 2)
pix1c2 = files_image(j, 3)
pix1c3 = files_image(j, 4)

End If

If pix1c1 = 183 or pix1c1=197 Then
BitmapO.SetPixel(files_image(j, 0), files_image(j, 1), Color.DarkRed)
Filet.WriteLine("{0};{1}", files_image(j, 0), files_image(j, 1))
End If
Next
End Using
Try
BitmapO.Save("D:\maw243.jpg")
Catch ex As Exception
MsgBox("Sorry ! We ran into an error.")
End Try
AI_Theme_Form4.Show()
AI_Theme_Form4.PictureBox2.BackgroundImage = Image.FromFile("D:\maw243.jpg")
AI_Theme_Form4.PictureBox2.BackgroundImageLayout = ImageLayout.Stretch
BitmapO.Dispose()
End Sub

Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
OpenFileDialog1.ShowDialog()
Try
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Error - "A Generic Error Occurred in GDI+"

5. Re: A Generic Error Occurred In GDI+

Code:
Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
BitmapO = New Bitmap(OpenFileDialog1.FileName)
PictureBox1.BackgroundImage = BitmapO
BitmapO.Dispose  ' If this isn't disposed at some point the file specified in OpenFileDialog1.FileName remains open
End If
End Sub

6. Re: A Generic Error Occurred In GDI+

Originally Posted by lokesh R
As you suggested here is my code again with proper indentation >
Really, because I see no indentation at all. It looks like you copied the non-indented code from your rendered post and expected it to magically become indented. Maybe use the original code that actually contains the indentation if you want to display indentation.
vb.net Code:
Option Compare Binary
Option Explicit On
Option Infer On
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.IO
Public Class Form13
Dim BitmapO As Bitmap
Public Structure PixelStr
Public ColorO As Color  'Defines the pixel's color.
Public X As Integer     'Defines the pixel's horizontal position.
Public Y As Integer     'Defines the pixel's vertical position.
End Structure
Public Sub Main()
Dim Pixels As New List(Of PixelStr)
Dim pix1c1, pix1c2, pix1c3, pix2c1, pix2c2, pix2c3, A1, A2, A3 As Integer
Dim q As Integer
Dim files_image(BitmapO.Height * BitmapO.Width, 4) As Integer
For x As Integer = 0 To BitmapO.Width - 1
For y As Integer = 0 To BitmapO.Height - 1
Pixels.Add(New PixelStr With {.ColorO = BitmapO.GetPixel(x, y), .x = x, .y = y})
Next y
Next x
Using FileO As New System.IO.StreamWriter("D:\Codinates_circle.txt")
For Each Pixel As PixelStr In Pixels
With Pixel
files_image(q, 0) = .X
files_image(q, 1) = .Y
files_image(q, 2) = .ColorO.R
files_image(q, 3) = .ColorO.G
files_image(q, 4) = .ColorO.B
q = q + 1
End With
Next Pixel
End Using
Using Filet As New System.IO.StreamWriter("D:\Codinates_circle4.txt")
For j As Integer = 0 To BitmapO.Height * BitmapO.Width
If j < BitmapO.Height * BitmapO.Width - 50 Then
pix1c1 = files_image(j, 2)
pix1c2 = files_image(j, 3)
pix1c3 = files_image(j, 4)
End If
If pix1c1 = 183 or pix1c1=197 Then
BitmapO.SetPixel(files_image(j, 0), files_image(j, 1), Color.DarkRed)
Filet.WriteLine("{0};{1}", files_image(j, 0), files_image(j, 1))
End If
Next
End Using
Try
BitmapO.Save("D:\maw243.jpg")
Catch ex As Exception
MsgBox("Sorry ! We ran into an error.")
End Try
AI_Theme_Form4.Show()
AI_Theme_Form4.PictureBox2.BackgroundImage = Image.FromFile("D:\maw243.jpg")
AI_Theme_Form4.PictureBox2.BackgroundImageLayout = ImageLayout.Stretch
BitmapO.Dispose()
End Sub
Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
OpenFileDialog1.ShowDialog()
Try
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

7. Re: A Generic Error Occurred In GDI+

Originally Posted by .paul.
Code:
Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
BitmapO = New Bitmap(OpenFileDialog1.FileName)
PictureBox1.BackgroundImage = BitmapO
BitmapO.Dispose  ' If this isn't disposed at some point the file specified in OpenFileDialog1.FileName remains open
End If
End Sub

NO - Absolutely not >> This is not the case, may be I have not briefed it. You see the things is there are two pictureboxes

one picturbox uploads the picture and second picturebox show the processed image okay, so i have put try ..catch in both and the error is coming up in the second picturebox that is supposed to be showing the processed image (edited one) and the uploder still works well it shows another image in it. But the error comes in the saving of processing image .

If you run my code and number the try -catch errors like error1 and 2 instead of exception.message then you will see the error is in the processed image that needs to be saved and then shown in the second box.

despite that I have also put your code and it does not work

8. Re: A Generic Error Occurred In GDI+

Originally Posted by jmcilhinney
Really, because I see no indentation at all. It looks like you copied the non-indented code from your rendered post and expected it to magically become indented. Maybe use the original code that actually contains the indentation if you want to display indentation.
vb.net Code:
Option Compare Binary
Option Explicit On
Option Infer On
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.IO
Public Class Form13
Dim BitmapO As Bitmap
Public Structure PixelStr
Public ColorO As Color  'Defines the pixel's color.
Public X As Integer     'Defines the pixel's horizontal position.
Public Y As Integer     'Defines the pixel's vertical position.
End Structure
Public Sub Main()
Dim Pixels As New List(Of PixelStr)
Dim pix1c1, pix1c2, pix1c3, pix2c1, pix2c2, pix2c3, A1, A2, A3 As Integer
Dim q As Integer
Dim files_image(BitmapO.Height * BitmapO.Width, 4) As Integer
For x As Integer = 0 To BitmapO.Width - 1
For y As Integer = 0 To BitmapO.Height - 1
Pixels.Add(New PixelStr With {.ColorO = BitmapO.GetPixel(x, y), .x = x, .y = y})
Next y
Next x
Using FileO As New System.IO.StreamWriter("D:\Codinates_circle.txt")
For Each Pixel As PixelStr In Pixels
With Pixel
files_image(q, 0) = .X
files_image(q, 1) = .Y
files_image(q, 2) = .ColorO.R
files_image(q, 3) = .ColorO.G
files_image(q, 4) = .ColorO.B
q = q + 1
End With
Next Pixel
End Using
Using Filet As New System.IO.StreamWriter("D:\Codinates_circle4.txt")
For j As Integer = 0 To BitmapO.Height * BitmapO.Width
If j < BitmapO.Height * BitmapO.Width - 50 Then
pix1c1 = files_image(j, 2)
pix1c2 = files_image(j, 3)
pix1c3 = files_image(j, 4)
End If
If pix1c1 = 183 or pix1c1=197 Then
BitmapO.SetPixel(files_image(j, 0), files_image(j, 1), Color.DarkRed)
Filet.WriteLine("{0};{1}", files_image(j, 0), files_image(j, 1))
End If
Next
End Using
Try
BitmapO.Save("D:\maw243.jpg")
Catch ex As Exception
MsgBox("Sorry ! We ran into an error.")
End Try
AI_Theme_Form4.Show()
AI_Theme_Form4.PictureBox2.BackgroundImage = Image.FromFile("D:\maw243.jpg")
AI_Theme_Form4.PictureBox2.BackgroundImageLayout = ImageLayout.Stretch
BitmapO.Dispose()
End Sub
Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
OpenFileDialog1.ShowDialog()
Try
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
I am sorry !! may be i do not know the indentation word meaning >> are your referring to the tabbed code as in visual basic 2010 with "-" sign on the left

9. Re: A Generic Error Occurred In GDI+

Look at the difference between your code and everyone else’s code in this thread. Indented code is what you see here and also in your VS editor

10. Re: A Generic Error Occurred In GDI+

https://docs.microsoft.com/en-us/dot...d?view=net-5.0

11. Re: A Generic Error Occurred In GDI+

Originally Posted by .paul.
Look at the difference between your code and everyone else’s code in this thread. Indented code is what you see here and also in your VS editor
okay , got it the indexes along with the lines of the code. Will take care from now on !

12. Re: A Generic Error Occurred In GDI+

Originally Posted by .paul.

https://docs.microsoft.com/en-us/dot...d?view=net-5.0
okay , will try that and then confirm

13. Re: A Generic Error Occurred In GDI+

Originally Posted by lokesh R
okay , will try that and then confirm
I tried , but the error comes first and it does not work. And one more thing do you know how to enable the indexes in VS-2010 I have attached a screenshot i do not think that my Visual Studio 2010 has indentation .Attachment 179865

14. Re: A Generic Error Occurred In GDI+

You don't need the line numbers. Your code (in your VS editor) does have indentation

15. Re: A Generic Error Occurred In GDI+

Originally Posted by .paul.
You don't need the line numbers. Your code (in your VS editor) does have indentation
Yes, I got it - VS2010 does not do auto intent but i have enabled them in Tools-> Text Editor->Basic->Editor->Indent Type="Smart"-> Interaction ="Line Numbers"

its good to know information will help me to put it on the form with indent !

But Still I have not figured what to do with that GDI+ Error ?

16. Re: A Generic Error Occurred In GDI+

Somebody Help me ->>>>>

17. Re: A Generic Error Occurred In GDI+

I told you. Something in your application has opened your file and it remains open, thereby locking the file..

18. Re: A Generic Error Occurred In GDI+

Originally Posted by .paul.
I told you. Something in your application has opened your file and it remains open, thereby locking the file..
I have been redefining it but no luck !!! can someone please fix it

19. Re: A Generic Error Occurred In GDI+

Originally Posted by lokesh R
I have been redefining it but no luck !!! can someone please fix it
Got it . I was not able to close a form and the other picturebox was on this second form this was not getting dispossed. Now it is working fine.

Thank you Paul >>

20. Re: A Generic Error Occurred In GDI+

Originally Posted by .paul.
I told you. Something in your application has opened your file and it remains open, thereby locking the file..
Thank you !!

21. Re: [RESOLVED] A Generic Error Occurred In GDI+

Indenting on this forum is a bit of magic. If you copy indented form from VS, sure, THEN you get indenting, but you can do less than that, too. If you write freehand into a post, then a single space will tend to be extended into a tab when inside the [CODE][/CODE] tags. I've made a fair amount of use of that, though I've never really taken any time to understand it completely.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•