moel@1: /*
moel@1:  
moel@344:   This Source Code Form is subject to the terms of the Mozilla Public
moel@344:   License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344:   file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@1:  
moel@344:   Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344: 	
moel@1: */
moel@1: 
moel@1: using System;
moel@1: using System.Collections.Generic;
moel@1: using System.Drawing;
moel@127: using System.IO;
moel@1: using System.Reflection;
moel@1: 
moel@1: namespace OpenHardwareMonitor.Utilities {
moel@1:   public class EmbeddedResources {
moel@1: 
moel@1:     public static Image GetImage(string name) {
moel@1:       name = "OpenHardwareMonitor.Resources." + name;
moel@1: 
moel@1:       string[] names = 
moel@1:         Assembly.GetExecutingAssembly().GetManifestResourceNames();
moel@1:       for (int i = 0; i < names.Length; i++) {
moel@127:         if (names[i].Replace('\\', '.') == name) {
moel@127:           using (Stream stream = Assembly.GetExecutingAssembly().
moel@127:             GetManifestResourceStream(names[i])) {
moel@127: 
moel@127:             // "You must keep the stream open for the lifetime of the Image."
moel@127:             Image image = Image.FromStream(stream);
moel@127: 
moel@127:             // so we just create a copy of the image 
moel@127:             Bitmap bitmap = new Bitmap(image);
moel@127: 
moel@127:             // and dispose it right here
moel@127:             image.Dispose();
moel@127: 
moel@127:             return bitmap;
moel@127:           }
moel@127:         }
moel@127:       } 
moel@1: 
moel@1:       return new Bitmap(1, 1);    
moel@1:     }
moel@28: 
moel@28:     public static Icon GetIcon(string name) {
moel@28:       name = "OpenHardwareMonitor.Resources." + name;
moel@28: 
moel@28:       string[] names =
moel@28:         Assembly.GetExecutingAssembly().GetManifestResourceNames();
moel@28:       for (int i = 0; i < names.Length; i++) {
moel@127:         if (names[i].Replace('\\', '.') == name) {
moel@127:           using (Stream stream = Assembly.GetExecutingAssembly().
moel@127:             GetManifestResourceStream(names[i])) {
moel@127:             return new Icon(stream);
moel@127:           }
moel@127:         }          
moel@127:       } 
moel@28: 
moel@28:       return null;
moel@28:     }
moel@1:          
moel@1:   }
moel@1: }