-
Jan 13th, 2021, 11:59 PM
#1
Thread Starter
Member
[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 add As String
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()
add = OpenFileDialog1.FileName
Try
PictureBox1.BackgroundImage = Image.FromFile(add)
Catch ex As Exception
MsgBox(ex.Message)
End Try
BitmapO = New Bitmap(add)
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+"
Last edited by dday9; Jan 14th, 2021 at 10:52 AM.
< / L R. . . . >
-
Jan 14th, 2021, 12:35 AM
#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.
With regards to your question...
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 14th, 2021, 02:17 AM
#3
Thread Starter
Member
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.
With regards to your question...
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()
add = OpenFileDialog1.FileName
Try
PictureBox1.BackgroundImage = Image.FromFile(add)
Catch ex As Exception
MsgBox(ex.Message)
End Try
BitmapO = New Bitmap(add)
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
< / L R. . . . >
-
Jan 14th, 2021, 02:24 AM
#4
Thread Starter
Member
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 add As String
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()
add = OpenFileDialog1.FileName
Try
PictureBox1.BackgroundImage = Image.FromFile(add)
Catch ex As Exception
MsgBox(ex.Message)
End Try
BitmapO = New Bitmap(add)
End Sub
Error - "A Generic Error Occurred in GDI+"
< / L R. . . . >
-
Jan 14th, 2021, 03:47 AM
#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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 14th, 2021, 04:20 AM
#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 add As String
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()
add = OpenFileDialog1.FileName
Try
PictureBox1.BackgroundImage = Image.FromFile(add)
Catch ex As Exception
MsgBox(ex.Message)
End Try
BitmapO = New Bitmap(add)
End Sub
-
Jan 14th, 2021, 04:55 AM
#7
Thread Starter
Member
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
< / L R. . . . >
-
Jan 14th, 2021, 04:57 AM
#8
Thread Starter
Member
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 add As String
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()
add = OpenFileDialog1.FileName
Try
PictureBox1.BackgroundImage = Image.FromFile(add)
Catch ex As Exception
MsgBox(ex.Message)
End Try
BitmapO = New Bitmap(add)
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
< / L R. . . . >
-
Jan 14th, 2021, 05:12 AM
#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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 14th, 2021, 05:15 AM
#10
Re: A Generic Error Occurred In GDI+
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 14th, 2021, 05:24 AM
#11
Thread Starter
Member
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 !
< / L R. . . . >
-
Jan 14th, 2021, 05:25 AM
#12
Thread Starter
Member
Re: A Generic Error Occurred In GDI+
Originally Posted by .paul.
okay , will try that and then confirm
< / L R. . . . >
-
Jan 14th, 2021, 05:39 AM
#13
Thread Starter
Member
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
< / L R. . . . >
-
Jan 14th, 2021, 05:47 AM
#14
Re: A Generic Error Occurred In GDI+
You don't need the line numbers. Your code (in your VS editor) does have indentation
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 14th, 2021, 06:26 AM
#15
Thread Starter
Member
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 ?
< / L R. . . . >
-
Jan 14th, 2021, 06:27 AM
#16
Thread Starter
Member
Re: A Generic Error Occurred In GDI+
Somebody Help me ->>>>>
< / L R. . . . >
-
Jan 14th, 2021, 06:33 AM
#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..
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 14th, 2021, 08:26 AM
#18
Thread Starter
Member
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
< / L R. . . . >
-
Jan 14th, 2021, 09:11 AM
#19
Thread Starter
Member
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 >>
< / L R. . . . >
-
Jan 14th, 2021, 09:12 AM
#20
Thread Starter
Member
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 !!
< / L R. . . . >
-
Jan 14th, 2021, 11:23 AM
#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.
My usual boring signature: Nothing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|