Response.ContentType = "image/jpeg" causes Page_Load Event twice

G

Greg

I've searched everywhere for a solution
Microsoft .NET Framework V 2.0.50727
AutoEventWireup="false"

image2.aspx resize an image on the fly but Page_Load is triggered
twice:

Any suggestion?

Here is the code behind:

---------------------------------------------------------
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Text
Imports System.Drawing.Drawing2D


Partial Class ridimensiona_immagine_al_volo_image2
Inherits System.Web.UI.Page

'Prende un mimeType e ne restituisce il codec corrispondente.
Function GetEncoder(ByVal mimeType As String) As ImageCodecInfo

Dim codecs() As ImageCodecInfo =
ImageCodecInfo.GetImageEncoders() ' elenco codecs
Dim returnValue As ImageCodecInfo = Nothing ' codec da
resituire

'Ciclo sui codecs e verifico quale corrisponde al mimeType
passato
For Each codec As ImageCodecInfo In codecs
If codec.MimeType = mimeType Then
returnValue = codec
End If
Next

Return returnValue

End Function


Function Get_Thumbnail(ByVal Source_Img As System.Drawing.Image,
ByVal height As Integer, ByVal width As Integer) As
System.Drawing.Image

Dim Orginal_Width As Integer
Dim Orginal_Height As Integer
Dim Aspect_ratio As Integer
Orginal_Width = Source_Img.Width
Orginal_Height = Source_Img.Height
Aspect_ratio = Orginal_Height / height

Dim thumbnail As System.Drawing.Image = New Bitmap(width,
height)
Dim objGraphics As System.Drawing.Graphics
objGraphics = System.Drawing.Graphics.FromImage(thumbnail)
objGraphics.InterpolationMode =
InterpolationMode.HighQualityBicubic
objGraphics.SmoothingMode = SmoothingMode.HighQuality
objGraphics.PixelOffsetMode = PixelOffsetMode.HighQuality
objGraphics.CompositingQuality = CompositingQuality.HighQuality
If Request.QueryString("thumb") = "false" Then
Return Source_Img
Else
objGraphics.DrawImage(Source_Img, 0, 0, width, height)
Return thumbnail

End If
End Function

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim originalImg, thumb As System.Drawing.Image

'Dim imagePath As String = Request.QueryString("img")
'Dim maxHeight As Integer = Request.QueryString("h")
'Dim maxWidth As Integer = Request.QueryString("w")
Dim imagePath As String = "jpeg.jpg"
Dim maxHeight As Integer = 200
Dim maxWidth As Integer = 200

'Tento il ridimensionamento on the fly
Try
'Carico l'immagine da ridimensionare
originalImg =
Drawing.Image.FromFile(Server.MapPath(imagePath))

Dim newHeight As Integer = originalImg.Height
Dim newWidth As Integer = originalImg.Width

'Calcolo le dimensionio con le dovute proporzioni
If originalImg.Height > maxHeight Then
newHeight = maxHeight
newWidth = (originalImg.Width * newHeight) /
originalImg.Height

If newWidth > maxWidth Then
newWidth = maxWidth
newHeight = (originalImg.Height * newWidth) /
originalImg.Width
End If
ElseIf originalImg.Width > maxWidth Then
newWidth = maxWidth
newHeight = (originalImg.Height * newWidth) /
originalImg.Width

If newHeight > maxHeight Then
newHeight = maxHeight
newWidth = (originalImg.Width * newHeight) /
originalImg.Height
End If
End If

'Imposto il content type dell'output
Response.ContentType = "image/jpeg"

'Ottengo la versione Thumbnail dell'immaine originale
thumb = Get_Thumbnail(originalImg, newHeight, newWidth)

'Imposto il metodo di codifica perchè non si abbiano
perdite di qualità
Dim codecEncoder As ImageCodecInfo =
GetEncoder("image/jpeg")
Dim quality As Integer = 100
Dim encodeParams As New EncoderParameters(1)
Dim qualityParam As New
EncoderParameter(Imaging.Encoder.Quality, quality)
encodeParams.Param(0) = qualityParam

'Restituisco l'immagine all'output in streaming
thumb.Save(Response.OutputStream, codecEncoder,
encodeParams)

'Killo gli oggetti utilizzati
originalImg.Dispose()
thumb.Dispose()
Catch
'Ci sono stati problemi nella creazione del thumbnail
quindi creo al volo un'immagine di avviso (potrei definirne una default
e caricarla)
Dim objBMP As System.Drawing.Bitmap = New Bitmap(110, 110)
Dim objGraphics As System.Drawing.Graphics
objGraphics = System.Drawing.Graphics.FromImage(objBMP)
objGraphics.Clear(Color.Red)
Dim objFont As System.Drawing.Font = New Font("Arial", 16,
FontStyle.Bold)
objGraphics.DrawString("No Image", objFont, Brushes.White,
3, 40)
Response.ContentType = "image/GIF"
objBMP.Save(Response.OutputStream, ImageFormat.Gif)

' Kill our objects
objFont.Dispose()
objGraphics.Dispose()
objBMP.Dispose()
End Try

End Sub
End Class
 
G

Greg

Step on:
I've noticed that the problems seem to be the creation of the new
bitmap:
In the Function Get_Thumbnail changing:
Return thumbnail
With:
Return Source_Img

Solve the problem of Page_Load called twice, but obviously i still have
the problem of producing a high quality image.
originalImg.GetThumbnailImage(originalImg.Width, originalImg.Height,
Nothing, IntPtr.Zero)
solve the problem but the resul image is very low quality.

Waiting for your answer

thanks

Gregorio

P.S. sorry for my english but i'm italian
 
E

Egbert Nierop \(MVP for IIS\)

Greg said:
Step on:
I've noticed that the problems seem to be the creation of the new
bitmap:
In the Function Get_Thumbnail changing:
Return thumbnail
With:
Return Source_Img

Solve the problem of Page_Load called twice, but obviously i still have
the problem of producing a high quality image.
originalImg.GetThumbnailImage(originalImg.Width, originalImg.Height,
Nothing, IntPtr.Zero)
solve the problem but the resul image is very low quality.

Waiting for your answer

Hi Greg,

If you use GetThumbnailImage you use a shortcut for a lot of code, that
'assumes' your image quality etc. So a better quality is not to be expected.

You'll have to do it all in full, yourselves (including the image quality).

I have no idea whether or not you need to *create* an image from scratch, or
that you just have a BLOB which contains byte data with an image?

Here is some info how to do it from scratch!

http://technolog.nl/blogs/eprogramm...ate-Gradient-Buttons-using-ASPX-and-.NET.aspx
 
G

Greg

Sorry, i've tried to solve the problem with your article but i wasn't
able to
maybe i'm too newbie...
 
G

Greg

Ok, problem solved
it was fault of the address bar that load a preview of the image (in
firefox) and causes two Page_Load event.

thanks anyway
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top