CKFinder.Connector.ImageTools.ResizeImage C# (CSharp) Method

ResizeImage() public static method

public static ResizeImage ( string sourceFile, string targetFile, int maxWidth, int maxHeight, bool preserverAspectRatio, int quality ) : bool
sourceFile string
targetFile string
maxWidth int
maxHeight int
preserverAspectRatio bool
quality int
return bool
        public static bool ResizeImage( string sourceFile, string targetFile, int maxWidth, int maxHeight, bool preserverAspectRatio, int quality )
        {
            System.Drawing.Image sourceImage;

            System.IO.Stream sourceStream = Achilles.Acme.Storage.IO.File.OpenRead( sourceFile );
            System.IO.Stream targetStream = Achilles.Acme.Storage.IO.File.OpenWrite( targetFile );

            try
            {
                sourceImage = System.Drawing.Image.FromStream( sourceStream );
            }
            catch ( OutOfMemoryException )
            {
                // This is not a valid image. Do nothing.
                return false;
            }

            // If 0 is passed in any of the max sizes it means that that size must be ignored,
            // so the original image size is used.
            maxWidth = maxWidth == 0 ? sourceImage.Width : maxWidth;
            maxHeight = maxHeight == 0 ? sourceImage.Height : maxHeight;

            if ( sourceImage.Width <= maxWidth && sourceImage.Height <= maxHeight )
            {
                sourceImage.Dispose();

                if ( sourceFile != targetFile )
                    Achilles.Acme.Storage.IO.File.Copy( sourceFile, targetFile );

                return true;
            }

            Size oSize;
            if ( preserverAspectRatio )
            {
                // Gets the best size for aspect ratio resampling
                oSize = GetAspectRatioSize( maxWidth, maxHeight, sourceImage.Width, sourceImage.Height );
            }
            else
                oSize = new Size( maxWidth, maxHeight );

            System.Drawing.Image oResampled;

            if (sourceImage.PixelFormat == PixelFormat.Indexed || sourceImage.PixelFormat == PixelFormat.Format1bppIndexed || sourceImage.PixelFormat == PixelFormat.Format4bppIndexed || sourceImage.PixelFormat == PixelFormat.Format8bppIndexed || sourceImage.PixelFormat.ToString() == "8207")
                oResampled = new Bitmap( oSize.Width, oSize.Height, PixelFormat.Format24bppRgb );
            else
                oResampled = new Bitmap( oSize.Width, oSize.Height, sourceImage.PixelFormat );

            // Creates a Graphics for the oResampled image
            Graphics oGraphics = Graphics.FromImage( oResampled );

            // The Rectangle that holds the Resampled image size
            Rectangle oRectangle;

            // High quality resizing
            if ( quality > 80 )
            {
                oGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                // If HighQualityBicubic is used, bigger Rectangle is required to remove the white border
                oRectangle = new Rectangle( -1, -1, oSize.Width +1, oSize.Height +1 );
            }
            else
                oRectangle = new Rectangle( 0, 0, oSize.Width, oSize.Height );

            // Place a white background (for transparent images).
            oGraphics.FillRectangle( new SolidBrush( Color.White ), oRectangle );

            // Draws over the oResampled image the resampled Image
            oGraphics.DrawImage( sourceImage, oRectangle );

            sourceImage.Dispose();

            String extension = System.IO.Path.GetExtension( targetFile ).ToLower();

            if ( extension == ".jpg" || extension == ".jpeg" )
            {
                ImageCodecInfo oCodec = GetJpgCodec();

                if ( oCodec != null )
                {
                    EncoderParameters aCodecParams = new EncoderParameters( 1 );
                    aCodecParams.Param[0] = new EncoderParameter( Encoder.Quality, quality );
                    oResampled.Save( targetStream, oCodec, aCodecParams );
                }
                else
                {
                    // TJT: Fixme - stream save requires oCodec?
                    //oResampled.Save( targetStream, oCodec );
                }
            }
            else
            {
                switch ( extension )
                {
                    case ".gif":
                        try
                        {
                            // Use a proper palette
                            OctreeQuantizer quantizer = new OctreeQuantizer( 255, 8 );
                            using ( Bitmap quantized = quantizer.Quantize( oResampled ) )
                            {
                                quantized.Save( targetStream, System.Drawing.Imaging.ImageFormat.Gif );
                            }
                        }
                        catch ( System.Security.SecurityException )
                        {
                            // The calls to Marshal might fail in Medium trust, save the image using the default palette
                            oResampled.Save( targetStream, System.Drawing.Imaging.ImageFormat.Png );
                        }
                        break;

                    case ".png":
                        oResampled.Save( targetStream, System.Drawing.Imaging.ImageFormat.Png );
                        break;

                    case ".bmp":
                        oResampled.Save( targetStream, System.Drawing.Imaging.ImageFormat.Bmp );
                        break;
                }
            }
            oGraphics.Dispose();
            oResampled.Dispose();

            targetStream.Close();
            sourceStream.Close();

            return true;
        }