![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
However, for JPEG images, even when they are recreated the same size they originally were, appear extremely fuzzy and low quality. Am I forgetting something that is necessary for JPEGs, or is this just something that is unavoidable (I realize that when a JPEG is read and saved it is saved at a slightly lower quality than the original due to the compression algorithm)? Any ideas would be appreciated. Thanks. |
#3
| |||
| |||
|
|
I have created an ASP.NET Page to display images in a specified size and/or display a blank image if no image exists. Here is my code: %@ Page Language="vb" AutoEventWireup="false" CodeBehind="imgresizer.aspx.vb" Inherits="ECommerce.imgresizer" % Imports System.Drawing Imports System.Drawing.Imaging Imports System.Drawing.Drawing2D Partial Public Class imgresizer : Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim imgformat As ImageFormat = ImageFormat.Gif Dim resptype As String = "image/gif" Dim maxwidth As Integer = CInt(Request.QueryString("maxwidth")) Dim maxheight As Integer = CInt(Request.QueryString("maxheight")) Dim resizedbitmap As New Bitmap(maxwidth, maxheight) Dim resizedgraphic As Graphics = Graphics.FromImage(resizedbitmap) resizedgraphic.Clear(Color.White) If System.IO.File.Exists(MapPath("images/" & Request.QueryString("imagefile"))) Then Select Case My.Computer.FileSystem.GetFileInfo(MapPath("images /" & Request.QueryString("imagefile"))).Extension.ToLow er() Case "gif" imgformat = ImageFormat.Gif resptype = "image/gif" Case "jpg", "jpeg" imgformat = ImageFormat.Jpeg resptype = "image/jpeg" Case "png" imgformat = ImageFormat.Png resptype = "image/png" End Select Dim original As Image = System.Drawing.Image.FromFile(MapPath("images/" & Request.QueryString("imagefile"))) If original.Width <= maxwidth AndAlso original.Height <= maxheight Then resizedgraphic.DrawImage(original, (maxwidth - original.Width) \ 2, (maxheight - original.Height) \ 2, original.Width, original.Height) Else Dim scaleratio As Double = Math.Min(maxwidth / original.Width, maxheight / original.Height) Dim newsize As New Size(CInt(scaleratio * original.Width), CInt(scaleratio * original.Height)) resizedgraphic.DrawImage(original, (maxwidth - newsize.Width) \ 2, (maxheight - newsize.Height) \ 2, newsize.Width, newsize.Height) End If End If Response.ClearContent() Response.ContentType = resptype resizedbitmap.Save(Response.OutputStream, imgformat) End Sub End Class However, for JPEG images, even when they are recreated the same size they originally were, appear extremely fuzzy and low quality. Am I forgetting something that is necessary for JPEGs, or is this just something that is unavoidable (I realize that when a JPEG is read and saved it is saved at a slightly lower quality than the original due to the compression algorithm)? Any ideas would be appreciated. Thanks. |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |