Results 1 to 4 of 4

Thread: *resolved* How can I change the image quality?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    *resolved* How can I change the image quality?

    I'm trying to open a JPG file and to change the quality (compression rate) of the image and save it.... it should be possible becuase I've heard that .NET fully supports JPEG and stuff... anyone knows how
    Last edited by MrPolite; Jul 16th, 2002 at 07:27 PM.

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    da! You can use the EnocderParameters to set ijmage quality, compression, gif version, rotation, and some others. Here is the code for my banner in my sig, which creates dynamic text on my banner and sets the image quality to 80. You can change the line with Response.OutputStream where it saves the final image to an actual hard drive path.

    Code:
    <%@ Page Language="VB" ContentType="image/jpeg" %>
    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.Drawing" %>
    <%@ Import Namespace="System.Drawing.Drawing2D" %>
    <%@ Import Namespace="System.Drawing.Imaging" %>
    
    <script language="VB" runat="server">
    
    Sub Page_Load(Sender as Object, E as EventArgs) 
    	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()
    End Sub
    
    </script>
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    tnx cander!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    Just another question what is the range of the image quality thingie? 0 to 100?

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Originally posted by MrPolite
    tnx cander!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    Just another question what is the range of the image quality thingie? 0 to 100?
    yep 0 to 100
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

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