zoukankan      html  css  js  c++  java
  • 将图像转换成一个图标

    /// <summary>
            /// Converts an image into an icon.
            /// 将图像转换成一个图标
            /// </summary>
            /// <param name="img">The image that shall become an icon</param>
            /// <param name="size">The width and height of the icon. Standard
            /// sizes are 16x16, 32x32, 48x48, 64x64.</param>
            /// <param name="keepAspectRatio">Whether the image should be squashed into a
            /// square or whether whitespace should be put around it.</param>
            /// <returns>An icon!!</returns>
            public static Icon ConvertIcon(Image img, int size, bool keepAspectRatio)
            {
                if (img == null)
                    return null;
     
                Bitmap square = new Bitmap(size, size); // create new bitmap
                Graphics g = Graphics.FromImage(square); // allow drawing to it
                int x, y, w, h; // dimensions for new image
                if (!keepAspectRatio || img.Height == img.Width)
                {
                    // just fill the square
                    x = y = 0; // set x and y to 0
                    w = h = size; // set width and height to size
                }
                else
                {
                    // work out the aspect ratio
                    float r = (float)img.Width / (float)img.Height;
     
                    // set dimensions accordingly to fit inside size^2 square
                    if (r > 1)
                    { // w is bigger, so divide h by r
                        w = size;
                        h = (int)((float)size / r);
                        x = 0; y = (size - h) / 2; // center the image
                    }
                    else
                    { // h is bigger, so multiply w by r
                        w = (int)((float)size * r);
                        h = size;
                        y = 0; x = (size - w) / 2; // center the image
                    }
                }
     
                // make the image shrink nicely by using HighQualityBicubic mode
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(img, x, y, w, h); // draw image with specified dimensions
                g.Flush(); // make sure all drawing operations complete before we get the icon
                // following line would work directly on any image, but then
                // it wouldn't look as nice.
                return Icon.FromHandle(square.GetHicon());
            }
  • 相关阅读:
    田忌赛马(动态规划)
    关于程序中使用相对路径的小技巧
    ReadyGo新闻管理系统 使用号召帖
    解决:Visual Studio2005打开UTF8网页文件时出现乱码
    10个不为人知 但绝对值得收藏的网站
    幻数
    模板偏特化的威力
    c++ 拷贝
    win7下Visualbox 装Ubuntu10.10, 装eclipsecdt+svn插件全过程
    找完工作的心情
  • 原文地址:https://www.cnblogs.com/hdl217/p/2256437.html
Copyright © 2011-2022 走看看