Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

c# - How to convert an image to an icon without losing transparency?

I have PNG images which I need to convert to an icon before displaying it.

This is how I did it:

public Icon ImageToIcon(Image imgTest)
{
    Bitmap bitmap = new Bitmap(imgTest);
    Icon icoTest;

    IntPtr iPtr = bitmap.GetHicon();
    icoTest = (Icon) Icon.FromHandle(iPtr).Clone();

    return icoTest;
}

I lose transparency after doing this, alpha transparent images are not rendered as expected....can this be solved?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No, there's a lot more to it. Icons have a pretty elaborate internal structure, optimized to work reasonably on 1980s hardware. An icon image has three bitmaps, one for the icon, a monochrome bitmap that indicates what parts of the image are transparent and another monochrome bitmap that indicates what parts are reversed. Generating those monochrome bitmaps is pretty painful, .NET doesn't support them. Nor does Bitmap.GetHicon() make an attempt at it. You'll need a library to do the work for you.

Vista gave some relief, it started supporting icons that contain a PNG image. You'll have a shot at generating it with your own code. Like this:

    public static Icon IconFromImage(Image img) {
        var ms = new System.IO.MemoryStream();
        var bw = new System.IO.BinaryWriter(ms);
        // Header
        bw.Write((short)0);   // 0 : reserved
        bw.Write((short)1);   // 2 : 1=ico, 2=cur
        bw.Write((short)1);   // 4 : number of images
        // Image directory
        var w = img.Width;
        if (w >= 256) w = 0;
        bw.Write((byte)w);    // 0 : width of image
        var h = img.Height;
        if (h >= 256) h = 0;
        bw.Write((byte)h);    // 1 : height of image
        bw.Write((byte)0);    // 2 : number of colors in palette
        bw.Write((byte)0);    // 3 : reserved
        bw.Write((short)0);   // 4 : number of color planes
        bw.Write((short)0);   // 6 : bits per pixel
        var sizeHere = ms.Position;
        bw.Write((int)0);     // 8 : image size
        var start = (int)ms.Position + 4;
        bw.Write(start);      // 12: offset of image data
        // Image data
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        var imageSize = (int)ms.Position - start;
        ms.Seek(sizeHere, System.IO.SeekOrigin.Begin);
        bw.Write(imageSize);
        ms.Seek(0, System.IO.SeekOrigin.Begin);

        // And load it
        return new Icon(ms);
    }

Tested on .NET 4.5 and Windows 8.1. Beware of the possibility of "fringes" you'll see on PNG images with transparency on the edges. That only works well when the image is displayed on a well-known background color. Which, by design, an icon can never depend on. A dedicated icon editor will always be the only truly good way to get good looking icons.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...