这是我的问题: 我正在尝试循环文件夹从文件夹中获取文件并将每个文件保存到图像然后保存到DataBase但由于某种原因,图像的长度始终相同。就像它没有创建一个新的图像实例。
这是我的代码+评论:
Dim dba As New DBAccess
Dim folderName As String = String.Empty
Dim imageFileName As String = String.Empty
Dim ms1 As New MemoryStream()
Dim ds As DataSet = New DataSet
If m_IncidentCaseID > 0 Then
If Not String.IsNullOrEmpty(Sessions.GetKeyValueSessionFile(m_SessionID, "IRPicturesPath")) Then
m_IncidentPicturesPath = Convert.ToString(Sessions.GetKeyValueSessionFile(m_SessionID, "IRPicturesPath"))
End If
folderName = m_IncidentPicturesPath '!= Set the path of the folder.
'!= For Each file get the data from the DataBase: PictureID,PictureIndex,PictureFileName.
m_HashTableIRPicturesWriter = New Hashtable
m_DictionaryEntryIRPicturesWriter = New DictionaryEntry
Dim strArr As String() = Directory.GetFiles(folderName)
For Each filePath As String In strArr
ds = dba.GetPicturesIDFromPath(filePath, m_User.CompanyCode)
If Not ds Is Nothing Then
m_HashTableIRPicturesWriter.Add(ds.Tables(0).Rows(0).Item("PictureID").ToString(), filePath)
End If
Next
'!= We saved each file Path +PictrureID to Hashtable
'!== Loop through the hashtable and for each file in the folder create an image.
For Each filename As DictionaryEntry In m_HashTableIRPicturesWriter
Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(filename.Value())
Dim bytes As Byte()
img.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg)
bytes = ms1.ToArray()
'!=================================== Delete and Insert to the Temp_Table in the DB ==============================!
Dim dBConnection As String
dBConnection = Globals.GetConnectionStringMain(m_User.CompanyCode())
Dim connString As New SqlConnection(dBConnection)
'# First Delete the table from the IncidentID that we want to insert.
Dim sql1 As String = "Delete From Temp_Incident_Images Where PictureID =" & filename.Key()
Dim cmd1 As New SqlCommand(sql1, connString)
If connString.State = ConnectionState.Closed Then
connString.Open()
End If
cmd1.Parameters.Add(New SqlParameter("@IncidentID", m_IncidentCaseID))
cmd1.ExecuteNonQuery()
connString.Close()
If connString.State = ConnectionState.Closed Then
connString.Open()
End If
'# Second Inserst the PictureID to the Table.
Dim sql As String = "Insert Into Temp_Incident_Images(PictureID,IncidentID,IncidentImage) values (@PictureID,@IncidentID,@IncidentImage)"
Dim cmd As New SqlCommand(sql, connString)
cmd.Parameters.AddWithValue("@IncidentID", m_IncidentCaseID)
cmd.Parameters.AddWithValue("@PictureID", filename.Key())
Dim data As Byte() = ms1.GetBuffer() '!=When checking the Data it's same all the time.
Dim p As New SqlParameter("@IncidentImage", SqlDbType.Image)
p.Value = data
cmd.Parameters.Add(p)
Try
cmd.ExecuteNonQuery()
Catch ex As Exception
Throw New SystemException(ex.Message.ToString.Trim)
End Try
ms1.Close()
connString.Close()
img.Dispose()
Next
End If
如何从这行代码中创建一个新的图像实例:
Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(filename.Value())
Dim bytes As Byte()
img.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg)
bytes = ms1.ToArray()
答案 0 :(得分:0)
如果您在此处遇到数据库的图像路径问题,则可以使用简单的代码。
添加文件上传和标签或类似的内容,
<asp:FileUpload id="FileUploadControl" runat="server" />
<asp:Label runat="server" id="StatusLabel" text="Upload status: " />
之后添加一个按钮,执行您想要的任何操作。
<asp:Button runat="server" class="btn btn-info pull-right" id="UploadButton" text="Upload" onclick="UploadButton_Click" />
按钮内添加
Try
If FileUploadControl.PostedFile.ContentType = "image/jpeg" Then
If FileUploadControl.PostedFile.ContentLength < 102400 Then
Dim filename As String = Path.GetFileName(FileUploadControl.FileName)
FileUploadControl.SaveAs(Server.MapPath("~/ProductImages/") & filename)
StatusLabel.Text = "Upload status: File uploaded!"
Else
StatusLabel.Text = "Upload status: The file has to be less than 100 kb!"
End If
Else
StatusLabel.Text = "Upload status: Only JPEG files are accepted!"
End If
Catch ex As Exception
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message
End Try
如果您解决了问题,请告诉我。如果您需要更多帮助,我可以编辑代码。