Ett exempel på JPEG-komprimering
Hej
Jag håller själv på med en del test av denna funktion.
Nedanstående kan användas med följande exempel:
Imports System.Drawing
Imports System.Drawing.Imaging
/.../
Dim i As System.Drawing.Image = _
System.Drawing.Image.FromFile(Server.MapPath("images\some.tif"))
SaveJPGWithCompressionSetting(i, Server.MapPath("images\some.jpg"), 90)
Komprimering 1 - 100 där 100 är högsta kvalitet (lägst komprimering)
Följande exempel är saxat från MS KB:
Setting the JPEG Quality Factor.
The JPEG encoder in System.Drawing gives an encoder parameter for setting the JPEG quality factor when you save a JPEG file. This encoder parameter complies with the convention of using an integer with a range of 1 to 100.
The value of this encoder parameter influences the creation and use of quantization tables related to the suggested quality factor.
The following code sample shows how this encoder parameter is used to save a JPEG file with a specified quality factor:
Private 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
Return encoders(j)
End If
Next j
Return Nothing
End Function
Private Sub SaveJPGWithCompressionSetting(ByVal image As Image, ByVal szFileName As String, ByVal lCompression As Long)
Dim eps As EncoderParameters = New EncoderParameters(1)
eps.Param(0) = New EncoderParameter(Encoder.Quality, lCompression)
Dim ici As ImageCodecInfo = GetEncoderInfo("image/jpeg")
image.Save(szFileName, ici, eps)
End Sub
Retrieve the Quality Factor from a JPEG File The quality factor is not stored directly in the JPEG file, so you cannot read the quality factor from the file.
However, you can read the quantization tables from the JPEG file
by using the PropertyItems property on the Image class. But even with the quantization tables, you cannot always determine a quality factor.
You might be able to determine the quality factor by comparing the quantization tables against the "standard" IJG-generated tables.
However, because some applications may use custom tables, you will not always find a match. For more information about the quantization tables, see the "References" section.