当我通过图片框将图片插入我的数据库时,我得到一个例外
问题描述:
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:
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
答
System.Drawing.Bitmap.FromFile上的文档不完整,因为它没有提到文件会发生什么。但是,请参阅System.Drawing.Bitmap(String)的文档:
http://msdn.microsoft.com/en-us/library/0cbhe98f.aspx [ ^ ]
它说:
文件保持锁定,直到位图被处理掉。
从文档中不清楚什么样的锁定 接受了。你会假设它已被打开以便读取,因此对文件的写入被锁定 - 但是不清楚是否支持或锁定共享读取。
构造函数的简单实现可能使用FromFile方法 - 所以假设适用于构造函数的文档也适用于FromFile是合理的。
稍后,您尝试使用以下方法再次打开文件:
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)