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

Visual Basic.NETC#ASP.NET

Avatar of undefined
Last Comment
SterlingMcClung

8/22/2022 - Mon
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

Nasir Razzaq

ASKER CERTIFIED SOLUTION
SterlingMcClung

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SterlingMcClung

Looks like CodeCruiser beet me on that one.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
devnewbee

ASKER
Sterling, what is different between your code and mine?
SterlingMcClung

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.