PageRenderTime 15ms CodeModel.GetById 11ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Languages/Ruby/Samples/Tutorial/wpf/SplashScreen.cs

#
C# | 59 lines | 26 code | 4 blank | 29 comment | 1 complexity | e7d56e782694c407e6e9b295ddb95bad MD5 | raw file
 1/* ****************************************************************************
 2 *
 3 * Copyright (c) Microsoft Corporation. 
 4 *
 5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
 6 * copy of the license can be found in the License.html file at the root of this distribution. If 
 7 * you cannot locate the  Apache License, Version 2.0, please send an email to 
 8 * ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 9 * by the terms of the Apache License, Version 2.0.
10 *
11 * You must not remove this notice, or any other, from this software.
12 *
13 *
14 * ***************************************************************************/
15
16using System;
17using System.Diagnostics;
18using System.IO;
19using System.Resources;
20using System.Windows;
21
22/// <summary>
23/// This assembly is used to embed a splash screen image. System.Windows.SplashScreen only supports
24/// embedded images. We could avoid the need to use compiled C# code and instead generate a Ref.Emit
25/// on the fly to hold the embedded image resource, but it might affect start up time.
26/// 
27/// Run the following commands to create SplashScreen.dll:
28///   csc /t:library /r:WindowsBase.dll SplashScreen.cs
29///   rbx -e "require 'SplashScreen.dll'; SplashScreen::SplashScreen.write_image_resource 'SplashScreen.png', 'SplashScreen.g.resources'"
30///   csc /t:library /r:WindowsBase.dll /resource:SplashScreen.g.resources SplashScreen.cs
31/// </summary>
32namespace SplashScreen {
33    public class SplashScreen {
34        /// <summary>
35        /// Creates a resources file with an image
36        /// </summary>
37        /// <param name="imageSourcePath"></param>
38        /// <param name="outputPath"></param>
39        public static void WriteImageResource(string imageSourcePath, string outputPath) {
40            Debug.Assert(imageSourcePath.EndsWith(".png"));
41            Debug.Assert(outputPath.EndsWith(".resources"));
42
43            FileInfo fileInfo = new FileInfo(imageSourcePath);
44            FileStream fsSource = fileInfo.OpenRead();
45            byte[] bytes = new byte[fileInfo.Length + 1];
46            int bytesRead = fsSource.Read(bytes, 0, bytes.Length);
47            Debug.Assert(bytesRead == fileInfo.Length);
48            MemoryStream memoryStream = new MemoryStream(bytes, 0, bytesRead);
49            ResourceWriter resw = new ResourceWriter(outputPath);
50            resw.AddResource(imageSourcePath.ToLowerInvariant(), memoryStream);
51            resw.Close();
52        }
53
54        public static void Show() {
55            var s = new System.Windows.SplashScreen(typeof(SplashScreen).Assembly, "splashscreen.png");
56            s.Show(true);
57        }
58    }
59}