Laden...

Icons mit mehreren Auflösungen (inkl. 256x256 PNG Compressed Icons) per Code erstellen

Erstellt von KainPlan vor 14 Jahren Letzter Beitrag vor 14 Jahren 1.919 Views
K
KainPlan Themenstarter:in
133 Beiträge seit 2009
vor 14 Jahren
Icons mit mehreren Auflösungen (inkl. 256x256 PNG Compressed Icons) per Code erstellen

Huhu,

Ab Win Vista (oder schon eher??) unterstützt der Windows-Explorer ja dieses neue Iconformat (256x256 PNG Compressed Icons). Nun wollte ich wissen ob jemand guten Code kennt der solche Icons erstellt? Eine Doku für das Dateiformat damit ich selbst "handanlegen" kann wär auch okay. Irgendwie find ich nix. 😃

Danke schonmal.

K
KainPlan Themenstarter:in
133 Beiträge seit 2009
vor 14 Jahren

Okay war bissl blöd hab doch ne doku gefunden und schnell selbst was gebastelt für meine zwecke reichts:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Drawing;

namespace GamesVerwaltung
{
    class MultiIcon
    {
        private struct TIconHeader
        {
            public short res0;
            public short imgtype;
            public short imgcount;
        }
        private struct TIconData
        {
            public byte width;
            public byte height;
            public byte clcount;
            public byte res0;
            public short clplanes;
            public short bpp;
            public int imglen;
            public int imgadr;
        }

        private Bitmap[] bitmaps;
        private TIconData[] icondata;
        private TIconHeader iconheader;

        public MultiIcon()
        {
            bitmaps = new Bitmap[6];
            icondata = new TIconData[6];

            iconheader = new TIconHeader();
            iconheader.imgcount = 6;
            iconheader.imgtype = 1;
            iconheader.res0 = 0;
        }

        private void setImage(int idx, Bitmap bmp)
        {
            bitmaps[idx] = bmp;

            icondata[idx].bpp = 32;
            icondata[idx].clcount = 0;
            icondata[idx].clplanes = 0;
            icondata[idx].height = (byte)(bmp.Height == 256 ? 0 : bmp.Height);
            icondata[idx].width = (byte)(bmp.Width == 256 ? 0 : bmp.Width);
            icondata[idx].res0 = 0;
            icondata[idx].imgadr = -1;
            icondata[idx].imglen = -1;
        }

        public void SetAll(Image img)
        {
            setImage(0, createHighRes(img, 16, 16));
            setImage(1, createHighRes(img, 24, 24));
            setImage(2, createHighRes(img, 32, 32));
            setImage(3, createHighRes(img, 48, 48));
            setImage(4, createHighRes(img, 128, 128));
            setImage(5, createHighRes(img, 256, 256));
        }

        public void Save(string file)
        {
            FileStream fstr = new FileStream(file, FileMode.Create, FileAccess.Write);
            writeIcon(fstr);
            fstr.Close();
            fstr.Dispose();
        }

        private void writeIcon(Stream str)
        {
            BinaryWriter bw = new BinaryWriter(str);

            bw.Write(iconheader.res0);
            bw.Write(iconheader.imgtype);
            bw.Write(iconheader.imgcount);

            int offset = 6 /*ANZAHL DER BILDER*/ * 16 /*BYTEGRÖßE ICONHEADER*/;
            int iconheader0offset = (int)str.Position;
            str.Position += offset;

            for (int i = 0; i < 6; i++)
            {
                Bitmap bmp = bitmaps[i];

                //MemoryStream mstr = new MemoryStream();
                icondata[i].imgadr = (int)str.Position;
                bmp.Save(str, System.Drawing.Imaging.ImageFormat.Png);
                icondata[i].imglen = (int)(str.Position - icondata[i].imgadr);
            }

            str.Position = iconheader0offset;

            for (int i = 0; i < 6; i++)
            {
                TIconData idat = icondata[i];
                /*
                    0 	1 	Specifies image width in pixels. Can be 0, 255 or a number between 0 to 255. Should be 0 if image width is 256 pixels.
                    1 	1 	Specifies image height in pixels. Can be 0, 255 or a number between 0 to 255. Should be 0 if image height is 256 pixels.
                    2 	1 	Specifies number of colors in the color palette. Should be 0 if the image is truecolor.
                    3 	1 	Reserved. Should be 0.[Notes 1]
                    4 	2 	In .ICO format: Specifies color planes. Should be 0 or 1.[Notes 2]
                    6 	2 	In .ICO format: Specifies bits per pixel. [Notes 3]
                    8 	4 	Specifies the size of the bitmap data in bytes
                    12 	4 	Specifies the offset of bitmap data address in the file
                */
                bw.Write(idat.width);
                bw.Write(idat.height);
                bw.Write(idat.clcount);
                bw.Write(idat.res0);
                bw.Write(idat.clplanes);
                bw.Write(idat.bpp);
                bw.Write(idat.imglen);
                bw.Write(idat.imgadr);
            }
        }

        private Bitmap createHighRes(Image img, int w, int h)
        {
            Bitmap bmp = new Bitmap(w, h);

            Graphics dc = Graphics.FromImage(bmp);
            dc.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            dc.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            dc.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            dc.DrawImage(img, 0, 0, w, h);
            dc.Dispose();

            return bmp;
        }
    }
}