Thursday, July 17, 2008

Binding images using Embeded Resource in ASP.Net, C#

Follow the simple 5 steps to make use of Emebeded Resource in .Net

Step 1: Open a new project, select windows application and click ok.

Step 2: Right click on the Solution, click on add existing item, and add all the images you want to bind.
Now, go to the each image property and select Embedded Resource as Build Action.

Step 3: Add the following code in Form behind class.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using System.IO;
using System.Diagnostics;

namespace EmbededResource
{
public partial class Form1 : Form
{

ArrayList pics;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.PictureBox pBox;
private System.Windows.Forms.Button btnDisplay;
public Form1()
{
InitializeComponent();
pics = new ArrayList();

}

private void Form1_Load(object sender, EventArgs e)
{
Stream imgStream = null;
Bitmap bmp = null;

// get a reference to the current assembly
Assembly a = Assembly.GetExecutingAssembly();

string[] resNames = a.GetManifestResourceNames();

foreach (string s in resNames)
{
//txtInfo.Text += s + "\r\n";
if (s.EndsWith("jpg"))
{
// attach to stream to the resource in the manifest
imgStream = a.GetManifestResourceStream(s);
if (!(null == imgStream))
{
// create a new bitmap from this stream and
// add it to the arraylist
bmp = Bitmap.FromStream(imgStream) as Bitmap;

if (!(null == bmp))
{
pics.Add(bmp);
}
bmp = null;
imgStream.Close();
imgStream = null;
}
}
}

}

private void pBox_Click(object sender, EventArgs e)
{
Random generator = new Random();
Bitmap bmp = pics[generator.Next(pics.Count)] as Bitmap;
if (!(null == bmp))
{
pBox.Image = bmp;

}
bmp = null;
generator = null;
}

private void pBox_DoubleClick(object sender, EventArgs e)
{
this.Close();
}

}
}

Step 4: Go to the defination of InitializeComponent() and add the foolowing code

private void InitializeComponent()
{
this.pBox = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pBox)).BeginInit();
this.SuspendLayout();

this.pBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pBox.Location = new System.Drawing.Point(8, 8);
this.pBox.Name = "pBox";
this.pBox.Size = new System.Drawing.Size(264, 272);
this.pBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pBox.TabIndex = 0;
this.pBox.TabStop = false;
this.pBox.DoubleClick += new System.EventHandler(this.pBox_DoubleClick);
this.pBox.Click += new System.EventHandler(this.pBox_Click);

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(279, 293);
this.Controls.Add(this.pBox);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.Click += new System.EventHandler(this.Form1_Click);
((System.ComponentModel.ISupportInitialize)(this.pBox)).EndInit();
this.ResumeLayout(false);

}

Step 5:
Build your code and thus you can run your appilcation.

No comments: