terjetyl / Simple.ImageResizer

A simple c# image resizer with ScaleToFit and ScaleToFill using wpf libraries. Resizes to jpg, gif, png, tiff. Demosite is hosted at AppHarbor: http://imageresizer.apphb.com/
58 stars 27 forks source link

encoding Type #8

Open biasmey opened 9 years ago

biasmey commented 9 years ago

I am using ImageResult(filepath, with, height) in my project but it always encode image using type 2. thx

biasmey commented 9 years ago

Could you please change this code in ImageResult and upgrade nuget .

thx

private static string GetResizedImagePath(string filepath, int width, int height) { var resizedPath = filepath;

        if (width <= 0 && height <= 0) 
            return resizedPath;
        resizedPath = filepath.GetPathForResizedImage(width, height);

        if (!Directory.Exists(resizedPath))
        {
            var directoryName = new FileInfo(resizedPath).DirectoryName;
            if (directoryName != null)
                Directory.CreateDirectory(directoryName);
        }

        if (File.Exists(resizedPath)) 
            return resizedPath;

        var encode = GetEncode(filepath);
        var imageResizer = new ImageResizer(filepath);
        if (width > 0 && height > 0)
        {
            imageResizer.Resize(width, height, encode);
        }
        else if (width > 0)
        {
            imageResizer.Resize(width, encode);
        }
        imageResizer.SaveToFile(resizedPath);
        imageResizer.Dispose();
        return resizedPath;
    }

private static ImageEncoding GetEncode(string filepath) { ImageFormat imageFormat; using (var image = new Bitmap(filepath)) { imageFormat = image.RawFormat; }

        if (Equals(imageFormat, ImageFormat.Bmp))
            return ImageEncoding.Bmp;
        if (Equals(imageFormat, ImageFormat.Gif))
            return ImageEncoding.Gif;
        if (Equals(imageFormat, ImageFormat.Jpeg))
            return ImageEncoding.Jpg90;
        if (Equals(imageFormat, ImageFormat.Png))
            return ImageEncoding.Png;
        return Equals(imageFormat, ImageFormat.Tiff) ? ImageEncoding.Tiff : ImageEncoding.Jpg100;
    }