I want to get image from the file..I m able to do it....But mine code do not close the file,after getting the image,I want to close the file.Cz after getting the image,I want to delete that file.....on Deleteing error is dere file is user by another process...

Public Sub GetImagesContent(ByVal sFileName As String)
        Try
            Dim sExtension As String
            Dim img As System.Drawing.Image

            sExtension = System.IO.Path.GetExtension(sFileName)
            If Trim(sExtension) = ".bmp" Or _
                Trim(sExtension) = ".jpg" Or _
                Trim(sExtension) = ".jpeg" Or _
                Trim(sExtension) = ".gif" Or _
                Trim(sExtension) = ".png" Then
                img = Image.FromFile(sFileName)
                ImgThumbNailView.Images.Add(img)
           End if

IS Image.FromFile do not close the file????

Suppose I convert string to fileinfo object

dim a as fileinfo=new fileinfo(sFileName)

How to close the file,Can somebody tell me?

Recommended Answers

All 4 Replies

Use Image.FromStream

hi frnd, find the solution of mine code-

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim img As Image
            Dim sFileName As String = "D:\Documents and Settings\Mansi\Desktop\Winter.jpg"
            img = Image.FromFile(sFileName)
            ImageList1.Images.Add(img)
            PictureBox1.Image = ImageList1.Images.Item(0)
            img.Dispose()
            System.IO.File.Delete(sFileName)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

End Class

Can u provide me the code of urs,CZ i didnt know how to get image using from stream

Use FileStream class:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim img As Image
            Dim sFileName As String = "D:\Documents and Settings\Mansi\Desktop\Winter.jpg"
            Dim fs as new System.IO.FileStream(sFileName,System.IO.FileMode.Open)
            img = Image.FromStream(fs)
            fs.close()
            ImageList1.Images.Add(img)
            PictureBox1.Image = ImageList1.Images.Item(0)
            img.Dispose()
            System.IO.File.Delete(sFileName)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.