如何将图像转换为图标不失透明度? [英] How to convert an image to an icon without losing transparency?

查看:137
本文介绍了如何将图像转换为图标不失透明度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我需要显示它之前转换为图标的PNG图片

I have PNG images which I need to convert to an icon before displaying 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?

推荐答案

没有,还有更多的东西很多。图标有一个相当复杂的内部结构,优化上世纪80年代的硬件合理工作。图标图像具有的的位图,一个用于该图标,单色位图,指示哪些图像的部分是透明的,另一单色位图,指示部分被颠倒的。生成的单色位图是相当痛苦,.NET不支持他们。也不Bitmap.GetHicon()做它的尝试。你需要一个库来为你做的工作。

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的给了一些救济,它开始支持包含一个PNG图像图标。你在用自己的代码生成它有一个镜头。像这样的:

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);
    }



测试在.NET 4.5和Windows 8.1。谨防边缘的可能性,你的PNG图片看到的边缘透明度。仅工作良好时被显示在一个公知的背景颜色的图像。其中,由设计,图标不能依靠。一个专门的图标编辑器总是会得到很好看的图标唯一真正好方法。

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.

这篇关于如何将图像转换为图标不失透明度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆