zoukankan      html  css  js  c++  java
  • Image 转换成 Icon

    /// <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>
            private Icon MakeIcon(Image img, int size, bool keepAspectRatio)
            {
                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());
            }

    转:http://www.cnblogs.com/boneking/archive/2010/05/06/1728643.html

  • 相关阅读:
    C#中char[]与string之间的转换
    Java文件操作之文件追加 Chars
    冒泡排序法的改进 Chars
    选择排序 Chars
    编译原理 Chars
    Apk反编译 Chars
    VC中常用的方法 Chars
    Node.Js入门级《培训》
    新概念系列之《Part2 Lesson 24 It could be worse》
    《CLR via C#》Part2之Chapter5 基元类型、引用类型和值类型(三)
  • 原文地址:https://www.cnblogs.com/qq1223558/p/3651571.html
Copyright © 2011-2022 走看看