Results 1 to 21 of 21

Thread: A generic error occurred in GDI+.

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    12

    Exclamation A generic error occurred in GDI+.

    Hi All

    Iam getting this error while Updating Picture box :

    c

    iam using stored proc :

    Code:
    ALTER procedure [dbo].[Update_Customer]
    
    	@CustomerID int output,
    	@CustomerName nvarchar (50),
    	@CustomerPhoto image,
    	@CustomerEmail nvarchar(Max),
    	@CustomerPhone1 nvarchar(12),
    	@CustomerPhone2 nvarchar(12),
    	@CustomerAddress nvarchar(Max),
    	@CustomerFax nvarchar(12),
    	@CustomerStatus bit,
    	@CountryID int,
    	@CityID int,
    	@Notes nvarchar (Max),
    	@ModifiedBy nvarchar (30)
    	
    
    as
    
        BEGIN
            update CustomersTbl set 
    		    CustomerID=@CustomerID,
    		    CustomerName=@CustomerName,
    			CustomerPhoto=@CustomerPhoto,
    			CustomerEmail=@CustomerEmail,
    			CustomerPhone1=@CustomerPhone1,
    			CustomerPhone2=@CustomerPhone2,
    			CustomerAddress=@CustomerAddress,
    			CustomerFax=@CustomerFax,
    			CustomerStatus=@CustomerStatus,
    			CountryID=@CountryID,
    			CityID=@CityID,
    			Notes=@Notes,
    			ModifiedDate=getdate(),
    			ModifiedBy=@ModifiedBy
    		where CustomerID=@CustomerID
        END

    In VS 2012 I have Two Layers

    Data Layer Code :

    Code:
            Friend Function Update_Customer(ByVal CustomerID As String, ByVal CustomerName As String, ByVal CustomerEmail As String, ByVal CustomerPhone1 As String, ByVal CustomerPhone2 As String, ByVal CustomerAddress As String, ByVal CustomerFax As String, ByVal CustomerStatus As Boolean, ByVal CountryID As Integer, ByVal CityID As Integer, ByVal Notes As String, ByVal ModifiedBy As String) As String
                Dim retval As String
                Dim cmd As New SqlCommand("Update_Customer")
                cmd.Parameters.AddWithValue("@CustomerID", CustomerID)
                cmd.Parameters.AddWithValue("@CustomerName", CustomerName)
                cmd.Parameters.AddWithValue("@CustomerPhoto", SqlDbType.Image).Value = imgToByteArray(FrmManageDetailsCustomers.PictureBox2.Image)
                cmd.Parameters.AddWithValue("@CustomerEmail", CustomerEmail)
                cmd.Parameters.AddWithValue("@CustomerPhone1", CustomerPhone1)
                cmd.Parameters.AddWithValue("@CustomerPhone2", CustomerPhone2)
                cmd.Parameters.AddWithValue("@CustomerAddress", CustomerAddress)
                cmd.Parameters.AddWithValue("@CustomerFax", CustomerFax)
                cmd.Parameters.AddWithValue("@CustomerStatus", CustomerStatus)
                cmd.Parameters.AddWithValue("@CountryID", CountryID)
                cmd.Parameters.AddWithValue("@CityID", CityID)
                cmd.Parameters.AddWithValue("@Notes", Notes)
                cmd.Parameters.AddWithValue("@ModifiedBy", ModifiedBy)
                retval = dm.executeNonQuery(cmd)
                Return retval
            End Function

    And Business Layer :

    Code:
            Public Function Update_Customer(ByVal CustomerID As String, ByVal CustomerName As String, ByVal CustomerEmail As String, ByVal CustomerPhone1 As String, ByVal CustomerPhone2 As String, ByVal CustomerAddress As String, ByVal CustomerFax As String, ByVal CustomerStatus As Boolean, ByVal CountryID As Integer, ByVal CityID As Integer, ByVal Notes As String, ByVal ModifiedBy As String) As String
                Dim retval As String
                retval = p.Update_Customer(CustomerID, CustomerName, CustomerEmail, CustomerPhone1, CustomerPhone2, CustomerAddress, CustomerFax, CustomerStatus, CountryID, CityID, Notes, ModifiedBy)
                Return retval
            End Function

    In Module I have this Code :

    Code:
        Public Function imgToByteArray(ByVal img As Image) As Byte()
            Using mStream As New MemoryStream()
                img.Save(mStream, img.RawFormat)
                Return mStream.ToArray()
            End Using
        End Function

    And the Last Code is for Update button :

    Code:
    Dim retval As String = p.Update_Customer(txtCustomerCode.Text, txtCustomerName.Text, txtCustomerEmail.Text, txtCustomerPhone1.Text, txtCustomerPhone2.Text, txtCustomerAddress.Text, txtCustomerFax.Text, CheckBox2.Checked, ComboCustomerCountry.SelectedValue, ComboCustomerCity.SelectedValue, txtCustomernote.Text, FrmMain.LblUserID.Text)

    The Error that iam getting is : A generic error occurred in GDI+.

  2. #2
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: A generic error occurred in GDI+.

    Might not be of help to you, but - first of all try your Update_Customer code with Using block. About your error - nothing in your code looks to me that you're dealing with grapchics. do you use your Picturebox for any kind of Painting. If so, then you should examine Paint event of Picturebox. MSDN actually recommends that you should use as less Picturebox as possible, specially when dealing with GDI+, use Label or something else instead.

    Also, your stored procedure - that is a snapshot from your DB right ? Stored procedure should be locatted there, you only call It from VB.NET. At least that is how I do in Oracle....

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: A generic error occurred in GDI+.

    How EXACTLY do you load the Image in the first place? One reason for this rather unhelpful error message is trying to save an Image that was loaded from a file without closing the file. For instance, the Image.FromFile method and the Bitmap constructor that takes a file path as an argument both keep the specified file open until the object is disposed.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    12

    Re: A generic error occurred in GDI+.

    Quote Originally Posted by jmcilhinney View Post
    How EXACTLY do you load the Image in the first place? One reason for this rather unhelpful error message is trying to save an Image that was loaded from a file without closing the file. For instance, the Image.FromFile method and the Bitmap constructor that takes a file path as an argument both keep the specified file open until the object is disposed.
    i load it from GridView


    Code:
    FrmManageDetailsCustomers.PictureBox2.Image = byteArrayToImage(MainCustomersGv.Rows(MainCustomersGv.CurrentRow.Index).Cells(2).Value)

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: A generic error occurred in GDI+.

    Quote Originally Posted by Salem141 View Post
    MainCustomersGv.Rows(MainCustomersGv.CurrentRow.Index)
    Um, what's the point of getting the current row, then getting its index, then using that index to get the row at that index? Isn't that just going to give you back the row you already had? If you want the current row then just stop at the current row, i.e. use 'MainCustomersGv.CurrentRow'.

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    12

    Re: A generic error occurred in GDI+.

    So what code i have to write instead of

    MainCustomersGv.Rows(MainCustomersGv.CurrentRow.Index)

    any help ??

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: A generic error occurred in GDI+.

    isntead of
    MainCustomersGv.Rows(MainCustomersGv.CurrentRow.Index).Cells(2).Value

    use
    MainCustomersGv.CurrentRow.Cells(2).Value

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: A generic error occurred in GDI+.

    Quote Originally Posted by Salem141 View Post
    So what code i have to write instead of

    MainCustomersGv.Rows(MainCustomersGv.CurrentRow.Index)

    any help ??
    Really?
    Quote Originally Posted by jmcilhinney View Post
    use 'MainCustomersGv.CurrentRow'.

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    12

    Re: A generic error occurred in GDI+.

    The same error iam getting

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: A generic error occurred in GDI+.

    That was just a general recommendation on code improvement, not a solution to this issue. The only specific reason for that particular error message that I'm aware of is obviously not in play on this occasion, so I'm at a loss. That said, you haven't shown us your 'byteArrayToImage' method, so maybe there's something in there.

  11. #11

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    12

    Re: A generic error occurred in GDI+.

    Quote Originally Posted by jmcilhinney View Post
    That was just a general recommendation on code improvement, not a solution to this issue. The only specific reason for that particular error message that I'm aware of is obviously not in play on this occasion, so I'm at a loss. That said, you haven't shown us your 'byteArrayToImage' method, so maybe there's something in there.

    This is

    Code:
        Public Function byteArrayToImage(ByVal byt As Byte()) As Image
            Dim ms As New System.IO.MemoryStream()
            Dim drwimg As Image = Nothing
            Try
                ms.Write(byt, 0, byt.Length)
                drwimg = New Bitmap(ms)
            Finally
                ms.Close()
            End Try
            Return drwimg
        End Function

  12. #12
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: A generic error occurred in GDI+.

    Quote Originally Posted by Salem141 View Post
    Code:
        Public Function byteArrayToImage(ByVal byt As Byte()) As Image
            Dim ms As New System.IO.MemoryStream()
            Dim drwimg As Image = Nothing
            Try
                ms.Write(byt, 0, byt.Length)
                drwimg = New Bitmap(ms)
            Finally
                ms.Close()
            End Try
            Return drwimg
        End Function
    That's probably half your problem right there: you are closing the memory stream immediately after you create the image and well before you dispose of the image.

    If you look at the documentation for the Bitmap Constructor that takes as Stream as an argument, you'll see that it says You must keep the stream open for the lifetime of the Bitmap.

    The solution to that would depend on whether it is important to maintain the original image in the database, or an image that pretty much looks like the original.

  13. #13
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: A generic error occurred in GDI+.

    Quote Originally Posted by Inferrd View Post
    That's probably half your problem right there: you are closing the memory stream immediately after you create the image and well before you dispose of the image.

    If you look at the documentation for the Bitmap Constructor that takes as Stream as an argument, you'll see that it says You must keep the stream open for the lifetime of the Bitmap.

    The solution to that would depend on whether it is important to maintain the original image in the database, or an image that pretty much looks like the original.
    Wow... interesting. So, if you're generating multiple images, you have to keep all of the streams for each one around? Surely there's a way to disconnect the image from the stream.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,758

    Re: A generic error occurred in GDI+.

    So, if you're generating multiple images, you have to keep all of the streams for each one around?
    To keep the original image you do, but you can always clone the image then close the stream and return the clone...
    VB.net Code:
    1. Private Function imageFromStream(ms As IO.Stream) As Image
    2.  
    3.         Dim img As Image = New Bitmap(ms)
    4.         Dim noStreamImage As Image = img.Clone
    5.         ms.Close()
    6.         Return noStreamImage
    7.  
    8.     End Function
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  15. #15
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: A generic error occurred in GDI+.

    Quote Originally Posted by kebo View Post
    To keep the original image you do, but you can always clone the image then close the stream and return the clone...
    Unfortunately, the clone approach has the same problems with regards to the stream.

    A while ago I spent several hours researching (Googling) this and ended up more confused than I started. There's a hell of a lot of advice out there, and half of it is wrong or misleading.

    I tend to create a new second image from the first and then dispose of the first image and its stream, something like:
    Code:
     Public Function byteArrayToBitmap(ByVal bytes As Byte()) As Bitmap
         Dim imageOut As Bitmap = Nothing
    
         Using ms As New System.IO.MemoryStream(bytes)
             Dim imageTemp As Image = Image.FromStream(ms)
             imageOut = New Bitmap(imageTemp)
             imageTemp.Dispose()
         End Using
    
         Return imageOut
     End Function
    The big disadvantage of that approach, however, is that the new image loses any metadata that the original may have had, and typically has a different colour depth and/or horizontal/vertical resolutions. That's why I asked Salem141 whether it was important that the original data stored in his images was to be saved or not.

  16. #16

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    12

    Re: A generic error occurred in GDI+.

    I have change it from

    Code:
    FrmManageDetailsCustomers.PictureBox2.Image = byteArrayToImage(MainCustomersGv.Rows(MainCustomersGv.CurrentRow.Index).Cells(2).Value)
    To

    Code:
    FrmManageDetailsCustomers.PictureBox2.Image = imageFromStream(MainCustomersGv.Rows(MainCustomersGv.CurrentRow.Index).Cells(2).Value)

    I get this error

    Unable to cast object of type 'System.Byte[]' to type 'System.IO.Stream'.

  17. #17
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: A generic error occurred in GDI+.

    That's probably because
    MainCustomersGv.CurrentRow.Cells(2).Value (really? Again with the index when you already have the row?)

    returns a byte array not a stream.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  18. #18

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    12

    Re: A generic error occurred in GDI+.

    Quote Originally Posted by techgnome View Post
    That's probably because
    MainCustomersGv.CurrentRow.Cells(2).Value (really? Again with the index when you already have the row?)

    returns a byte array not a stream.

    -tg

    i change it cuz i got
    Value of type '1-dimensional array of Byte' cannot be converted to 'System.Drawing.Image

  19. #19
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: A generic error occurred in GDI+.

    Ok, so it told you that you can't change an orange into an apple, so you replaced it with a banana...

    the cell contains a bytearray, that's what you're going to want to pass in... inside that you convert the byte array into a stream, a memory stream makes sense to me, then pass that stream to the image ... your original byteArrayToImage did all that... the only problem is that you need to keep the stream around ... the solution to which it seems like there's mixed results...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: A generic error occurred in GDI+.

    The reason that GDI+ requires a Stream to remain open is that it goes back to that Stream at various points to retrieve additional information. I don't know exactly what information it is but you get that generic GDI+ error when you try to save because GDI+ tries to go back to the Stream from which the Image was created to get information it needs when saving but it fails because that Stream is closed.

    Now that we've got to that point, I recall that I encountered the same issue a number of years ago. My solution at that time was to simply keep the Stream open indefinitely and let it be cleaned up by the system at the end. This is not ideal but it is feasible if the number of Streams you create will be small. Perhaps a better option would be to create a Dictionary(Of Image, Stream) to store each open Stream against the Image that was created from it. That way, when you're done with an Image and are ready to dispose it, you can get the corresponding Stream from the Dictionary and dispose that too, then remove that item from the Dictionary, e.g.
    vb.net Code:
    1. Private ReadOnly streamsByImage As New Dictionary(Of Image, Stream)
    2.  
    3. Private Function ToImage(bytes As Byte()) As Image
    4.     Dim strm As New MemoryStream(bytes)
    5.     Dim img = Image.FromStream(strm)
    6.  
    7.     streamsByImage.Add(img, strm)
    8.  
    9.     Return img
    10. End Function
    11.  
    12. Private Sub DisposeImage(img As Image)
    13.     Dim strm As Stream = Nothing
    14.  
    15.     If streamsByImage.TryGetValue(img, strm) Then
    16.         strm.Dispose()
    17.         streamsByImage.Remove(img)
    18.     End If
    19.  
    20.     img.Dispose()
    21. End Sub

  21. #21

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    12

    Re: A generic error occurred in GDI+.

    Thank You Very Much

    It Works Now 100%

Posting Permissions

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



Click Here to Expand Forum to Full Width