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..cto r(Int32 width, Int32 height, PixelFormat format)
at System.Drawing.Bitmap..cto r(Image original, Int32 width, Int32 height)
at System.Drawing.Bitmap..cto r(Image original)
at attachment.ProcessRequest( HttpContex t context) in C:\Inetpub\test.org\faxthu mbnail.ash x:line 52
at System.Web.HttpApplication .CallHandl erExecutio nStep.Syst em.Web.Htt pApplicati on.IExecut ionStep.Ex ecute()
at System.Web.HttpApplication .ExecuteSt ep(IExecut ionStep step, Boolean& completedSynchronously)
*****
Error Message: Parameter is not valid.
Stack Trace: at System.Drawing.Bitmap..cto
at System.Drawing.Bitmap..cto
at System.Drawing.Bitmap..cto
at attachment.ProcessRequest(
at System.Web.HttpApplication
at System.Web.HttpApplication
<%@ 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
You need to pass it a callback method
http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage.aspx
http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage.aspx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Looks like CodeCruiser beet me on that one.
ASKER
Sterling, what is different between your code and mine?
Sorry,
It is a small change. I changed a line to:
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.
It is a small change. I changed a line to:
Dim thumbnail As System.Drawing.Bitmap = image.GetThumbnailImage(w, h, Nothing, Nothing)
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.
Open in new window