2009-06-20 13:49:35 +00:00
using System ;
using System.Drawing.Drawing2D ;
using System.Drawing.Imaging ;
using System.Drawing ;
2013-12-16 13:35:18 +00:00
using Umbraco.Core.IO ;
2009-06-20 13:49:35 +00:00
using System.IO ;
2013-12-16 13:35:18 +00:00
2009-06-20 13:49:35 +00:00
namespace umbraco.editorControls.imagecropper
{
2013-09-25 19:03:05 +10:00
[Obsolete("IDataType and all other references to the legacy property editors are no longer used this will be removed from the codebase in future versions")]
2009-06-20 13:49:35 +00:00
public class ImageTransform
{
2013-12-16 13:35:18 +00:00
private static readonly MediaFileSystem _fs = FileSystemProviderManager . Current . GetFileSystemProvider < MediaFileSystem > ( ) ;
2009-06-20 13:49:35 +00:00
public static void Execute ( string sourceFile , string name , int cropX , int cropY , int cropWidth , int cropHeight , int sizeWidth , int sizeHeight , long quality )
{
2013-12-16 13:35:18 +00:00
if ( ! _fs . FileExists ( sourceFile ) ) return ;
2009-06-20 13:49:35 +00:00
2013-12-16 13:35:18 +00:00
string path = string . Empty ;
2009-06-20 13:49:35 +00:00
2013-12-16 13:35:18 +00:00
//http or local filesystem
if ( sourceFile . Contains ( "/" ) )
path = sourceFile . Substring ( 0 , sourceFile . LastIndexOf ( '/' ) ) ;
else
path = sourceFile . Substring ( 0 , sourceFile . LastIndexOf ( '\\' ) ) ;
2009-06-20 13:49:35 +00:00
2013-12-16 13:35:18 +00:00
// TODO: Make configurable and move to imageInfo
//if(File.Exists(String.Format(@"{0}\{1}.jpg", path, name))) return;
2009-06-20 13:49:35 +00:00
2013-12-16 13:35:18 +00:00
//Do we need this check as we are always working with images that are already in a folder??
//DirectoryInfo di = new DirectoryInfo(path);
//if (!di.Exists) di.Create();
2014-07-10 11:45:57 +10:00
using ( var stream = _fs . OpenFile ( sourceFile ) )
using ( var image = Image . FromStream ( stream ) )
using ( var croppedImage = CropImage ( image , new Rectangle ( cropX , cropY , cropWidth , cropHeight ) ) )
using ( var resizedImage = ResizeImage ( croppedImage , new Size ( sizeWidth , sizeHeight ) ) )
using ( var b = new Bitmap ( resizedImage ) )
2009-06-20 13:49:35 +00:00
{
2014-07-10 11:45:57 +10:00
SaveJpeg ( String . Format ( "{0}/{1}.jpg" , path , name ) , b , quality ) ;
2009-06-20 13:49:35 +00:00
}
2014-07-10 11:45:57 +10:00
2009-06-20 13:49:35 +00:00
}
2013-04-22 11:12:03 -12:00
private static void SaveJpeg ( string path , Bitmap img , long quality )
2009-06-20 13:49:35 +00:00
{
// Encoder parameter for image quality
EncoderParameter qualityParam = new EncoderParameter ( Encoder . Quality , quality ) ;
// Jpeg image codec
2013-04-22 11:12:03 -12:00
ImageCodecInfo jpegCodec = GetEncoderInfo ( "image/jpeg" ) ;
2009-06-20 13:49:35 +00:00
if ( jpegCodec = = null )
return ;
EncoderParameters encoderParams = new EncoderParameters ( 1 ) ;
encoderParams . Param [ 0 ] = qualityParam ;
2014-07-10 11:45:57 +10:00
using ( var fileStream = new MemoryStream ( ) )
{
img . Save ( fileStream , jpegCodec , encoderParams ) ;
fileStream . Position = 0 ;
_fs . AddFile ( path , fileStream , true ) ;
}
2013-12-16 13:35:18 +00:00
2009-06-20 13:49:35 +00:00
}
2013-04-22 11:12:03 -12:00
private static ImageCodecInfo GetEncoderInfo ( string mimeType )
2009-06-20 13:49:35 +00:00
{
// 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 ;
}
2013-04-22 11:12:03 -12:00
private static Image CropImage ( Image img , Rectangle cropArea )
2009-06-20 13:49:35 +00:00
{
2013-04-22 11:12:03 -12:00
if ( cropArea . Right > img . Width )
cropArea . Width - = ( cropArea . Right - img . Width ) ;
2009-06-20 13:49:35 +00:00
2013-04-22 11:12:03 -12:00
if ( cropArea . Bottom > img . Height )
cropArea . Height - = ( cropArea . Bottom - img . Height ) ;
2009-06-20 13:49:35 +00:00
2013-05-31 05:46:10 -02:00
var bmpCrop = new Bitmap ( cropArea . Width , cropArea . Height ) ;
using ( var graphics = Graphics . FromImage ( bmpCrop ) )
{
graphics . DrawImage ( img , new Rectangle ( 0 , 0 , bmpCrop . Width , bmpCrop . Height ) , cropArea , GraphicsUnit . Pixel ) ;
}
2013-04-22 11:12:03 -12:00
return bmpCrop ;
}
2009-06-20 13:49:35 +00:00
2013-04-22 11:12:03 -12:00
private static Image ResizeImage ( Image imgToResize , Size size )
{
2009-06-20 13:49:35 +00:00
int destWidth = size . Width ;
int destHeight = size . Height ;
Bitmap b = new Bitmap ( destWidth , destHeight ) ;
2014-07-10 11:45:57 +10:00
using ( var ia = new ImageAttributes ( ) )
{
ia . SetWrapMode ( WrapMode . TileFlipXY ) ;
2013-04-22 11:12:03 -12:00
2014-07-10 11:45:57 +10:00
using ( Graphics g = Graphics . FromImage ( b ) )
{
g . PixelOffsetMode = PixelOffsetMode . HighQuality ;
g . Clear ( Color . White ) ;
g . InterpolationMode = InterpolationMode . HighQualityBicubic ;
g . CompositingQuality = CompositingQuality . HighQuality ;
g . SmoothingMode = SmoothingMode . HighQuality ;
g . DrawImage ( imgToResize , new Rectangle ( 0 , 0 , destWidth , destHeight ) , 0 , 0 , imgToResize . Width ,
imgToResize . Height , GraphicsUnit . Pixel , ia ) ;
ia . Dispose ( ) ;
}
}
2009-06-20 13:49:35 +00:00
return b ;
}
}
}