0

In my project, I need to resize the image (e.g.1080 to 720), reduce the file size (may be resolution, 0-100) which is uploaded by client, so that they can then download it.

I can reduce the file size (by resolution) in C#, but not scale and resize. Can anyone tell me how to do that, even using APIs or frameworks.

My reduce resolution code in C#: (from this post)

public static void Saveimage(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);
    string imageType = GetImageFormat(img);
    ImageCodecInfo imageCodec = GetEncoderInfo(imageType);
    EncoderParameters encoderParams = new EncoderParameters(1);
    encoderParams.Param[0] = qualityParam;
    img.Save(path, imageCodec, encoderParams);
}


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;
}

public static string GetImageFormat(Image img)
{
    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
        return "image/jpeg";

    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
        return "image/png";
    if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Tiff))
        return "image/tiff";
    else
        return "image/jpeg";
}

Call:

Saveimage(@"C:\mypath", myImage, 1);
Community
  • 1
  • 1
king yau
  • 500
  • 1
  • 9
  • 28
  • Show your C# code. `Image.Save` method does actually provide many format encoders. – Yeldar Kurmangaliyev Aug 12 '16 at 08:13
  • http://imageprocessor.org/ – Rory McCrossan Aug 12 '16 at 08:13
  • @YeldarKurmangaliyev my code is very like [this](http://stackoverflow.com/a/4161930/5984022) to reduce resolution – king yau Aug 12 '16 at 08:27
  • @kingyau Talking about the code in this answer, it is possible to extend the method with `string encoderName` argument, and then use it instead of `GetEncoderInfo("image/jpeg")`. Change `image/jpeg` in order to save your file to different formats. – Yeldar Kurmangaliyev Aug 12 '16 at 08:29
  • @YeldarKurmangaliyev hi, i updated my question. ya, it is success to reduce the resolution of the image, but do you know how to scale and resize the image in .tiff, .png, and .jepg? many thanks – king yau Aug 12 '16 at 09:48
  • I removed the jQuery tag. jQuery cannot do the things that you have asked, it may serve as an interface but is not a proper tag for this question – Elzo Valugi Aug 12 '16 at 13:57

0 Answers0