Thumbnail Image Quality Issues

  • Thread starter Thread starter Chris D
  • Start date Start date
C

Chris D

Hi,

I have an application where a user uploads an image and I create two
thumbnails. One a small image and the second is a larger image but still
smaller then the original. I store these in SQL 2000 server.

The quality of the small thumbnail is ok, but the quality of the larger
image is terrible. I tried both Jpeg and Gif formats and there is no
difference.

Is there any way to control the compression level of a Jpeg??

If any one can give me some suggestions I would greatly appreciate it.

Chris
 
Yes there is a Quality level for jpeg images. Start out simple and create
an application that takes an image, modifies it (height, width, quality)
then save it or stram it to the browser in different formats including the
original and see how it looks. If your images are photos then I would stick
with jpegs. I don't know what method/control you are using but this is a
good starting point.

Chris D said:
Hi,

I have an application where a user uploads an image and I create two
thumbnails. One a small image and the second is a larger image but still
smaller then the original. I store these in SQL 2000 server.
image is terrible. I tried both Jpeg and Gif formats and there is no
difference.
The quality of the small thumbnail is ok, but the quality of the larger>
Is there any way to control the compression level of a Jpeg??
 
I have tried storing it in png with no visible improvement.

Is there maybe a specific way of converting it??

I don't think there is a problem with the GetThumbNail method as it returns
a good quality image if I get the image from the aspx page. the quality
worsens drastically when I write the image to a stream and to SQL server and
then retrieve it again.

Any ideas on that

Chris
 
It looks like you are using the image (GDI) class to do your conversions. I
don't think GetThumbNail by default does not do anything with the image
quality level. Hecnce you are seeing a decent image. What method are you
using to convert your other image? fyi, you can try the GetThumbNail and
specify a height and width for your second image if you really want.
Somethimes images have a thumbnails embeeded in the file in which
GetThumbNail returns that other wise GetThumbNail will just scale your image
to your specification (height, width). You will have to post code for me to
help you.
 
Dan,

Here is the code I'm using to do this. Thanks in advance for taking a look
at this for me. I'm out of ideas.

'Declarations
Dim fsImageSize As Integer
Dim fsImageContent As Byte()
Dim fsImageType As String
Dim tnThumbnailSize As Integer
Dim tnThumbnailContent As Byte()
Dim tnThumbnailType As String
Dim orImage As System.Drawing.Image
Dim fsImage As System.Drawing.Image
Dim tnImage As System.Drawing.Image
Dim WHRatio As Single
Dim ms As New System.IO.MemoryStream()
Dim ms2 As New System.IO.MemoryStream()

'get image from submit and calculate WH ration
orImage = System.Drawing.Image.FromStream(Picture.PostedFile.InputStream)
WHRatio = orImage.Width / orImage.Height

'Resize image and create Thumbnail
fsImage = ResizeImage(orImage, 500 / WHRatio, 500) '---- See below for the
resizeimage function
tnImage = ResizeImage(orImage, 125 / WHRatio, 125) '---- See below for the
resizeimage function

'Convert both images to binary for save operation
fsImage.Save(ms, Imaging.ImageFormat.Bmp)
fsImageSize = ms.Length
ReDim fsImageContent(fsImageSize)
ms.Seek(0, IO.SeekOrigin.Begin)
ms.Read(fsImageContent, 0, fsImageContent.Length)

tnImage.Save(ms2, Imaging.ImageFormat.Bmp)
tnThumbnailSize = ms2.Length
ReDim tnThumbnailContent(tnThumbnailSize)
ms2.Seek(0, IO.SeekOrigin.Begin)
ms2.Read(tnThumbnailContent, 0, tnThumbnailContent.Length)

Then I dump it to the DB via Stored Procedure:

..Parameters.Add("@ImageContent", SqlDbType.Image).Value = fsImageContent
..Parameters.Add("@ThumbnailContent", SqlDbType.Image).Value =
tnThumbnailContent

Resize Function:

Shared Function ResizeImage(ByVal p_objImage As System.Drawing.Image, ByVal
p_intHeight As Integer, ByVal p_intWidth As Integer) As System.Drawing.Image
Dim intHeight As Integer
Dim intWidth As Integer
intHeight = p_objImage.Height
intWidth = p_objImage.Width
If p_intHeight > -1 And p_intWidth > -1 Then
intHeight = p_intHeight
intWidth = p_intWidth
ElseIf p_intHeight > -1 Then
If p_intWidth > -1 Then
intWidth = p_intWidth
Else
intWidth = CInt(intWidth / (intHeight / p_intHeight))
End If
intHeight = p_intHeight
End If
If p_intWidth > -1 Then
If p_intHeight > -1 Then
intHeight = p_intHeight
Else
intHeight = CInt(intHeight / (intWidth / p_intWidth))
End If
intWidth = p_intWidth
End If
Return p_objImage.GetThumbnailImage(intWidth, intHeight, Nothing, Nothing)
End Function

Thaks again ... Chris
 
Here are some routines I just put together. Anything with "img..." is a
image parameter that you need to pass in. I am confused on your height to
width scaling but I think you have the right idea. Play around with that a
bit more but for now leave it out and try these routunies. I would not use
GetThumbnail for images bigger than 100x100.

Sub StreamImage(imgHeight as integer, imgWidth as integer, imgFileName as
String, imgQuality as integer)

Dim myNewImage As System.drawing.Image
Dim myImageCodecInfo As ImageCodecInfo
Dim myEncoder As Encoder
Dim myEncoderParameter As EncoderParameter
Dim myEncoderParameters As EncoderParameters

Try
Select Case imgQuality

Case Is = 0

If imgWidth = 0 Then imgWidth = 200
If imgHeight = 0 Then imgHeight = 200
myNewImage = mgAdjustBitmap(imgFileName, imgWidth,
imgHeight)

myNewImage.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg)

Case Else

If imgWidth = 0 Then imgWidth = 90
If imgHeight = 0 Then imgHeight = 90
myNewImage = mgGetThumbNail(imgFileName, imgWidth,
imgHeight)

'Image Type
'myImageCodecInfo = GetEncoderInfo("image/tiff")
myImageCodecInfo = GetEncoderInfo("image/jpeg")

'Image Qualiy for Jpeg only
myEncoder = Encoder.Quality
myEncoderParameters = New EncoderParameters(1)
'25L is the quality level Hard to 25 coded for now
'but I would use imgQuality to create the 25L for
example
myEncoderParameter = New EncoderParameter(myEncoder,
25L)
myEncoderParameters.Param(0) = myEncoderParameter

'myNewImage.Save(Response.OutputStream,
ImageFormat.Jpeg)
myNewImage.Save(Response.OutputStream, myImageCodecInfo,
myEncoderParameters)

End Select

Catch

End Try

End Sub

Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo

Dim J As Integer
Dim encoders As ImageCodecInfo()
encoders = ImageCodecInfo.GetImageEncoders()
For J = 0 To encoders.Length
If encoders(J).MimeType = mimeType Then
GetEncoderInfo = encoders(J)
Exit For
End If
Next

End Function

Function mgGetThumbNail(ByVal strFile As String, ByVal intWidth As
Integer, ByVal intHeight As Integer) As System.drawing.Image

Dim myBitmap As Bitmap = New Bitmap(strFile)
Return myBitmap.GetThumbnailImage(intWidth, intHeight, Nothing,
Nothing)
myBitmap.Dispose()

End Function

Function mgAdjustBitmap(ByVal strFile As String, ByVal intHorz As
Integer, ByVal intVert As Integer) As Bitmap

Dim mysize As Size
Dim myBitmap As Bitmap = New Bitmap(strFile)
If intHorz > 0 Then
mysize = New Size(intHorz, intVert)
Else
mysize = New Size(myBitmap.Width, myBitmap.Height)
End If
Dim myImg As Bitmap = New Bitmap(myBitmap, mysize)
Return myImg
myBitmap.Dispose()

End Function
 
Back
Top