2010-09-21 131 views
0

我的老師給了我們示例代碼,它非常類似於下面的代碼。我還沒有收到他的回信,想知道爲什麼我的代碼無法正常工作。有人能給我一些建議,說明我做錯了什麼,或者更簡單的方法來顯示圖像。謝謝你的時間。顯示圖像

<%@ WebHandler Language="VB" Class="images" %> 

Imports System 
Imports System.Web 
Imports System.Data.SqlClient 
Public Class images : Implements IHttpHandler 

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest 


     Dim id As String = context.Request.QueryString("ImageId") 
     Dim userId As Integer = 4 


     Dim conn As New System.Data.SqlClient.SqlConnection 


     Dim cmd As New System.Data.SqlClient.SqlCommand 


     conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString 
     cmd.Connection = conn 
     cmd.CommandText = "SELECT Image FROM mrg_Image WHERE [email protected]" 
     cmd.Parameters.AddWithValue("@userId", userId) 

     conn.Open() 
     Dim file_bytes As Byte() = cmd.ExecuteScalar()   
     Dim file_bytes_stream As New System.IO.MemoryStream(file_bytes) 

     'During the build - The next line of code is highlighted green with the error message of, "Parameter is invalid" 

     Dim the_image As New System.Drawing.Bitmap(file_bytes_stream) 

     context.Response.ContentType = "image/jpg" 
     the_image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)   

     conn.Close() 
    End Sub 

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable 
     Get 
      Return False 
     End Get 
    End Property 

End Class 
+0

它建立在我的機器上細 – 2010-09-21 21:46:10

回答

0

編輯:在評論中回答(如是)。提問者的代碼很好...


看起來不錯。你是否在下面的第一行和最後一行之間進行了implict類型轉換?

Dim file_bytes_stream As New System.IO.MemoryStream(file_bytes) 

'During the build - The next line of code is highlighted green with the error message of, "Parameter is invalid" 

Dim the_image As New System.Drawing.Bitmap(file_bytes_stream) 

如果你有option explicit集,你可能需要做...

Dim file_bytes_memory_stream As New System.IO.MemoryStream(file_bytes) 

Dim file_bytes_stream as System.IO.Stream = DirectCast(file_bytes_memory_stream, System.IO.Stream) 

Dim the_image As New System.Drawing.Bitmap(file_bytes_stream) 
+0

我已經嘗試過了,我仍然得到同樣的錯誤,「參數無效」。 – PaulR 2010-09-21 22:46:23

+0

我剛剛檢查過它,它在原始版本中爲我構建。你使用的是什麼版本的Visual Studio?此外它上面是選項嚴格不選項顯式我應該引用... – 2010-09-21 23:03:06

+0

等待,這看起來不像編譯錯誤,它看起來像一個運行時異常!你確定你是在編譯時得到它(即當你構建項目,構建 - >構建解決方案),而不是當你真正運行項目時? – 2010-09-21 23:09:21