c#将图像格式转换为jpg
c#
image
image-processing
11
0

我需要从用户处获得一张具有不同格式扩展名的图片,并且我希望始终将其另存为“ jpg”,以便于处理。在没有提高质量的情况下,在C#中有什么好方法吗?

参考资料:
Stack Overflow
收藏
评论
共 4 个回答
高赞 时间 活跃

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

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);
收藏
评论

尝试这个:

       public static class BitmapExtension
       {
          public static void Save(this Bitmap bitmap, String fileName, ImageFormat imageFormat, long quality = 75L)
          {
             using (var encoderParameters = new EncoderParameters(1))
             using (encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality))
             {
                ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

                bitmap.Save(fileName, codecs.Single(codec => codec.FormatID == imageFormat.Guid), encoderParameters);
             }
          }
       }

       ...

       Bitmap bitmap = new Bitmap("myImage.bmp");

       bitmap.Save("myImage.jpg", ImageFormat.Jpeg, 50L);
收藏
评论

Windows GDI +提供了Image类Bitmap类 ,用于在内存中存储图像并在内存中处理图像。 GDI +在图像编码器的帮助下将图像写入磁盘文件,并在图像解码器的帮助下从磁盘文件加载图像。

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

请遵循Image.Save方法(字符串,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;
        }

    }

希望有帮助。

收藏
评论

来自: http : //msdn.microsoft.com/zh-cn/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;
}
收藏
评论
新手导航
  • 社区规范
  • 提出问题
  • 进行投票
  • 个人资料
  • 优化问题
  • 回答问题