473,322 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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

Oct 19 '06 #1
4 3790
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

Oct 19 '06 #2

"Greg" <gr************@hotmail.itwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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/eprogramme...-and-.NET.aspx
Oct 19 '06 #3
Sorry, i've tried to solve the problem with your article but i wasn't
able to
maybe i'm too newbie...

Oct 19 '06 #4
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

Oct 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jawahar Rajan | last post by:
All, I am using a few Input type of "Image" instead of a classic submit button in a form to achieve various tasks for example image1 - add user image2 - modify user image3 - delete user...
5
by: Roger Shrubber | last post by:
I have a page with images that the user can drag from one frame to another. I need them to see a "ghost image" of the image they are dragging, while the original stays put. I use the onmousemove...
2
by: Mr.Clean | last post by:
If I have an Input of type image, it is not listed in the Forms elements when walking to DOM using MSHTML. Is this expected behaviour and how would I get the image input to submit the form NOT...
0
by: Sam | last post by:
I'm wondering if I can pass variables to "TestForm.aspx" with the way I'm using it. It's acting as a jpeg image and looks like: <%@ Page ContentType="image/Jpeg" %> <%@ Import...
0
by: monfu | last post by:
Dear all I have the following code:- System.Drawing.Image src_image = System.Drawing.Image.FromStream(imgStream); Bitmap bitmap = new Bitmap(image_width, image_height,...
21
by: cman | last post by:
does anyone know why i can't generate images with: header("Content-type:image/jpeg"); imagejpeg($img_number); i've tried different examples but i always get a text output as if the header...
3
by: Bri | last post by:
AC97 on WinXP, Image control on a Form and on a Report. I have followed the advice on the following MVPS page: http://www.mvps.org/access/api/api0038.htm This states that after changing the...
2
by: brianflannery | last post by:
Greetings! My situation is this. I currently have a database of which in a form displays jpeg pictures (one at a time) which are linked to the db and stored in a separate folder. I have set up a...
3
by: =?Utf-8?B?V2lsbA==?= | last post by:
I have an image "button" which causes a postback. I handle the "click" with If Request.Form("btnBU7WD6_Submit.X") 0 Then... this works great. However, when a user double-clicks the image...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.