Using embedded resources
Written by: maffelu , 2009-04-16 18:00:02
using System;
namespace VisualVehicles
{
/// <summary>
/// Sums up the features of a vehicle, including the path to the picture.
/// </summary>
public class Vehicle
{
//Fields
private string brand;
private string bType;
private int conceptYear;
private string color;
private string imageAddress;
//Properties
public string Brand
{
get{return brand;}
set{brand = value;}
}
public string BType
{
get{return bType;}
set{bType = value;}
}
public int ConceptYear
{
get{return conceptYear;}
set{conceptYear = value;}
}
public string Color
{
get{return color;}
set{color = value;}
}
public string ImageAddress
{
get{return imageAddress;}
set{imageAddress = value;}
}
//Constructor
public Vehicle (string brand, string bType, string color, int conceptYear, string imageAddress)
{
Brand = brand;
BType = bType;
Color = color;
ConceptYear = conceptYear;
ImageAddress = imageAddress;
}
}
}
using System.Drawing;
using System.Reflection;
using System.IO;
private Assembly assembly;
private Stream stream;
assembly = Assembly.GetExecutingAssembly();
stream = assembly.GetManifestResourceStream("Namespace.filename.extension");
pictureBox1.Image = Image.FromStream(stream);
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
namespace VisualVehicles
{
public partial class MainForm : Form
{
//Fields
private List<Vehicle> carList;
private Assembly assembly;
private Stream stream;
public MainForm()
{
InitializeComponent();
}
void MainFormLoad(object sender, EventArgs e)
{
//Create the list we will be using
carList = new List<Vehicle>();
//Create some cars to work with
CreateCars();
//List the cars
foreach(Vehicle v in carList)
{
lsbIndex.Items.Add(v.Brand + " " + v.BType);
}
}
private void CreateCars()
{
//Add som cars according to the Vehicle class constructor
//The last parameter, imageAddress is the location of the
//embedded picture.
carList.Add(new Vehicle("Jaguar", "XF", "Silver", 2007, "VisualVehicles.images.jaguarXF.jpg"));
carList.Add(new Vehicle("Jaguar", "XKR Silverstone", "Silver", 2001, "VisualVehicles.images.2001JaguarXKRsilverstone.jpg"));
carList.Add(new Vehicle("Jaguar", "XJR-15", "Black", 1990, "VisualVehicles.images.1990JaguarXJR-15.jpg"));
carList.Add(new Vehicle("Saab", "9-3 Convertible \"Yellow Edition\"", "Yellow", 2008, "VisualVehicles.images.2008Saab9-3ConvertibleYellowEdition.jpg"));
carList.Add(new Vehicle("Saab", "9-5", "Blue", 1997, "VisualVehicles.images.Saab9-5.jpg"));
carList.Add(new Vehicle("Nissan", "X-Trail", "Red", 2001, "VisualVehicles.images.2001NissanX-trail.jpg"));
carList.Add(new Vehicle("Honda", "Alfa Romeo", "Blue", 2007, "VisualVehicles.images.2007HondaAlfaRomeo.jpg"));
carList.Add(new Vehicle("Honda", "Delta", "Silver", 2009, "VisualVehicles.images.2009HondaDelta.jpg"));
}
void LsbIndexSelectedIndexChanged(object sender, EventArgs e)
{
//Get the selected index from the listbox
int idx = lsbIndex.SelectedIndex;
//Get the assembly that contains the code that is currently executing
assembly = Assembly.GetExecutingAssembly();
//Load the resources from this assembly, find the one that matches our imageAddress!
stream = assembly.GetManifestResourceStream(carList[idx].ImageAddress.ToString());
//Load the image from the stream we got above
pbCar.Image = Image.FromStream(stream);
//Set the textboxes from the list
txtBrand.Text = carList[idx].Brand;
txtType.Text = carList[idx].BType;
txtColor.Text = carList[idx].Color;
txtYear.Text = carList[idx].ConceptYear.ToString();
//Set the form text to brand + type
this.Text = carList[idx].Brand + " " + carList[idx].BType;
}
}
}
There are no comments on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.