我需要實作一個帶有用戶個人資料圖片的網頁。但是,從資料庫中加載圖片需要大約 2 秒才能完全加載。
在加載個人資料圖片時,我需要放置一個帶有圓形影片的 GIF。
然后,一旦資料庫完全回復了影像,它應該將 GIF 影像更改為用戶的個人資料圖片。
這是控制器:
<HttpGet>
Public Async Function ProfilePicture(ByVal UserId As Integer) As Task(Of ActionResult)
Using oUser As New User
oUser.UserID = UserId
Dim oData As Byte() = Await oUser.RetrievePicFromDb
If oData IsNot Nothing AndAlso oData.Length > 0 Then
Return File(oData, "image/jpg", "")
Else
Return File("~/Content/Images/default-profile.png", "image /jpg", "")
End If
End Using
End Function
這是從資料庫中檢索的資料
Public Async Function RetrievePicFromDb() As Task(Of Byte())
Dim b As Byte()
Try
Using cn As New SqlClient.SqlConnection
cn.ConnectionString = ConfigurationManager.ConnectionStrings("SQLServer").ConnectionString
cn.Open()
Dim oCmd As New SqlClient.SqlCommand
oCmd.Connection = cn
oCmd.CommandType = CommandType.Text
oCmd.Parameters.Clear()
oCmd.Parameters.AddWithValue("@UserID", UserID)
oCmd.CommandText = "Select Picture From Users Where UserID = @UserID"
Dim iCol As Integer = 0
Dim oReader As SqlClient.SqlDataReader = oCmd.ExecuteReader
oReader.Read()
Dim bImageByte(oReader.GetBytes(iCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
oReader.GetBytes(iCol, 0, bImageByte, 0, bImageByte.Length)
Dim ms As New System.IO.MemoryStream(bImageByte)
Using ms
Dim jpgMS = New System.IO.MemoryStream
Dim jpgImage As System.Drawing.Image = System.Drawing.Image.FromStream(ms)
jpgImage.Save(jpgMS, System.Drawing.Imaging.ImageFormat.Jpeg)
b = jpgMS.ToArray()
End Using
oReader.Close()
cn.Close()
End Using
Return b
Catch ex As Exception
Return Nothing
End Try
End Function
正在生成警告錯誤:
并且顯示的圖片是
感謝您提供任何幫助!
謝謝!
uj5u.com熱心網友回復:
嘗試使用這個 await Task.Run(() => @Your 同步函式);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/505715.html
上一篇:CDate轉換為不同的日期格式