Utilities/IconFactory.cs
changeset 384 76f859f4aea1
parent 344 3145aadca3d2
     1.1 --- a/Utilities/IconFactory.cs	Sun Oct 21 14:45:24 2012 +0000
     1.2 +++ b/Utilities/IconFactory.cs	Sat Oct 27 11:40:28 2012 +0000
     1.3 @@ -4,7 +4,7 @@
     1.4    License, v. 2.0. If a copy of the MPL was not distributed with this
     1.5    file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.6   
     1.7 -  Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
     1.8 +  Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
     1.9  	
    1.10  */
    1.11  
    1.12 @@ -63,16 +63,13 @@
    1.13      private struct ICONIMAGE {
    1.14        public BITMAPINFOHEADER Header;
    1.15        public byte[] Colors;
    1.16 -      public byte[] XOR;
    1.17 -      public byte[] AND;
    1.18 +      public int MaskSize;
    1.19  
    1.20        public ICONIMAGE(int width, int height, byte[] colors) {
    1.21          this.Header = new BITMAPINFOHEADER(width, height << 1, 
    1.22            (8 * colors.Length) / (width * height));
    1.23          this.Colors = colors;
    1.24 -        int maskSize = (width * height) >> 3;
    1.25 -        this.XOR = new byte[maskSize];
    1.26 -        this.AND = new byte[maskSize];
    1.27 +        MaskSize = (width * height) >> 3;
    1.28        }
    1.29  
    1.30        public void Write(BinaryWriter bw) {
    1.31 @@ -80,8 +77,8 @@
    1.32          int stride = Header.Width << 2;
    1.33          for (int i = (Header.Height >> 1) - 1; i >= 0; i--)
    1.34            bw.Write(Colors, i * stride, stride);
    1.35 -        bw.Write(XOR);        
    1.36 -        bw.Write(AND);
    1.37 +        for (int i = 0; i < 2 * MaskSize; i++)
    1.38 +          bw.Write((byte)0);        
    1.39        }
    1.40      }
    1.41  
    1.42 @@ -103,7 +100,7 @@
    1.43          this.Planes = image.Header.Planes;
    1.44          this.BitCount = image.Header.BitCount;
    1.45          this.BytesInRes = (uint)(image.Header.Size +
    1.46 -          image.Colors.Length + image.XOR.Length + image.AND.Length);
    1.47 +          image.Colors.Length + image.MaskSize + image.MaskSize);
    1.48          this.ImageOffset = (uint)imageOffset;
    1.49        }
    1.50  
    1.51 @@ -149,6 +146,9 @@
    1.52            (Entries.Length > 0 ? Entries[0].Size : 0)); } 
    1.53        }
    1.54      }
    1.55 +
    1.56 +    private static BinaryWriter binaryWriter = 
    1.57 +      new BinaryWriter(new MemoryStream());
    1.58  	
    1.59      public static Icon Create(byte[] colors, int width, int height, 
    1.60        PixelFormat format) {
    1.61 @@ -161,13 +161,12 @@
    1.62        dir.Entries[0].ImageOffset = dir.Size;
    1.63  
    1.64        Icon icon;
    1.65 -      using (BinaryWriter bw = new BinaryWriter(new MemoryStream())) {
    1.66 -				dir.Write(bw);
    1.67 -        image.Write(bw);
    1.68 +      binaryWriter.BaseStream.Position = 0;
    1.69 +			dir.Write(binaryWriter);
    1.70 +      image.Write(binaryWriter);
    1.71  
    1.72 -				bw.BaseStream.Position = 0;
    1.73 -        icon = new Icon(bw.BaseStream);
    1.74 -			}
    1.75 +			binaryWriter.BaseStream.Position = 0;
    1.76 +      icon = new Icon(binaryWriter.BaseStream);
    1.77  
    1.78        return icon;
    1.79      }