Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
please provide me solution for following code it shows GDI+ error on pic.save.
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
Posted
Updated 12-Feb-20 23:02pm

As I told you last time you posted this: vb.net 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:
VB
Dim safeImage as New Bitmap(img)
You can now close and dispose the original stream and image.

In future, have a little patience please...
 
Share this answer
 
Comments
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!
I had the same problem, I solved it creating the path were I want to save and giving right access.
Regards.
 
Share this answer
 
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.
 
Share this answer
 
Comments
Geeky Sharma 26-Oct-20 20:57pm    
then how to give a path without using \/ ?
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)
cmd.ExecuteNonQuery()
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)
ms.Close()
ms.Dispose()

da.Dispose()
 
Share this answer
 
Comments
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900