HighTechTalks DotNet Forums  

JPEG image appearing low quality when copied

Dotnet Framework (Drawing) microsoft.public.dotnet.framework.drawing


Discuss JPEG image appearing low quality when copied in the Dotnet Framework (Drawing) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Nathan Sokalski
 
Posts: n/a

Default JPEG image appearing low quality when copied - 11-20-2007 , 09:48 AM






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.


Reply With Quote
  #2  
Old   
Michael C
 
Posts: n/a

Default Re: JPEG image appearing low quality when copied - 11-22-2007 , 03:11 PM






"Nathan Sokalski" <njsokalski (AT) hotmail (DOT) com> wrote

Quote:
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.
You can set the quality of a jpg using EncoderParameters. Look up help on
this or search google.
Quote:



Reply With Quote
  #3  
Old   
Bob Powell [MVP]
 
Posts: n/a

Default Re: JPEG image appearing low quality when copied - 11-24-2007 , 05:04 PM



The only way to save the same image quality as the original is to always
save your jpegs at 100%.

The problem is that JPEG compression is a lossy system so the more times you
copy and save, the more the image degrades.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


"Nathan Sokalski" <njsokalski (AT) hotmail (DOT) com> wrote

Quote:
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.


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.