Link to home
Start Free TrialLog in
Avatar of devnewbee
devnewbee

asked on

Parmater is not valid in Fax thumbnail code - Please Help

Can anyone look at the attached code and tell me why this error is occuring?

*****

Error Message: Parameter is not valid.
Stack Trace:    at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)
  at System.Drawing.Bitmap..ctor(Image original, Int32 width, Int32 height)
  at System.Drawing.Bitmap..ctor(Image original)
  at attachment.ProcessRequest(HttpContext context) in C:\Inetpub\test.org\faxthumbnail.ashx:line 52
  at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
<%@ WebHandler Language="VB" Class="attachment" %>

Imports System
Imports System.Web

Public Class attachment : Implements IHttpHandler
    
    Const SMALL_MAXDIM As Integer = 200
    Const LARGE_MAXDIM As Integer = 600
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        If context.Request.QueryString("id") = "" OrElse Not IsNumeric(context.Request.QueryString("id")) OrElse context.Request.QueryString("page") = "" OrElse Not IsNumeric(context.Request.QueryString("page")) Then
            context.Response.ContentType = "image/jpeg"
            context.Response.Write(Nothing)
        Else
            Dim i As Integer = context.Request.QueryString("page")
            Dim w As Integer
            Dim h As Integer
            Dim sqlCommand As New System.Data.SqlClient.SqlCommand("tg_GetApplicationAttachment", New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("cnBTO").ConnectionString))
            Dim sqlReader As System.Data.SqlClient.SqlDataReader
            Dim buffer() As Byte
            Dim intMaxDimension As Integer = SMALL_MAXDIM
            If context.Request.QueryString("large") = "1" Then
                intMaxDimension = LARGE_MAXDIM
            End If
            sqlCommand.CommandType = CommandType.StoredProcedure
            sqlCommand.Parameters.AddWithValue("@application_attachment_id", context.Request.QueryString("id"))
            sqlCommand.Parameters.AddWithValue("@send_file_body", True)
            sqlCommand.Connection.Open()
            sqlReader = sqlCommand.ExecuteReader()
            sqlReader.Read()
            
            If sqlReader("compressed") Then
                buffer = Utils.DeCompress(sqlReader("file_body"))
            Else
                buffer = sqlReader("file_body")
            End If
            
            sqlReader.Close()
            sqlCommand.Connection.Close()
            
            Dim image As New System.Drawing.Bitmap(New System.IO.MemoryStream(buffer))
            Dim frameDimensions As New System.Drawing.Imaging.FrameDimension(image.FrameDimensionsList(0))
            image.SelectActiveFrame(frameDimensions, i)
            If (image.Height / image.VerticalResolution) > (image.Width / image.HorizontalResolution) Then
                h = intMaxDimension
                w = intMaxDimension * (CDec(image.Width / image.HorizontalResolution) / CDec(image.Height / image.VerticalResolution))
            Else
                h = intMaxDimension * (CDec(image.Height / image.VerticalResolution) / CDec(image.Width / image.HorizontalResolution))
                w = intMaxDimension
            End If
            Dim thumbnail As System.Drawing.Bitmap = New System.Drawing.Bitmap(image).GetThumbnailImage(w, h, Nothing, Nothing)
            context.Response.ContentType = "image/jpeg"
            thumbnail.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
        End If
    
    End Sub
   
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
End Class

Open in new window

Avatar of SterlingMcClung
SterlingMcClung

Try this:

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

Imports System
Imports System.Web

Public Class attachment : Implements IHttpHandler
    
    Const SMALL_MAXDIM As Integer = 200
    Const LARGE_MAXDIM As Integer = 600
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        If context.Request.QueryString("id") = "" OrElse Not IsNumeric(context.Request.QueryString("id")) OrElse context.Request.QueryString("page") = "" OrElse Not IsNumeric(context.Request.QueryString("page")) Then
            context.Response.ContentType = "image/jpeg"
            context.Response.Write(Nothing)
        Else
            Dim i As Integer = context.Request.QueryString("page")
            Dim w As Integer
            Dim h As Integer
            Dim sqlCommand As New System.Data.SqlClient.SqlCommand("tg_GetApplicationAttachment", New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("cnBTO").ConnectionString))
            Dim sqlReader As System.Data.SqlClient.SqlDataReader
            Dim buffer() As Byte
            Dim intMaxDimension As Integer = SMALL_MAXDIM
            If context.Request.QueryString("large") = "1" Then
                intMaxDimension = LARGE_MAXDIM
            End If
            sqlCommand.CommandType = CommandType.StoredProcedure
            sqlCommand.Parameters.AddWithValue("@application_attachment_id", context.Request.QueryString("id"))
            sqlCommand.Parameters.AddWithValue("@send_file_body", True)
            sqlCommand.Connection.Open()
            sqlReader = sqlCommand.ExecuteReader()
            sqlReader.Read()
            
            If sqlReader("compressed") Then
                buffer = Utils.DeCompress(sqlReader("file_body"))
            Else
                buffer = sqlReader("file_body")
            End If
            
            sqlReader.Close()
            sqlCommand.Connection.Close()
            
            Dim image As New System.Drawing.Bitmap(New System.IO.MemoryStream(buffer))
            Dim frameDimensions As New System.Drawing.Imaging.FrameDimension(image.FrameDimensionsList(0))
            image.SelectActiveFrame(frameDimensions, i)
            If (image.Height / image.VerticalResolution) > (image.Width / image.HorizontalResolution) Then
                h = intMaxDimension
                w = intMaxDimension * (CDec(image.Width / image.HorizontalResolution) / CDec(image.Height / image.VerticalResolution))
            Else
                h = intMaxDimension * (CDec(image.Height / image.VerticalResolution) / CDec(image.Width / image.HorizontalResolution))
                w = intMaxDimension
            End If
            Dim thumbnail As System.Drawing.Bitmap = image.GetThumbnailImage(w, h, Nothing, Nothing)
            context.Response.ContentType = "image/jpeg"
            thumbnail.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
        End If
    
    End Sub
   
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
End Class

Open in new window

Avatar of Nasir Razzaq
ASKER CERTIFIED SOLUTION
Avatar of SterlingMcClung
SterlingMcClung

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Looks like CodeCruiser beet me on that one.
Avatar of devnewbee

ASKER

Sterling, what is different between your code and mine?
Sorry,

It is a small change.  I changed a line to:

 
Dim thumbnail As System.Drawing.Bitmap = image.GetThumbnailImage(w, h, Nothing, Nothing)

Open in new window


You don't need to create a new bitmap, just to call GetThumbnailImage.  You can simply do that on your existing bitmap image.  Your code created a new bitmap from your "image" bitmap and used that newly created bitmap to generate a thumbnail.  However, since the newly created bitmap is not assigned to anything, it is a little frivolous.

I think that the main problem is that the delegate has to be passed in and that the IntPtr must be zero, not nothing.