Results 1 to 6 of 6

Thread: Converting BMP to JPG loses quality

  1. #1

    Thread Starter
    Addicted Member WALDO's Avatar
    Join Date
    Aug 2002
    Location
    Swing of Prussia, PA
    Posts
    244

    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:
    1. <% ' import all relevant namespaces %>  
    2. <%@ Page Debug="true" %>
    3. <%@ import namespace="System" %>    
    4. <%@ import namespace="System.Drawing" %>    
    5. <%@ import namespace="System.Drawing.Imaging" %>    
    6. <%@ import namespace="System.IO" %>    
    7. <script runat="server" >
    8.        
    9. Sub ConvertFile()  
    10.            
    11.     ' create New image and bitmap objects. Load the image file and put into a resized bitmap.  
    12.     Dim g As System.Drawing.Image = System.Drawing.Image.FromFile(Request.Form("src")) 
    13.     Dim imgOutput
    14.    
    15.     If Request.Form("Thumbnail") > "" Then
    16.         imgOutput = New Bitmap(g, 130, 90)
    17.     Else
    18.         imgOutput = New Bitmap(g)
    19.     End If
    20.    
    21.    
    22.     ' Set the contenttype  
    23.     Response.ContentType = "image/jpeg"    
    24.  
    25.     ' send the converted image to the viewer    
    26.     imgOutput.save(Response.OutputStream, system.drawing.imaging.imageformat.JPEG) ' output to the user    
    27.            
    28.     ' tidy up  
    29.     g.dispose()
    30.     imgOutput.dispose()    
    31.            
    32. End Sub    
    33.        
    34. </script>  
    35. <%  
    36.        
    37. ' make sure Nothing has gone to the client  
    38. Response.Clear
    39. Call ConvertFile()
    40. Response.End
    41.        
    42. %>

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    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()
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Addicted Member WALDO's Avatar
    Join Date
    Aug 2002
    Location
    Swing of Prussia, PA
    Posts
    244

    Talking 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:
    1. ' Get an array of ImageEncoders..array item 1 is Jpeg.
    2. Dim imgCodecs() As ImageCodecInfo  = ImageCodecInfo.GetImageEncoders()
    3.    
    4. ' Set quality Parameter for the Jpeg codec
    5. Dim imgParams As EncoderParameters = New EncoderParameters(1)
    6. Dim imgQuality As EncoderParameter = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80)
    7. ' Set quality
    8. imgParams.Param(0) = imgQuality
    9.      
    10. ' Render BitMap Stream Back To Client
    11. newBitmap.Save(Response.OutputStream,imgCodecs(1), imgParams)
    Is that right?
    Last edited by WALDO; Dec 13th, 2002 at 10:16 AM.

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    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!
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5

    Thread Starter
    Addicted Member WALDO's Avatar
    Join Date
    Aug 2002
    Location
    Swing of Prussia, PA
    Posts
    244
    Dig It

  6. #6

    Thread Starter
    Addicted Member WALDO's Avatar
    Join Date
    Aug 2002
    Location
    Swing of Prussia, PA
    Posts
    244

    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
  •  



Click Here to Expand Forum to Full Width