The code sample is a function that takes an image as input and returns an Icon object. The function also takes a boolean parameter that specifies whether to keep the aspect ratio or not.
Icon MakeIcon(Image img, int size, bool keepAspRatio) { Bitmap square = new Bitmap(size, size); Graphics g = Graphics.FromImage(square); int x, y, w, h; if(!keepAspRatio|| img.Height == img.Width) { x = y = 0; w = h = size; } else { float r = (float)img.Width / (float)img.Height; if(r > 1) { w = size; h = (int)((float)size / r); x = 0; y = (size - h) / 2; } else { w = (int)((float)size * r); h = size; y = 0; x = (size - w) / 2; } } g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(img, x, y, w, h); g.Flush(); return Icon.FromHandle(square.GetHicon()); }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|