page 1 of 1
Comments: 4 | Views: 1670
Hi....I need help for my little problem here. I'm using Visual Basic 2005 and SQL Server 2000. I want to save an image file to the database.
There is this button called btnImage that I use to display the image. When I click this button, an OpenFileDialog will be shown to select a file. Then the image will be displayed on the button as its BackgroundImage. And then I call a procedure named SaveImageToSQL to save the image file. But everytime I call it, I always receive this error message: "The file [FileDirPath] is being opened by another process........" or something like that. I guess this happens because the file is being loaded to the btnImage.
Can anyone suggest what I should do here? I want to save the image to database and also preview it on the image. I tried using the PictureBox instead, but it always ends up to the same problem.

Here's what my code looks like:

Imports System.IO

Private Sub btnImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImage.Click
        OpenFileDialog1.ShowDialog()
        If OpenFileDialog1.FileName <> "" Then
            Try
                btnImage.BackgroundImage = Image.FromFile(OpenFileDialog1.FileName)
                btnImage.Tag = OpenFileDialog1.FileName
                PhotoAttached = True
            Catch ex As Exception
                lblState.Text = ex.Message
                lblState.ForeColor = Color.Red
            End Try
        End If
    End Sub

Private Function SaveImageToSQL(ByVal FilePath As String, ByVal SaveMode As Integer) As Boolean
        Dim fs As FileStream = New FileStream(FilePath, FileMode.Open)
        Dim img As Byte() = New Byte(fs.Length) {}
        Dim pic As SqlParameter = New SqlParameter("@pic", SqlDbType.Image)

        fs.Read(img, 0, fs.Length)
        fs.Close()

        If SaveMode = 1 Then              'Update Photo
            strCommand = "UPDATE Photo SET Photo=@pic WHERE Username='" & txtUsername.Text & "'"
        Else                                          'New Photo
            strCommand = "INSERT INTO Photo(Username,Photo) VALUES('" & txtUsername.Text & "',@pic)"
        End If

        Try
            pic.Value = img
            objCommand = New SqlCommand(strCommand, objConn)
            objCommand.Parameters.Add(pic)
            objCommand.ExecuteNonQuery()
        Catch ex As Exception
            lblState.Text = ex.Message
            lblState.ForeColor = Color.Red
        End Try

    End Function

Any suggestion will be a great help to me. Thanx in advance.
TommyCarlier
TommyCarlier
I want my scalps!
Last year I wrote a blog post about this. The code is in C#, but I think it's not hard to translate to VB. It's only a few lines of code. http://tommycarlier.blogspot.com/2006/06/reading-image-from-file-without.html
Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...
Tommy, the method you're using to do this probably works in most cases, but it is not generically safe.

You see, the reason Image.FromFile keeps the file locked is because GDI+ can (depending on the codec, the size of the file, and other factors) delay decoding (parts of) the image until they are actually needed (e.g. when you draw them). The Image.FromStream method will for the same reason keep a reference to the stream you passed it. If you close the stream and you run into one of these delayed-decoding scenarios, GDI+ will access the stream and fail with an ObjectDisposedException.

The only way that I know of to achieve this that is 100% safe in all cases is to load the image from file, create a new image, draw the original image into the new image, and then close the original one (which unlocks the file).

Public Module Utils
   Public Function ImageFromFile(ByVal fileName As String) As Image
      Using image As Image = image.FromFile(fileName)
         Dim result As New Bitmap(image.Width, image.Height, Imaging.PixelFormat.Format32bppArgb)
         Using g As Graphics = Graphics.FromImage(result)
            g.DrawImage(image, 0, 0, image.Width, image.Height)
         End Using
         Return result
      End Using
   End Function
End Module

stevo_
stevo_
Human after all
Does that have an effect on the memory usage?
page 1 of 1
Comments: 4 | Views: 1670
Microsoft Communities