|
-
Dec 12th, 2002, 06:44 PM
#1
Thread Starter
Addicted Member
Converting BMP to JPG loses quality
I'm a little new at ASP.Net, so bear with me. I have a page where users upload bitmaps to my site. I then convert them to JPGs of the same size and destroy the bitmaps. In doing so, they lose color quality a little and become a little blurry. Is there any way around this? Here is my code:
VB Code:
<% ' import all relevant namespaces %>
<%@ Page Debug="true" %>
<%@ import namespace="System" %>
<%@ import namespace="System.Drawing" %>
<%@ import namespace="System.Drawing.Imaging" %>
<%@ import namespace="System.IO" %>
<script runat="server" >
Sub ConvertFile()
' create New image and bitmap objects. Load the image file and put into a resized bitmap.
Dim g As System.Drawing.Image = System.Drawing.Image.FromFile(Request.Form("src"))
Dim imgOutput
If Request.Form("Thumbnail") > "" Then
imgOutput = New Bitmap(g, 130, 90)
Else
imgOutput = New Bitmap(g)
End If
' Set the contenttype
Response.ContentType = "image/jpeg"
' send the converted image to the viewer
imgOutput.save(Response.OutputStream, system.drawing.imaging.imageformat.JPEG) ' output to the user
' tidy up
g.dispose()
imgOutput.dispose()
End Sub
</script>
<%
' make sure Nothing has gone to the client
Response.Clear
Call ConvertFile()
Response.End
%>
-
Dec 13th, 2002, 09:16 AM
#2
you may need to set the jpg quality encoder parameter. This is something i write that should help
Code:
Dim rnd As New Random()
Dim strText As String
' create a font
Dim fnt As Font = New Font("Arial", 12, FontStyle.Bold)
' Load a bitmap based image from a file
Dim newBitmap As Bitmap = New Bitmap(Server.MapPath("back.jpg"))
' This line is for creating a blank bitmap.
'Dim newBitmap As Bitmap = New Bitmap(400,50,PixelFormat.Format32bppARGB)
' Bind the Bitmap to the graphics object
Dim g As Graphics = Graphics.FromImage(newBitmap)
' Get a random number to choose what text to show
Select Case rnd.Next(5)
Case 1
strText = "The place to be!"
Case 2
strText = "What did you expect this message to say?"
Case 3
strText = "How about a nice hot cup of STFU!"
Case 4
strText = "Proving Monkeys CAN fly!"
Case Else
strText = "D'oh!"
End Select
' get the length of the string in pixels
Dim tempg As Graphics = Graphics.FromImage(new Bitmap(1,1))
Dim StringLength As Integer = tempg.MeasureString(strText,fnt).Width
tempg.Dispose
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias
' Draw the string to the Graphics object. Subtract the strings pixel length from the
' images width do determine postioning so that the lst character is always along the
' right edge of the image. Draw shadow first
g.DrawString(strText, fnt, New SolidBrush(Color.Black), newBitmap.Width - StringLength, 65)
g.DrawString(strText, fnt, New SolidBrush(Color.LightSteelBlue), newBitmap.Width - StringLength - 2, 63)
' Get an array of ImageEncoders..array item 1 is Jpeg.
Dim imgCodecs() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders()
' Set quality Parameter for the Jpeg codec
Dim imgParams As EncoderParameters = New EncoderParameters(1)
Dim imgQuality As EncoderParameter = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80)
' Set quality
imgParams.Param(0) = imgQuality
' Render BitMap Stream Back To Client
newBitmap.Save(Response.OutputStream,imgCodecs(1), imgParams)
fnt.Dispose()
g.Dispose()
-
Dec 13th, 2002, 09:51 AM
#3
Thread Starter
Addicted Member
Cander, you rock!
You truly are the master of the DUH.
I'm going to try some of this code today.
It looks like this is the part I should be focusing on:
VB Code:
' Get an array of ImageEncoders..array item 1 is Jpeg.
Dim imgCodecs() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders()
' Set quality Parameter for the Jpeg codec
Dim imgParams As EncoderParameters = New EncoderParameters(1)
Dim imgQuality As EncoderParameter = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80)
' Set quality
imgParams.Param(0) = imgQuality
' Render BitMap Stream Back To Client
newBitmap.Save(Response.OutputStream,imgCodecs(1), imgParams)
Is that right?
Last edited by WALDO; Dec 13th, 2002 at 10:16 AM.
-
Dec 13th, 2002, 09:53 AM
#4
your welcome..oh and btw this line
Dim imgQuality As EncoderParameter = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80)
is where you specify the quality level. Just change 80 to any number 0 -100..100 being highest quality. D'uh!
-
Dec 13th, 2002, 09:56 AM
#5
Thread Starter
Addicted Member
-
Dec 13th, 2002, 10:14 AM
#6
Thread Starter
Addicted Member
Well, it wasn't quite 100%, but
It was a lot closer. Thanks, Cander.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|