tags:

views:

47

answers:

1

Hello fellow Programmers

My instructor gave us example code that is very similiar to the code below. I haven't heard back from him yet and wanted to find out why my code won't work properly. Could someone give me some advice on what I'm doing wrong or an easier method to display images. Thanks for your time.

<%@ 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 UserId=@userId"
        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
A: 

EDIT: Answer (such as it is) in comments. The questioners code is fine...


Looks fine to me. Are you comitting an implict type conversion between the first and last lines below?

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)

If you have option explicit set you might need to do...

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)
Christopher Edwards
I've tried it and I still get the same error, "Parameter is invalid".
PaulR
I've just checked it and it built for me, in the original version. What version of Visual Studio are you using? Also above it was option strict not option explicit I should have cited...
Christopher Edwards
Wait, that doesn't look like a compile error it looks like a runtime exception! Are you sure you are getting it at compile time (i.e. when you build the project, Build -> Build Solution) and not when you actually run the project?
Christopher Edwards
I'm using VS 2008 - To be honest I'm not sure what the difference of a runtime error and a compile error. Everytime I try to compile my program, that one line is highlighted green and it complains that I have an invalid parameter. Sorry, if your questions are lost to me.
PaulR
A compile error happens when the text that forms your code is being translated into code the .net run time can understand, called IL (Intermediate Language). If you press the "play" button it will compile then run you code. If you just want it to compile you go to "Build" them "Build Solution", I don't think you will get an error. I'm pretty sure it's runtime error.
Christopher Edwards
Do you have an image stored in the DB for user 4 and for whatever query string you are putting in the URL?
Christopher Edwards
OK, I ran the code as well as compiling it (slightly altered to reference a different image in a different DB) . It works for me...
Christopher Edwards
Thanks for your suggestions, I brought your feedback before my instructor and he has agreed to take a look at it.
PaulR
Friend Christopher - your amiable programming comrade has talked to his instructor. The problem is not with my code but with the image itself. When I use a file upload my image is stored in a database. The code above extracts that image file. When I hover over the file_bytes variable (which stores the results from the execute scalar) He noticed that the length was 6 bytes and should be a lot more. I'm not sure how to begin problem shooting this. Have you heard of this sort of problem? Thanks for your help thus far.
PaulR
I asked another question with new code. Thanks again
PaulR