1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2002-2005 Novell, Inc.
23 // Jackson Harper (jackson@ximian.com)
25 // Based on work done by:
26 // Dennis Hayes (dennish@Raytek.com)
27 // Aleksey Ryabchuk (ryabchuk@yahoo.com)
31 using System.Collections;
32 using System.Drawing.Imaging;
33 using System.Runtime.Serialization;
34 using System.Runtime.InteropServices;
37 namespace System.Windows.Forms {
40 public sealed class ImageListStreamer : ISerializable {
42 private static byte [] signature = new byte [] {77 , 83 , 70 , 116};
44 private Image [] images;
45 private Size image_size;
46 private Color back_color;
48 private ImageListStreamer (SerializationInfo info, StreamingContext context) {
52 byte [] data = (byte [])info.GetValue ("Data", typeof (byte []));
53 if (data == null || data.Length <= signature.Length)
55 // check the signature ( 'MSFt' )
56 if (data [0] != signature [0] || data [1] != signature [1] ||
57 data [2] != signature [2] || data [3] != signature [3])
60 // calulate size of array needed for decomressed data
62 int real_byte_count = 0;
63 for (i = signature.Length; i < data.Length; i += 2)
64 real_byte_count += data [i];
66 if (real_byte_count == 0)
70 byte [] decompressed = new byte [real_byte_count];
72 for (i = signature.Length; i < data.Length; i += 2) {
73 for (int k = 0; k < data [i]; k++)
74 decompressed [j++] = data [i + 1];
77 MemoryStream stream = new MemoryStream (decompressed);
78 BinaryReader reader = new BinaryReader (stream);
80 IntPtr hbmMask = IntPtr.Zero;
81 IntPtr hbmColor= IntPtr.Zero;
84 // read image list header
85 ushort usMagic = reader.ReadUInt16 ();
86 ushort usVersion = reader.ReadUInt16 ();
87 ushort cCurImage = reader.ReadUInt16 ();
88 ushort cMaxImage = reader.ReadUInt16 ();
89 ushort cGrow = reader.ReadUInt16 ();
90 ushort cx = reader.ReadUInt16 ();
91 ushort cy = reader.ReadUInt16 ();
92 uint bkcolor = reader.ReadUInt32 ();
93 ushort flags = reader.ReadUInt16 ();
95 short [] ovls = new short [4];
96 for (i = 0; i < ovls.Length; i++)
97 ovls[i] = reader.ReadInt16 ();
99 image_size = new Size (cx, cy);
100 back_color = Color.FromArgb ((int) bkcolor);
102 MemoryStream start = new MemoryStream (decompressed,
103 (int) stream.Position,
104 (int) stream.Length - (int) stream.Position,
107 Image image = Image.FromStream (start);
109 // Holy calamity. This is what happens on MS
110 // if the background colour is 0xFFFFFFFF (CLR_NONE)
111 // the mask is set to the color at pixel 0, 0
112 Bitmap bmp = image as Bitmap;
113 if (bkcolor == 0xFFFFFFFF && bmp != null)
114 back_color = bmp.GetPixel (0, 0);
116 int step = image.Width / cx;
117 images = new Image [cCurImage];
119 Rectangle dest_rect = new Rectangle (0, 0, cx, cy);
120 for (int r = 0 ; r < cCurImage ; r++) {
121 Rectangle area = new Rectangle (
125 Bitmap b = new Bitmap (cx, cy);
126 using (Graphics g = Graphics.FromImage (b)) {
127 g.DrawImage (image, dest_rect, area,
130 b.MakeTransparent (back_color);
134 } catch (Exception e) {
140 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
145 internal Image [] Images {
146 get { return images; }
149 internal Size ImageSize {
150 get { return image_size; }
153 internal ColorDepth ImageColorDepth {
154 get { return ColorDepth.Depth32Bit; }
157 internal Color BackColor {
158 get { return back_color; }