Keep aspect ratio on product images

Code
//class EcoResProductImageThumbnail

public container generateThumbnail(int _height,int _width,DocuRef _docuRef)
{
    
container               imageThumbnail;
    
DocuRef                 docuRef;
    
DocuValue               docuValue;
    
Filename                completeFileName,saveThumbFileName,saveTmpFileName;
    
str                     fileExtension;
    
BinData                 binData = new BinData(), binDataOrig = new BinData();
    
System.IO.FileStream    imageStreamDecode;
    
System.Drawing.Bitmap   img;
    
System.Drawing.Image    thumbnail;
    
System.Drawing.Imaging.ImageFormat format;

    
System.IntPtr           intPtr = new System.IntPtr(0);

    
// MODIFICATION-->
    
System.Drawing.Graphics graphics;

    
real ratioX;
    
real ratioY;
    
real ratio;
    
int newHeight;
    
int newWidth;
    
int posX;
    
int posY;
    
int originalWidth;
    
int originalHeight;
    
// MODIFICATION <--

    
str tempPath;

    
#define.ThumbFile("thumb")
    #define.TmpFile("tmp.jpg")

    
tempPath System.IO.Path::GetTempPath();
    
saveThumbFileName tempPath #ThumbFile;
    
saveTmpFileName   tempPath #TmpFile;
    
try
    {
        
docuRef     DocuRef::findRecId(_docuRef.RecId);
        
docuValue   DocuRef::findValue(docuRef.ValueRecId).docuValue();
        if(
isUrlPath)
        {
            
completeFileName this.downloadFromUrlToFile(_docuRef.Notes);
        }
        else if(
docuValue.File == conNull())
        {
            
completeFileName docuRef.completeFilename();
        }
        else
        {
            
binDataOrig.setData(docuValue.File);
            
binDataOrig.saveFile(saveTmpFileName);
            
completeFileName saveTmpFileName;
        }
        if(!
isUrlPath || isFileDownloaded)
        {
            
imageStreamDecode = new System.IO.FileStream(completeFileNameSystem.IO.FileMode::OpenSystem.IO.FileAccess::ReadSystem.IO.FileShare::ReadWrite); // For file stream

            
img = new System.Drawing.Bitmap(imageStreamDecode);

            
format img.get_RawFormat();

            
fileExtension this.getImageFormat(format);
            
saveThumbFileName += fileExtension;

            
// MODIFICTAION -->
            //thumbnail = img.GetThumbnailImage(_width,_height, null,intPtr);
            
thumbnail = new System.Drawing.Bitmap(_width,_height);

            
graphics =  System.Drawing.Graphics::FromImage(thumbnail);
            
graphics.set_InterpolationMode(System.Drawing.Drawing2D.InterpolationMode::HighQualityBicubic);
            
graphics.set_SmoothingMode(System.Drawing.Drawing2D.SmoothingMode::HighQuality);
            
graphics.set_PixelOffsetMode(System.Drawing.Drawing2D.PixelOffsetMode::HighQuality);
            
graphics.set_CompositingQuality(System.Drawing.Drawing2D.CompositingQuality::HighQuality);
            
graphics.Clear(System.Drawing.Color::get_White());

            
originalWidth img.get_Width();
            
originalHeight img.get_Height();

            
// Figure out the ratio
            
ratioX _width  originalWidth;
            
ratioY _height originalHeight;
            
// use whichever multiplier is smaller
            
ratio ratioX ratioY ratioX ratioY;

            
// now we can get the new height and width
            
newWidth originalWidth ratio;
            
newHeight originalHeight ratio;

            
// Now calculate the X,Y position of the upper-left corner
            // (one of these will always be zero)
            
posX = (_width - (originalWidth ratio)) / 2;
            
posY = (_height - (originalHeight ratio)) / 2;

            
graphics.DrawImage(img,posXposYnewWidthnewHeight);
            
// MODIFICATION <--

            
thumbnail.Save(saveThumbFileNameformat);

            
binData.loadFile(saveThumbFileName);
            
imageThumbnail binData.getData();
            
imageStreamDecode.Close();
            
this.deleteThumbnail(saveThumbFileName);
            
this.deleteThumbnail(saveTmpFileName);
        }
    }
    catch
    {
        
imageThumbnail conNull();
        
imageStreamDecode.Close();
        
infolog.clear(0);
        
this.deleteThumbnail(saveThumbFileName);

        
this.deleteThumbnail(saveTmpFileName);

    }
    return 
imageThumbnail;
}
AX versionMicrosoft Dynamics AX 2012
PropertiesCreated by Alex 2015-01-14 14:00:02 (Public)
TagsInventTable