我有这个SQL数据库“学生”,我想将图像(全部或选中)从它的表“StudentPic”导出到扩展名为“.jpg”的特定文件夹。
这是我的工作而且没有用。对此有任何意见。?
Dim sql As String = " SELECT TOP 10 StudID,StudentPic FROM Student"
Dim cmd As SqlCommand = New SqlCommand(sql, conn)
conn.Open
Dim dr As SqlDataReader = cmd.ExecuteReader
While dr.Read
Dim bytes() As Byte = CType(dr("StudentPic"),Byte())
Dim memStream As MemoryStream = New MemoryStream(bytes)
Try
Dim MyImage As Bitmap = New Bitmap(memStream)
MyImage = New Bitmap(MyImage, 200, 250)
MyImage.Save((dr("StudID") + ".jpg"), ImageFormat.Jpeg)
Catch ex As Exception
Throw ex
End Try
End While
答案 0 :(得分:1)
您可以使用查询从表格中选择图像,然后使用.net图像功能将图像保存到磁盘。这并不难,但正如已经提到的那样,期望在这里编写代码并不是一个好主意。我会在.net中深入研究它,如果你遇到困难,请在这里发布你的问题(详细信息)。祝你好运!
答案 1 :(得分:0)
您可以将图像检索到字节数组,然后使用FileStream写入文件。 这是示例。
从数据库中检索图像是将图像保存到数据库的完全相反的过程。
SqlCommand cmd = new SqlCommand("select Pic from StudentPic where StudentID=@ID",
new SqlConnection("conn stirng"));cmd.Parameters.AddWithValue("@ID",'id of a student');cmd.Connection.Open();
执行“ExecuteScalar”因为我们只想要“IMAGE”列数据。
byte[] byteImg=(byte[])cmd.ExecuteScalar();cmd.Connection.Close();
将此数据保存到文件夹。
string strPath=Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\StudentPics\\"+"picName.jpg";
FileStream fs=new FileStream(strFileTime,FileMode.CreateNew,FileAccess.Write);
fs.Write(byteImg,0,byteImg.Length);fs.Flush();fs.Close();
希望有所帮助。