如何将大尺寸(MB)照片转换为小尺寸? [英] How can I convert big size(MB) photo to small size?

查看:265
本文介绍了如何将大尺寸(MB)照片转换为小尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我解决这个问题.如何将大尺寸的照片(例如3MB)转换/转换为小尺寸的照片(例如980KB)?使用c#.net

Please help me about this thing. How can I transform/convert a big size photo(e.g. 3MB) to small size(e.g. 980KB)? using c#.net

推荐答案

这称为重新采样.

现在,当您讨论文件大小而不是位图大小时,这意味着您正在谈论压缩图像文件格式.您无法通过所需的输出文件大小来计算转换参数.该大小取决于许多参数:压缩格式的类型和参数以及图像本身的复杂程度.在完成压缩本身之前,无法预测由于压缩而产生的图像大小.

我建议您在将真实位图重新采样为特定大小方面有一定的经验,并将结果保存到文件中以了解输出文件的大小.

要进行重新采样,可以使用类System.Windows.Media.Imaging.TransformedBitmap.
请参阅 http://msdn.microsoft.com/en-us/library /system.windows.media.imaging.transformedbitmap.aspx [ ^ ].请参阅此页面上的代码示例以及属性System.Windows.Media.Imaging.TransformedBitmap.Transform的用法.您需要将变换类的其他实例分配给此属性,以按比例缩小图像.

将其与转换类System.Windows.Media.ScaleTransform一起使用,请参见 http://msdn.microsoft .com/en-us/library/system.windows.media.scaletransform.aspx [
This is called re-sampling.

Now, as you''re discussing file size and not bitmap size, it means you''re talking about compressed image file formats. You cannot calculate the conversion parameter by the desired output file size. This size depends on many parameters: type and parameters of compression format and level of complexity of the image itself. It is impossible to predict the resulting size of the image as a result of compression before the compression itself is done.

I suggest you get some experience with re-sampling of real-life bitmaps to certain sizes and save the results to file to get an idea of output file size.

To do re-sampling, you can use the class System.Windows.Media.Imaging.TransformedBitmap.
See http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.transformedbitmap.aspx[^]. See the code sample on this page and the usage of the property System.Windows.Media.Imaging.TransformedBitmap.Transform. You need to assign the different instance of the transform class to this property to get scale down the image.

Use it with the transform class System.Windows.Media.ScaleTransform, see http://msdn.microsoft.com/en-us/library/system.windows.media.scaletransform.aspx[^].

Nothing can give you full guarantee of the specified resulting file size on output.

—SA


您好,

另一种方法是您使用System.Drawing命名空间的Image对象.
您可以通过仅使用EncoderParameters来加载图像并处理图像,然后将其保存到文件或流中:

Hello,

Another way would be that you use the Image object of the System.Drawing namespace.
You could load the image and process the image by just playing with the EncoderParameters and save it to a file or a stream:

/// <summary>
/// Method for getting the encoder info out of a mime type.
/// </summary>
/// <param name="mimeType">The mime type to search for.</param>
/// <returns>The <see cref="ImageCodecInfo" /> for the supplied mime type. If the mime type is not found, null is returned.</returns>
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;
}
/// <summary>
/// Image processing reduces the file quality to level 25 (of 100) and stores the new image.
/// </summary>
/// <param name="inputFilename">The image to process.</param>
/// <param name="outputFilename">Filename, where the processed image should be saved.</param>
public void ProcessImage(string inputFilename, string outputFilename)
{
    // Load the image from the file.
    Image inputImage = Image.FromFile(inputFilename);
    // Get an ImageCodecInfo object that represents the JPEG codec.
    ImageCodecInfo imageCodecInfo = GetEncoderInfo("image/jpeg");
    // Get the image encoder Guid for the quality property.
    Encoder qualityParamIdentifier = Encoder.Quality;
    // Initialize the encoder parameters array.
    EncoderParameters encoderParameters = new EncoderParameters(1);
    // Define a new EncoderParameter for the quality parameter and set it to quality level 25 (of 100)
    EncoderParameter qualityParam = new EncoderParameter(qualityParamIdentifier, 25L);
    // Add the quality parameter to the encoder.
    encoderParameters.Param[0] = qualityParam;
    // Save the bitmap.
    inputImage.Save(outputFilename, imageCodecInfo, encoderParameters);
}





以下是有关Image.Save和定义图像质量的EncoderParameters的更多信息:
http://msdn.microsoft.com/en-us/library/ytz20d80.aspx [ ^ ].


希望这会有所帮助.

最好的问候,祝您有愉快的一天,并祝您编程愉快,
停止





Here are more informations about the Image.Save and the EncoderParameters defining the image quality:
http://msdn.microsoft.com/en-us/library/ytz20d80.aspx[^].


Hope this helps.

Best regards, have a nice day and happy coding,
Stops


这篇关于如何将大尺寸(MB)照片转换为小尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆