c#将图像格式转换为jpg

时间:2012-06-05 09:31:28

标签: c# image image-processing

我需要从用户那里获得一张具有不同格式扩展名的图片,并且我希望始终将其保存为“jpg”,以便于处理。 在没有武装质量的情况下,有没有一种好的方法可以做到这一点?

4 个答案:

答案 0 :(得分:23)

来自:http://msdn.microsoft.com/en-us/library/system.drawing.imaging.encoderparameter.aspx

private void VaryQualityLevel()
{
// Get a bitmap.
Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);

// Create an Encoder object based on the GUID
// for the Quality parameter category.
System.Drawing.Imaging.Encoder myEncoder =
    System.Drawing.Imaging.Encoder.Quality;

// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
EncoderParameters myEncoderParameters = new EncoderParameters(1);

EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 
    50L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jpgEncoder, 
    myEncoderParameters);

myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityHundred.jpg", jpgEncoder, 
    myEncoderParameters);

// Save the bitmap as a JPG file with zero quality level compression.
myEncoderParameter = new EncoderParameter(myEncoder, 0L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityZero.jpg", jpgEncoder, 
    myEncoderParameters);

}

...

private ImageCodecInfo GetEncoder(ImageFormat format)
{

    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

    foreach (ImageCodecInfo codec in codecs)
    {
        if (codec.FormatID == format.Guid)
        {
            return codec;
        }
    }
    return null;
}

答案 1 :(得分:12)

我使用这个简单的扩展来转换流,它所做的只是将其转换而不做任何质量。

public static Stream ConvertImage(this Stream originalStream, ImageFormat format)
{
                var image = Image.FromStream(originalStream);

                var stream = new MemoryStream();
                image.Save(stream, format);
                stream.Position = 0;
                return stream;
}

用法:

var outputStream = gifStream.ConvertImage(ImageFormat.Png);

答案 2 :(得分:2)

  

Windows GDI +提供Image classBitmap class进行存储   内存中的图像和内存中的图像处理。 GDI +写图像   借助图像编码器到磁盘文件并加载图像   借助图像解码器的磁盘文件。

检查Image.Save Method重载以实现您的功能。它支持ImageFormat Class类型的各种图像格式。

关注Image.Save Method (String, ImageCodecInfo, EncoderParameters)上的示例,该示例将允许您使用编码等保存Jpg文件。

代码段:

using System;
using System.Drawing;
using System.Drawing.Imaging;
class Example_SetJPEGQuality
{
    public static void Main()
    {
        Bitmap myBitmap;
        ImageCodecInfo myImageCodecInfo;
        Encoder myEncoder;
        EncoderParameter myEncoderParameter;
        EncoderParameters myEncoderParameters;

        // Create a Bitmap object based on a BMP file.
        myBitmap = new Bitmap("Shapes.bmp");

        // Get an ImageCodecInfo object that represents the JPEG codec.
        myImageCodecInfo = GetEncoderInfo("image/jpeg");

        // Create an Encoder object based on the GUID

        // for the Quality parameter category.
        myEncoder = Encoder.Quality;

        // Create an EncoderParameters object.

        // An EncoderParameters object has an array of EncoderParameter

        // objects. In this case, there is only one

        // EncoderParameter object in the array.
        myEncoderParameters = new EncoderParameters(1);

        // Save the bitmap as a JPEG file with quality level 25.
        myEncoderParameter = new EncoderParameter(myEncoder, 25L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        myBitmap.Save("Shapes025.jpg", myImageCodecInfo, myEncoderParameters);

        // Save the bitmap as a JPEG file with quality level 50.
        myEncoderParameter = new EncoderParameter(myEncoder, 50L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        myBitmap.Save("Shapes050.jpg", myImageCodecInfo, myEncoderParameters);

        // Save the bitmap as a JPEG file with quality level 75.
        myEncoderParameter = new EncoderParameter(myEncoder, 75L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        myBitmap.Save("Shapes075.jpg", myImageCodecInfo, myEncoderParameters);
    }

//编解码器信息

  private static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for(j = 0; j < encoders.Length; ++j)
            {
                if(encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }

    }

希望这有帮助..

答案 3 :(得分:0)

您可以使用Image API在内存中创建图像,然后将其保留在JPG中。 http://msdn.microsoft.com/en-us/library/ms142147.aspx