Parmater is not valid in Fax thumbnail code - Please Help

devnewbee
devnewbee used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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

Most Valuable Expert 2012
Top Expert 2014

Commented:
It also looks like you need to modify the GetThumbnailImage call to include a delegate and use IntPtr.Zero instead of Nothing for those parameters.  See:

http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage.aspx
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Looks like CodeCruiser beet me on that one.

Author

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial