Last post Jan 17, 2007 09:17 AM by ViktorBergmanSwe
Sep 25, 2006 04:49 AM|GeorgeClass|LINK
I know there are lots of questions like this but I haven't found an answer for this:
I have images on the same server where the web site is, now, the images are bigger than the size I would like to show, what I want to do is resize the the image (just one) that i want to show in an ImageControl to a smaller size without writting a temporary
image or a new one (on the fly) and without losing quality.
Any ideas ?
Thanks in advance !
Jan 17, 2007 05:09 AM|ViktorBergmanSwe|LINK
You must create a new image on the fly and return it as binary to the client. Furthermore, I would avoid using Asp:Image unless absolutely necessary (html <IMG> tag does the exact same thing without the server overhead). Try this:
Public Sub Page_Load(obj as object, e as eventargs) handles me.load
Dim intWidth As Integer = 300 'Set initial width, this is the base for all the proportions.
Dim bmp As New Bitmap(PATHTOFILE) 'Load bitmap from disk
'Resizing with proportions remaining:
Dim dblratio As Double = bmp.Width / intWidth
bmp = New Bitmap(bmp, intWidth, bmp.Height / dblratio)
'Output the bitmap directly to the browser:
<img src="renderimage.aspx?filename=PATHTOFILE" />
Jan 17, 2007 08:29 AM|surfer5|LINK
Jan 17, 2007 08:37 AM|ViktorBergmanSwe|LINK
You might also want to play with ImageFormat.PNG as your output stream type. Using JPEG produces smaller files but there is often a hit on quality since you are, essentially, resampling the image. This is especially true if you have any text on your image.
Try JPEG first because, if that suits your needs, it is probably better. But you specifically said you didn't want to lose quality and, if the JPEG format makes you loose too much quality, try PNG.
Do you know if ImageFormat.PNG represents PNG24 or just plain PNG? If its the later, then JPEG would still be the most preferrable.
Jan 17, 2007 08:57 AM|surfer5|LINK
I believe it is PNG24 but don't hold me to that. I know the images come through sharp and clear and support alpha channel opacity. I was just looking around to find the answer and some people were showing that the PNG actually produced the smallest stream
size when compared to BMP, JPEG, and GIF formats.
So, just out of curiosity, why do you think JPEG is preferrable?
Jan 17, 2007 09:17 AM|ViktorBergmanSwe|LINK
PNG24 also adds compression, otherwise it would be a raw bitmap so. The reason why i asked is that there is 2 types of PNG, PNG-8 and PNG-24. None of them are meant to replace JPEG, they are supposed to replace the use of the old crappy GIF format. But it
all depends on your needs, is it graphics or pure pictures. When working with pictures theres no reason to work with alpha channel opacity. You can also set the level of compression when using GDI, if done right (100% quality) JPEG return smaller files with
no loss (ofcourse theres a loss but its not significant) compared to PNG-24.
Once again, as always you have to select your format considered to your needs. Graphical items -> go with PNG. Pictures -> stick with JPEG (theres a reason why everybody uses jpeg for pictures).