/[AnywhereTS-MSSQL]/trunk/TSAdminTool/BootPicture.cs
ViewVC logotype

Contents of /trunk/TSAdminTool/BootPicture.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4 - (show annotations) (download)
Wed Jul 11 14:19:13 2012 UTC (8 years, 1 month ago) by william
File size: 12262 byte(s)
imported from https://anywherets.svn.sourceforge.net/svnroot/anywherets/trunk/
Revision: 1
Author: beartown
Date: 3:10:40 AM, Monday, June 21, 2010
Message:
First upload. Still todo in order to get a fully working product: Replace Thinstation images with vanilla Thinstation, modify ImageDesigntimeConfig.cs, ImageRuntimeConfig.cs in order to support the Thinstation image
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 using System.Drawing;
6 using System.Drawing.Imaging;
7 using System.Drawing.Drawing2D;
8 using System.CodeDom.Compiler;
9
10 namespace AnywhereTS
11 {
12 // Represents a boot picture
13 // Is used in ImageDesigntimeConfig.
14 public class ATSBootPicture
15 {
16 public int Type = 0; // 0 = text (no boot picture), 1 = Custom picture, 2-10 = reserved 11 = predef picture 1, 12 = predef picture 2, etc
17 public int Width = 1024; // Width of boot picture in pixels
18 public int Height = 768; // Height of boot picture in pixels
19 public int ProgressBar = 0; // 0=No progress bar. 1=Display progress bar. Currently not implemented
20 public Bitmap CustomPicture; // The custom boot picture.
21 public byte[] CustomPictureJpeg; // The custom boot picture in jpeg format.
22
23 // Constructor
24 public ATSBootPicture()
25 {
26 // Create a default boot picture
27 CustomPicture = new Bitmap(1024, 768);
28 // Print text on deafult boot picture
29 Graphics g = Graphics.FromImage(CustomPicture);
30 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
31 Font fontBig = new Font("Arial", Width / 15);
32 StringFormat strFormat = new StringFormat();
33 strFormat.Alignment = StringAlignment.Center;
34 strFormat.LineAlignment = StringAlignment.Center;
35 SolidBrush solidBrush = new SolidBrush(Color.Red);
36 g.Clear(Color.DarkCyan);
37 g.DrawString("AnywhereTS", fontBig, new SolidBrush(Color.Black), new RectangleF(2, 2, Width + 1, Height + 1), strFormat);
38 g.DrawString("AnywhereTS", fontBig, solidBrush, new RectangleF(0, 0, Width-1, Height-1), strFormat);
39 g.Dispose();
40 }
41
42
43 // Write boot picture into client
44 public void Write(string strInitrd, string strDataDir, bool nagText)
45 {
46 byte[] arrBuffer = new byte[2048];
47 string strBootPictureFile = ""; // File name and path for boot picture data file.
48 int intRead;
49
50 TempFileCollection tempDirectory = new TempFileCollection();
51
52 if (Type > 10)
53 { // Predefined boot image
54
55 // Calculate file name for built in boot picture
56 //Image filename is {1,2,3,4} for which image + {1,2,3} for resolution
57 strBootPictureFile = (Type - 10).ToString();
58 if (Width == 640 && Height == 480)
59 {
60 strBootPictureFile += "1";
61 }
62 else if (Width == 800 && Height == 600)
63 {
64 strBootPictureFile += "2";
65 }
66 else
67 { // 1024 x 768 or other resolutions
68 strBootPictureFile += "3";
69 }
70
71 strBootPictureFile = strDataDir+ @"\" + strBootPictureFile + ".dat";
72 }
73 else if (Type == 1)
74 { // Custom boot picture
75 Bitmap pictureToWrite = new Bitmap(CustomPicture);
76 if (nagText)
77 {
78 // Print text on picture
79 WriteNagTextOnImage(pictureToWrite);
80 }
81
82 // Save to memory stream
83 MemoryStream ms = new MemoryStream();
84 saveJpegToStream(ms, pictureToWrite, 90);
85 strBootPictureFile = tempDirectory.BasePath + "temp.dat";
86 WriteCustomBootPictureToFileFromStream(ms, strBootPictureFile, Width, Height);
87
88 } // if custom boot picture
89
90 if (Type == 1 || Type > 10)
91 { // Append the image file to the inird file
92 FileStream objSrc, objDest;
93
94 objSrc = File.Open(strBootPictureFile, FileMode.Open, FileAccess.Read);
95 objDest = File.Open(strInitrd, FileMode.Append, FileAccess.Write);
96 do
97 {
98 intRead = objSrc.Read(arrBuffer, 0, 2048);
99 objDest.Write(arrBuffer, 0, intRead);
100 } while (intRead != 0);
101 objSrc.Close();
102 objDest.Close();
103 }
104 tempDirectory.Delete();
105
106 } // WriteBootPicture
107
108
109 // ROUTINES FOR CUSTOM BOOT PICTURES
110
111 public void saveJpegToStream(MemoryStream mstream, Bitmap img, long quality)
112 {
113 // Encoder parameter for image quality
114 EncoderParameter qualityParam =
115 new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
116
117 // Jpeg image codec
118 ImageCodecInfo jpegCodec =
119 this.getEncoderInfo("image/jpeg");
120
121 if (jpegCodec == null)
122 return;
123
124 EncoderParameters encoderParams = new EncoderParameters(1);
125 encoderParams.Param[0] = qualityParam;
126
127 img.Save(mstream, jpegCodec, encoderParams);
128 }
129
130 // Resizes an image to the size set in Width and Height
131 public void ImageResize()
132 {
133 Bitmap b = new Bitmap(Width, Height);
134 Graphics g = Graphics.FromImage((Image)b);
135 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
136
137 g.DrawImage(CustomPicture, 0, 0, Width, Height);
138 g.Dispose();
139 CustomPicture = b;
140 }
141
142 private ImageCodecInfo getEncoderInfo(string mimeType)
143 {
144 // Get image codecs for all image formats
145 ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
146
147 // Find the correct image codec
148 for (int i = 0; i < codecs.Length; i++)
149 if (codecs[i].MimeType == mimeType)
150 return codecs[i];
151 return null;
152 }
153
154
155 public void WriteCustomBootPictureToFileFromStream(
156 MemoryStream msIn, // Input memory stream, a picture in jpeg format
157 string fileOut, // Path of the Boot pic file to be written
158 int bootPicWidth, // Boot pic width in pixels
159 int bootPicHeight) // Boot pic height in pixels
160 {
161 // Determine size of input picture
162
163 long bootPicSize = msIn.Length;
164 long totalSize = bootPicSize * 2;
165
166 // Create a file header in memory
167 byte[] fileHeader; // Buffer for file header
168 int headerLenght = 38;
169 fileHeader = new byte[headerLenght];
170 fileHeader[0] = (byte)'B';
171 fileHeader[1] = (byte)'O';
172 fileHeader[2] = (byte)'O';
173 fileHeader[3] = (byte)'T';
174 fileHeader[4] = (byte)'S';
175 fileHeader[5] = (byte)'P';
176 fileHeader[6] = (byte)'L';
177 fileHeader[7] = (byte)'3';
178 fileHeader[8] = 0; // unit
179 fileHeader[9] = 1; // state
180 fileHeader[10] = 7; //fgcolor
181 fileHeader[11] = 0; // bgcolor
182 fileHeader[12] = (byte)totalSize; //size 4 bytes
183 fileHeader[13] = (byte)(totalSize >> 8);
184 fileHeader[14] = (byte)(totalSize >> 16);
185 fileHeader[15] = (byte)(totalSize >> 24);
186 fileHeader[16] = 0; //tx two bytes
187 fileHeader[17] = 0; //tx
188 fileHeader[18] = 0; //ty two bytes
189 fileHeader[19] = 0; //ty
190 fileHeader[20] = (byte)bootPicWidth; //tw two bytes
191 fileHeader[21] = (byte)(bootPicWidth >> 8);
192 fileHeader[22] = (byte)bootPicHeight; //th two bytes
193 fileHeader[23] = (byte)(bootPicHeight >> 8);
194 fileHeader[24] = 0; // boxcount two bytes
195 fileHeader[25] = 0;
196 fileHeader[26] = 0; // Not used in this version (3) of the file format
197 fileHeader[27] = 0; // Not used in this version (3) of the file format
198 fileHeader[28] = (byte)bootPicSize; // silentl size 4 bytes
199 fileHeader[29] = (byte)(bootPicSize >> 8);
200 fileHeader[30] = (byte)(bootPicSize >> 16);
201 fileHeader[31] = (byte)(bootPicSize >> 24);
202 fileHeader[32] = 0; // sboxcount two bytes
203 fileHeader[33] = 0;
204 fileHeader[34] = 0; // Percent two bytes
205 fileHeader[35] = 0;
206 fileHeader[36] = 1; // overpaintok
207 fileHeader[37] = 0; // numpalette
208
209 // Prepare the streams
210 msIn.Position = 0; // Start reading from beginning of memory stream.
211 FileStream fsOut = new FileStream(fileOut,
212 FileMode.OpenOrCreate, FileAccess.Write);
213
214 // Write the file header to file.
215 fsOut.Write(fileHeader, 0, headerLenght);
216
217 // Write normal boot picture
218 // Continue with adding the jpeg to the file in chunks.
219 int bufferLen = 4096;
220 byte[] buffer = new byte[bufferLen];
221 int bytesRead;
222
223 do
224 {
225 // read a chunk of data from the input stream
226 bytesRead = msIn.Read(buffer, 0, bufferLen);
227
228 // write it
229 fsOut.Write(buffer, 0, bytesRead);
230 } while (bytesRead != 0);
231
232 // Write silent boot picture
233 msIn.Position = 0;
234 do
235 {
236 // read a chunk of data from the input file
237 bytesRead = msIn.Read(buffer, 0, bufferLen);
238
239 // write it
240 fsOut.Write(buffer, 0, bytesRead);
241 } while (bytesRead != 0);
242
243 // close everything
244 fsOut.Close();
245 }
246
247 public void WriteNagTextOnImage(Bitmap pic)
248 {
249 // Print text on boot picture
250 Graphics g = Graphics.FromImage(pic);
251 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
252 Font fontBig = new Font("Arial", Width/15);
253 Font fontSmall = new Font("Arial", 30);
254 StringFormat strFormat = new StringFormat();
255 strFormat.Alignment = StringAlignment.Center;
256 strFormat.LineAlignment = StringAlignment.Center;
257
258 SolidBrush solidBrush = new SolidBrush(Color.Red);
259
260 g.DrawString("AnywhereTS\nThin Client Solutions", fontBig, new SolidBrush(Color.Black), new RectangleF(2, 2, Width + 2 , Height + 2), strFormat);
261 g.DrawString("AnywhereTS\nThin Client Solutions", fontBig, solidBrush, new RectangleF(0, 0, Width, Height), strFormat);
262
263 g.DrawString("This text is not displayed in Pro version", fontSmall, new SolidBrush(Color.Black), new RectangleF(0.5f, (int)(Height / 2) + 0.5f, Width + 0.5f, (int)(Height / 2) + 0.5f), strFormat);
264 g.DrawString("This text is not displayed in Pro version", fontSmall, solidBrush, new RectangleF(0, (int)(Height/2), Width, (int)(Height/2)), strFormat);
265 g.Dispose();
266 }
267
268 public Image resizeImage(Image imgToResize, Size size)
269 {
270 int sourceWidth = imgToResize.Width;
271 int sourceHeight = imgToResize.Height;
272
273 float nPercent = 0;
274 float nPercentW = 0;
275 float nPercentH = 0;
276
277 nPercentW = ((float)size.Width / (float)sourceWidth);
278 nPercentH = ((float)size.Height / (float)sourceHeight);
279
280 if (nPercentH < nPercentW)
281 nPercent = nPercentH;
282 else
283 nPercent = nPercentW;
284
285 int destWidth = (int)(sourceWidth * nPercent);
286 int destHeight = (int)(sourceHeight * nPercent);
287
288 Bitmap b = new Bitmap(destWidth, destHeight);
289 Graphics g = Graphics.FromImage((Image)b);
290 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
291
292 g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
293 g.Dispose();
294
295 return (Image)b;
296 }
297
298
299 } // Class ATSBootPicture
300 } // Namespace

  ViewVC Help
Powered by ViewVC 1.1.22