Click here to Skip to main content
15,905,913 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
CSS
i am getting the following exception while i am inserting a picture into database through a picturebox

System.IO.IOException was unhandled
Message=The process cannot access the file 'C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg' because it is being used by another process.


i have tried the following code:



VB
Imports System.IO
02
Imports System.Data
03
Imports System.Data.SqlClient
04
Imports System.Data.SqlTypes
05
Public Class Form1
06
    Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=ImagesStore;Integrated Security=True;Pooling=False")
07
    Dim cmd As SqlCommand
08
 
09
    Private mImageFile As Image
10
    Private mImageFilePath As String
11
 
12
 Private Sub BtnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOpen.Click
13
        Dim imge As String
14
        OpenFileDialog1.Title = "Set Image File"
15
        OpenFileDialog1.Filter = "Bitmap Files|*.bmp" & _
16
            "|Gif Files|*.gif|JPEG Files|*.jpg"
17
        OpenFileDialog1.DefaultExt = "bmp"
18
        OpenFileDialog1.FilterIndex = 1
19
        OpenFileDialog1.FileName = ""
20
        OpenFileDialog1.ShowDialog()
21
 
22
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then
23
            Exit Sub
24
        End If
25
        If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
26
            imge = OpenFileDialog1.FileName
27
            PictureBox1.Image = System.Drawing.Bitmap.FromFile(imge)
28
        End If
29
        Dim sFilePath As String
30
        sFilePath = OpenFileDialog1.FileName
31
        If sFilePath = "" Then Exit Sub
32
 
33
        If System.IO.File.Exists(sFilePath) = False Then
34
            Exit Sub
35
        Else
36
            txtImageFile.Text = sFilePath
37
            mImageFilePath = sFilePath
38
        End If
39
    End Sub
40
 
41
 Private Sub BtnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnInsert.Click
42
        Try
43
            If (Me.txtImageFile.Text = String.Empty Or Me.txtTitle.Text =
44
                String.Empty) Then
45
                MessageBox.Show("Complete both form fields prior to submitting",
46
                "Missing Values", _
47
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
48
                Exit Sub
49
            End If
50
        Catch ex As Exception
51
            MessageBox.Show(ex.Message.ToString(), "File Test Error")
52
        End Try
53
 
54
        Dim fs As FileStream = New FileStream(mImageFilePath.ToString(),
55
        FileMode.Open)    \\I am getting the exception in this line \\
56
 
57
        Dim img As Byte() = New Byte(fs.Length) {}
58
        fs.Read(img, 0, fs.Length)
59
        fs.Close()
60
 
61
        Dim sSQL As String = "INSERT INTO ImagesStore(OriginalPath,ImageData)VALUES(@op,@id)"
62
        cmd = New SqlCommand(sSQL, conn)
63
 
64
        cmd.Parameters.AddWithValue("@op", (img))
65
        cmd.Parameters.AddWithValue("@id", (txtTitle.Text))
66
 
67
        Try
68
            conn.Open()
69
            cmd.ExecuteNonQuery()
70
            conn.Close()
71
            MessageBox.Show("Query executed.", "Image Load")
72
        Catch ex As Exception
73
            MessageBox.Show(ex.Message.ToString(), "Data Error")
74
            Exit Sub
75
        End Try
76
    End Sub
Posted
Comments
joshrduncan2012 6-Dec-12 13:20pm    
What line is the error pointing to?
Pratik65 6-Dec-12 13:28pm    
it is pointing to the FileStream in insert method

1 solution

The documenation on System.Drawing.Bitmap.FromFile is incomplete as it does not mention what happens with file. However, see the documentation for System.Drawing.Bitmap(String):

http://msdn.microsoft.com/en-us/library/0cbhe98f.aspx[^]

It says:

"The file remains locked until the Bitmap is disposed."

It's unclear from the documentation what kind of "lock" is taken on it. You'd assume it is opened for read, so that writes to the file are locked -- but it's not clear if shared reads are supported or locked out.

The trivial implementation of the constructor probably uses the FromFile method -- so it's plausible to assume the documentation that applies to the constructor also applies to FromFile.

Later, you try to open the file again using:

FileSteam(filename, FileMode.Open)


But you don't specify the access mode or shared acces mode, and it's again, not clear from the documentation what the default is.

What you probably want to do is explicitly open a file stream with shared read access and use that to create the bitmap:
Dim imgStream as FileStream new(imge, FileMode.Open, FileAccess.Read, FileShare.Read)
PictureBox1.Image = System.Drawing.Bitmap.FromStream(imgStream)


And then open it again the same way to access the data for storing in the database:
Dim fs As FileStream = New FileStream(mImageFilePath.ToString(),
        FileMode.Open, FileAcess.Read, FileShare.Read)
 
Share this answer
 

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