Exemplo n.º 1
0
        public byte[] GenerateIconFile()
        {
            int[] sizes = this.bitmaps.Keys.OrderBy(i => i).ToArray();
            if (sizes.Length == 0)
            {
                throw new InvalidOperationException("Cannot generate empty icon file.");
            }
            List <byte> imageBytes        = new List <byte>();
            List <int>  startingPositions = new List <int>();
            int         startingPosition  = 6 + 16 * sizes.Length; // ico header is 6 bytes and each file header is 16 bytes.
            List <byte> icoHeader         = new List <byte>();
            List <byte> pngHeaders        = new List <byte>();
            List <byte> pngPayloads       = new List <byte>();

            ToLittleEndian(0, 2, icoHeader);            // first two bytes are always 0
            ToLittleEndian(1, 2, icoHeader);            // 1 for ICO format (2 is CUR)
            ToLittleEndian(sizes.Length, 2, icoHeader); // number of files

            foreach (int size in sizes)
            {
                SystemBitmap          originalImage = this.bitmaps[size];
                int                   width         = originalImage.Width;
                int                   height        = originalImage.Height;
                int                   x             = (size - width) / 2;
                int                   y             = (size - height) / 2;
                SystemBitmap          resource      = new SystemBitmap(size, size);
                SystemBitmap.Graphics g             = resource.MakeGraphics();
                g.Blit(originalImage, x, y);
                g.Cleanup();

                byte[] pngBytes = resource.SaveBytes(ImageFormat.PNG);
                pngPayloads.AddRange(pngBytes);

                ToLittleEndian(size == 256 ? 0 : size, 1, pngHeaders);
                ToLittleEndian(size == 256 ? 0 : size, 1, pngHeaders);
                ToLittleEndian(0, 1, pngHeaders);                // 0 for not using a color palette
                ToLittleEndian(0, 1, pngHeaders);                // reserved, always 0
                ToLittleEndian(0, 2, pngHeaders);                // 0 color planes.
                ToLittleEndian(32, 2, pngHeaders);               // 32 bits per pixel
                ToLittleEndian(pngBytes.Length, 4, pngHeaders);  // file size in bytes
                ToLittleEndian(startingPosition, 4, pngHeaders); // byte position from the beginning of the file

                startingPosition += pngBytes.Length;
            }

            List <byte> finalOutput = icoHeader;

            finalOutput.AddRange(pngHeaders);
            finalOutput.AddRange(pngPayloads);

            return(finalOutput.ToArray());
        }
Exemplo n.º 2
0
 public IconSetGenerator AddInputImage(SystemBitmap bmp)
 {
     if (bmp.Width != bmp.Height)
     {
         int          size   = Math.Max(bmp.Width, bmp.Height);
         SystemBitmap newBmp = new SystemBitmap(size, size);
         this.ownedBitmapReferences.Add(newBmp);
         SystemBitmap.Graphics g = newBmp.MakeGraphics();
         int x = (size - bmp.Width) / 2;
         int y = (size - bmp.Height) / 2;
         g.Blit(bmp, x, y);
         g.Cleanup();
         bmp = newBmp;
     }
     this.bitmaps.Add(bmp);
     this.bitmapMaxDimenion.Add(Math.Max(bmp.Width, bmp.Height));
     return(this);
 }
Exemplo n.º 3
0
        public Dictionary <int, SystemBitmap> Generate()
        {
            Dictionary <int, SystemBitmap> lookup = new Dictionary <int, SystemBitmap>();

            SystemBitmap[] sources = this.bitmaps.OrderBy(b => - b.Width).ToArray();

            foreach (int desiredSize in this.outputSizes)
            {
                SystemBitmap          source = this.FindBestMatch(sources, desiredSize);
                SystemBitmap          bmp    = new SystemBitmap(desiredSize, desiredSize);
                SystemBitmap.Graphics g      = bmp.MakeGraphics();
                g.Blit(source, 0, 0, desiredSize, desiredSize);
                g.Cleanup();
                lookup.Add(desiredSize, bmp);
            }

            this.Cleanup();

            return(lookup);
        }