• Vui lòng đọc nội qui diễn đàn để tránh bị xóa bài viết
  • Tìm kiếm trước khi đặt câu hỏi

Save JPEG images tùy chỉnh chất lượng

Các thủ thuật về truyền thông đa phương tiện, đồ họa

Moderators: tungcan5diop, QUANITGROBEST

User avatar
PhapSuXeko
Mạnh Thường Quân
Mạnh Thường Quân
Posts: 295
Joined: Sun 03/05/2009 7:44 pm
Location: Tây Ninh
Has thanked: 1 time
Been thanked: 10 times
Contact:

Save JPEG images tùy chỉnh chất lượng

Postby PhapSuXeko » Thu 25/02/2010 11:42 am

Thủ thuật: Save JPEG images tùy chỉnh chất lượng
Tác giả: Kourosh Derakshan
Mô tả: Save JPEG images tùy chỉnh chất lượng



Code VB.NET


  1. Dim myImage As Image = ' ... load the image somehow
  2. ' Save the image with a quality of 50%
  3. SaveJpeg(destImagePath, myImage, 50)

[vbnet]' Please do not remove
' Written by Kourosh Derakshan
'

'add this!
Imports System.Drawing.Imaging


' Saves an image as a jpeg image, with the given quality
' Gets:
' path - Path to which the image would be saved.
' quality - An integer from 0 to 100, with 100 being the
' highest quality
Public Shared Sub SaveJpeg(ByVal path As String, ByVal img As Image, ByVal quality As Long)
If ((quality < 0) OrElse (quality > 100)) Then
Throw New ArgumentOutOfRangeException("quality must be between 0 and 100.")
End If

' Encoder parameter for image quality
Dim qualityParam As New EncoderParameter(Encoder.Quality, quality)
' Jpeg image codec
Dim jpegCodec As ImageCodecInfo = GetEncoderInfo("image/jpeg")

Dim encoderParams As New EncoderParameters(1)
encoderParams.Param(0) = qualityParam
img.Save(path, jpegCodec, encoderParams)
End Sub



' Returns the image codec with the given mime type
Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
' Get image codecs for all image formats
Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()

' Find the correct image codec
For i As Integer = 0 To codecs.Length - 1
If (codecs(i).MimeType = mimeType) Then
Return codecs(i)
End If
Next i

Return Nothing
End Function [/vbnet]

Code C#:

[csharp]Image myImage = //... load the image somehow
// Save the image with a quality of 50%
SaveJpeg (destImagePath, myImage, 50); [/csharp]
[csharp]// Please do not remove
// Written by Kourosh Derakshan
//

//add this!
using System.Drawing.Imaging;

/// <summary>
/// Saves an image as a jpeg image, with the given quality
/// </summary>
/// <param name="path">Path to which the image would be saved.</param>
// <param name="quality">An integer from 0 to 100, with 100 being the
/// highest quality</param>
public static void SaveJpeg (string path, Image img, int quality)
{
if (quality<0 || quality>100)
throw new ArgumentOutOfRangeException("quality must be between 0 and 100.");


// Encoder parameter for image quality
EncoderParameter qualityParam =
new EncoderParameter (Encoder.Quality, quality);
// Jpeg image codec
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");

EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;

img.Save (path, jpegCodec, encoderParams);
}

/// <summary>
/// Returns the image codec with the given mime type
/// </summary>
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
// Get image codecs for all image formats
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

// Find the correct image codec
for(int i=0; i<codecs.Length; i++)
if(codecs[i].MimeType == mimeType)
return codecs[i];
return null;
} [/csharp]


Trang chủ phần mềm xem ảnh ImageGlass: http://www.imageglass.org

Return to “[.NET] Âm thanh và Đồ họa”

Who is online

Users browsing this forum: No registered users and 1 guest