Utilities/EmbeddedResources.cs
author moel.mich
Sat, 11 Aug 2012 21:48:54 +0000
changeset 379 7af3aaeb42e9
parent 127 76aaf45a01c7
permissions -rw-r--r--
Added a few checks and delays to the driver loading code to increase the chance of loading the driver.
moel@1
     1
/*
moel@1
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@1
     6
 
moel@344
     7
  Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@1
     9
*/
moel@1
    10
moel@1
    11
using System;
moel@1
    12
using System.Collections.Generic;
moel@1
    13
using System.Drawing;
moel@127
    14
using System.IO;
moel@1
    15
using System.Reflection;
moel@1
    16
moel@1
    17
namespace OpenHardwareMonitor.Utilities {
moel@1
    18
  public class EmbeddedResources {
moel@1
    19
moel@1
    20
    public static Image GetImage(string name) {
moel@1
    21
      name = "OpenHardwareMonitor.Resources." + name;
moel@1
    22
moel@1
    23
      string[] names = 
moel@1
    24
        Assembly.GetExecutingAssembly().GetManifestResourceNames();
moel@1
    25
      for (int i = 0; i < names.Length; i++) {
moel@127
    26
        if (names[i].Replace('\\', '.') == name) {
moel@127
    27
          using (Stream stream = Assembly.GetExecutingAssembly().
moel@127
    28
            GetManifestResourceStream(names[i])) {
moel@127
    29
moel@127
    30
            // "You must keep the stream open for the lifetime of the Image."
moel@127
    31
            Image image = Image.FromStream(stream);
moel@127
    32
moel@127
    33
            // so we just create a copy of the image 
moel@127
    34
            Bitmap bitmap = new Bitmap(image);
moel@127
    35
moel@127
    36
            // and dispose it right here
moel@127
    37
            image.Dispose();
moel@127
    38
moel@127
    39
            return bitmap;
moel@127
    40
          }
moel@127
    41
        }
moel@127
    42
      } 
moel@1
    43
moel@1
    44
      return new Bitmap(1, 1);    
moel@1
    45
    }
moel@28
    46
moel@28
    47
    public static Icon GetIcon(string name) {
moel@28
    48
      name = "OpenHardwareMonitor.Resources." + name;
moel@28
    49
moel@28
    50
      string[] names =
moel@28
    51
        Assembly.GetExecutingAssembly().GetManifestResourceNames();
moel@28
    52
      for (int i = 0; i < names.Length; i++) {
moel@127
    53
        if (names[i].Replace('\\', '.') == name) {
moel@127
    54
          using (Stream stream = Assembly.GetExecutingAssembly().
moel@127
    55
            GetManifestResourceStream(names[i])) {
moel@127
    56
            return new Icon(stream);
moel@127
    57
          }
moel@127
    58
        }          
moel@127
    59
      } 
moel@28
    60
moel@28
    61
      return null;
moel@28
    62
    }
moel@1
    63
         
moel@1
    64
  }
moel@1
    65
}