Click here to Skip to main content
14,449,862 members
Rate this:
Please Sign up or sign in to vote.
See more:
please provide me solution for following code it shows GDI+ error on
Public Class Form1<br />
    Dim open As OpenFileDialog<br />
    Dim i As String<br />
    Dim img As Image = Nothing<br />
    Dim pic As String<br />
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<br />
    End Sub<br />
<br />
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<br />
        open = New OpenFileDialog<br />
        open.Filter = "Image Files(*.jpg;*.jpeg;*.gif;*.bmp;*.png;*.tif)|*.jpg;*.gif;*.bmp;*.tif"<br />
        open.ShowDialog()<br />
        pic = open.FileName<br />
        Try<br />
            Dim fs As New System.IO.FileStream(pic, IO.FileMode.Open, IO.FileAccess.Read)<br />
            img = Image.FromStream(fs)<br />
            fs.Close()<br />
            fs.Dispose()<br />
            My.Computer.FileSystem.DeleteFile(pic)<br />
            emppic.Image = img<br />
        Catch ex As Exception<br />
            img = Nothing<br />
        End Try<br />
    End Sub<br />
<br />
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click<br />
        Dim img6 As Image<br />
        img6 = emppic.Image<br />
        Try<br />
            emppic.Image.Save("C:\C-tek\Images\Temp\1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)<br />
        Catch ex As Exception<br />
            MsgBox("File  Saving Error." + ex.ToString)<br />
        End Try<br />
    End Sub<br />
End Class

thnks in advance
Updated 13-Feb-20 0:02am
Rate this:
Please Sign up or sign in to vote.

Solution 1

As I told you last time you posted this: 2008 A generic error occurred in GDI+.[^] the documentation very clearly says that you must keep teh stream open for teh lifetime of the image.
You don't.

So, you have two options:
1) Keep the stream open for the life of the image.
2) Copy the image to another and Dispose of the original stream and image once you have done that.
That's easy:
Dim safeImage as New Bitmap(img)
You can now close and dispose the original stream and image.

In future, have a little patience please...
Member 9377677 23-Oct-13 3:18am
thanks griff
Member 9377677 23-Oct-13 3:25am
as you can see from the code that I already did this by adding the image from files stream to img. and then assigned the value of img to picture box. after that I closed the stream. please tell me that whether do I need to again assign the value of img to new variable safeimage. thanks in advance. I will defiantly keep patience in future.
OriginalGriff 23-Oct-13 3:57am
"I already did this by adding the image from files stream to img."
No, you didn't.
After you have loaded "img" with your image from the stream, you need to create a new *copy* of the image with "New Bitmap(img)" and then set the "emppic.Image" value to that instead of the original "img". The copy is not associated with the stream, or the original image - it's just a copy of the actual image data - so the original stream and image can be disposed safely, and your later "Image.Save" call will work fine, because it is working with the copy, not the original stream based version.
Member 9377677 24-Oct-13 1:09am
thanks griff its solved
OriginalGriff 24-Oct-13 3:00am
You're welcome!
Rate this:
Please Sign up or sign in to vote.

Solution 2

I had the same problem, I solved it creating the path were I want to save and giving right access.
Rate this:
Please Sign up or sign in to vote.

Solution 3

I faced the same issue. In my scenario all are working fine,but getting exception when saving the image.the problem is i am saving the file with special character (\), which is not acceptable. now it is working fine after changed the file name.
Rate this:
Please Sign up or sign in to vote.

Solution 5

Dim filesize As UInt32
Dim ms As New MemoryStream
pbBookImg1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim imgbyte() As Byte = ms.GetBuffer
filesize = ms.Length

Dim sql As String
Dim dt As String
dt = Format(Now, "yyyy-MM-dd")
Select Case btnSave.Tag
    Case "Add"
        sql = "Insert into tbl_bookmaster(bookID,title,isbn,pages,langID,edition,authID,pubID,pubYear,secID,description,totalCopies,bookImg,dtCreated,isDelete) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,0)"
        Call addBookID()
    Case "Update"
        sql = "Update tbl_bookmaster set bookID=?,title=?,isbn=?,pages=?,langID=?,edition=?,authID=?,pubID=?,pubYear=?,secID=?,description=?,totalCopies=?,bookImg=? where bookID='" & Trim(dgvBookMaster.Tag) & "'"
End Select

cmd = New Odbc.OdbcCommand(sql, con)
cmd.Parameters.AddWithValue("?", lblBookID.Text)
cmd.Parameters.AddWithValue("?", txtTitle.Text)
cmd.Parameters.AddWithValue("?", txtISBN.Text)
cmd.Parameters.AddWithValue("?", txtPages.Text)
cmd.Parameters.AddWithValue("?", cbxLang.SelectedValue)
cmd.Parameters.AddWithValue("?", txtEdition.Text)
cmd.Parameters.AddWithValue("?", cbxAuth.SelectedValue)
cmd.Parameters.AddWithValue("?", cbxPublisher.SelectedValue)
cmd.Parameters.AddWithValue("?", txtYrPublished.Text)
cmd.Parameters.AddWithValue("?", cbxSection.SelectedValue)
cmd.Parameters.AddWithValue("?", txtDescription.Text)
cmd.Parameters.AddWithValue("?", txtCopies.Text)
cmd.Parameters.AddWithValue("?", imgbyte)
cmd.Parameters.AddWithValue("?", dt)
Call cleaner(tpDetails)
Call dgvloader("Select bookID,title,isbn,edition,fullName,pubDesc,pubYear,secDesc from vw_bookmaster where isDelete=0", dgvBookMaster)
lblBookID.Text = ""
dgvBookMaster.Tag = ""
tcBooks.SelectedTab = tpMain
Call buttEnabler(pnlLside, False)

Nattawutxp 13-Feb-20 5:07am
Ahhhh very good

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100