51
Finished Programs / Re: IDEA: Photo Downloader Kiosk
« on: October 27, 2008, 09:47 PM »
I haven't played around with the AHK code enough to give a good image scaling function in that will preserve the aspect ratio in AHK, but below is what I wrote to scale images in Circle Dock that preserves the aspect ratio while maximizing the image size given a desired size. I think it should be pretty easy for you guys to transfer this C# code to AHK form.
Code: C# [Select]
- /// <summary>
- /// Returns a bitmap resized to fit inside NewWidth and NewHeight without aspect ratio change. Uses high quality scaling.
- /// Returns null if an error is encountered.
- /// </summary>
- /// <param name="Bitmap">Image to resize.</param>
- /// <param name="NewWidth">Width of the resized bitmap.</param>
- /// <param name="NewHeight">Height of the resized bitmap.</param>
- public static Bitmap ScaleBySizeBestFit(ref Bitmap Bitmap, int NewWidth, int NewHeight)
- {
- try
- {
- // Scale the bitmap in high quality mode.
- using (Graphics gr = Graphics.FromImage(scaledBitmap))
- {
- gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
- gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
- gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
- gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
- double scaledBitmapAspectRatio = (double)scaledBitmap.Width / (double)scaledBitmap.Height;
- double sourceBitmapAspectRatio = (double)Bitmap.Width / (double)Bitmap.Height;
- int bestLeft, bestRight, bestTop, bestBottom, calcWidth, calcHeight;
- if (sourceBitmapAspectRatio > scaledBitmapAspectRatio) // The source bitmap is wider
- {
- bestLeft = 0;
- bestRight = scaledBitmap.Width;
- calcHeight = (int)((double)scaledBitmap.Width / sourceBitmapAspectRatio);
- bestTop = (scaledBitmap.Height - calcHeight) / 2;
- bestBottom = calcHeight;
- }
- else
- {
- bestTop = 0;
- bestBottom = scaledBitmap.Height;
- calcWidth = (int)((double)scaledBitmap.Height * sourceBitmapAspectRatio);
- bestLeft = (scaledBitmap.Width - calcWidth) / 2;
- bestRight = calcWidth;
- }
- }
- return scaledBitmap;
- }
- catch (Exception)
- {
- return null;
- }
- }
- }