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:rolleyes:
Printable View
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:rolleyes:
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>
tnx cander!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :):):):):):):):):):)
Just another question:D what is the range of the image quality thingie? 0 to 100?
yep 0 to 100Quote:
Originally posted by MrPolite
tnx cander!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :):):):):):):):):):)
Just another question:D what is the range of the image quality thingie? 0 to 100?