Click here to Skip to main content
15,887,746 members
Home / Discussions / C#
   

C#

 
AnswerAhhhh! Pin
peterchen3-Sep-05 22:02
peterchen3-Sep-05 22:02 
GeneralRe: Ahhhh! Pin
Mathew Hall3-Sep-05 22:14
Mathew Hall3-Sep-05 22:14 
QuestionHow to add Binary Resources Pin
Heinz_3-Sep-05 20:33
Heinz_3-Sep-05 20:33 
AnswerRe: How to add Binary Resources Pin
turbochimp4-Sep-05 16:34
turbochimp4-Sep-05 16:34 
GeneralRe: How to add Binary Resources Pin
Heinz_5-Sep-05 9:51
Heinz_5-Sep-05 9:51 
GeneralRe: How to add Binary Resources Pin
turbochimp6-Sep-05 11:21
turbochimp6-Sep-05 11:21 
GeneralRe: How to add Binary Resources Pin
Heinz_7-Sep-05 19:55
Heinz_7-Sep-05 19:55 
GeneralRe: How to add Binary Resources Pin
turbochimp7-Sep-05 20:43
turbochimp7-Sep-05 20:43 
As I mentioned in my previous message, if you have a file you want to embed in a resource file, create a file stream using the target file, read the stream from start to end (will yield a byte array - type = byte[]), then use that byte array as the source of the resource entry. When you read the bytes back out of the resource, you can then use whatever kind of writer you want to "rehydrate" the bytes into a file, an image etc.

Here is an example of embedding a single .JPG image in a resource file - it's a trivial example, but it works and should get you started in the right direction:

public static void WriteResourceToFile(string importPath, string resourceKey, string resourceFileName)
{
	// Read the file into a byte array
	byte[] buffer = null;
	using (Stream file = new FileStream(importPath, FileMode.Open, FileAccess.Read))
	{
		buffer = new byte[file.Length];
		file.Read(buffer, 0, (int)file.Length);
                file.Close();
	}

	// Write the file as a byte array to a resource file.
	using (ResourceWriter writer = new ResourceWriter(resourceFileName))
	{
		writer.AddResource(resourceKey, buffer);
		writer.Generate();
		writer.Close();
	}
}


The preceding function could be called to store an image file using:
ResourceUtility.WriteResourceToFile(@"c:\pic1.jpg", "pic1", "image.resources");


To read the image back out of the resource file, you could implement something like:

private System.Drawing.Image ReadImageResourceFromFile(string resourceFileName, object resourceKey)
{
	MemoryStream stream = null;
	Image i = null;

        // Read the resource file and find the requested key.
	using (ResourceReader reader = new ResourceReader(resourceFileName))
	{
		IDictionaryEnumerator loop = reader.GetEnumerator();
		while (loop.MoveNext())
		{
			if (loop.Key == resourceKey)
			{
				byte[] buffer = (byte[])loop.Value;
				stream = new MemoryStream(buffer);
				break;
			}
		}
		reader.Close();
	}

        // Create an Image object from the newly read byte array.
	if (stream != null && stream.Length > 0)
	{
		stream.Position = 0;
		i = Image.FromStream(stream);
		stream.Close();
	}
	return i;
}


...and could be called using:

System.Drawing.Image result = ReadImageResourceFromFile("image.resources", "pic1");


Hope this helps.

The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’

GeneralRe: How to add Binary Resources Pin
Heinz_8-Sep-05 11:31
Heinz_8-Sep-05 11:31 
Questionplz help me Pin
Sayed Sajjad Raza Zaidi3-Sep-05 20:12
Sayed Sajjad Raza Zaidi3-Sep-05 20:12 
AnswerRe: plz help me Pin
Mohamad Al Husseiny4-Sep-05 10:14
Mohamad Al Husseiny4-Sep-05 10:14 
Questionparse string by using xml Pin
savage_3-Sep-05 13:50
savage_3-Sep-05 13:50 
AnswerRe: parse string by using xml Pin
Guffa3-Sep-05 15:11
Guffa3-Sep-05 15:11 
QuestionPlease Help:Marshaling Question?? Pin
E6AD3-Sep-05 12:51
E6AD3-Sep-05 12:51 
QuestionURGENTLY complicated Pin
malak nour3-Sep-05 12:03
malak nour3-Sep-05 12:03 
AnswerRe: URGENTLY complicated Pin
Judah Gabriel Himango3-Sep-05 12:26
sponsorJudah Gabriel Himango3-Sep-05 12:26 
Questionhelp regarding server monitoring Pin
vkas_in_guj3-Sep-05 10:10
vkas_in_guj3-Sep-05 10:10 
AnswerRe: help regarding server monitoring Pin
Dave Kreskowiak3-Sep-05 11:34
mveDave Kreskowiak3-Sep-05 11:34 
QuestionHow to Convert this in C# Pin
majidbhutta3-Sep-05 8:21
majidbhutta3-Sep-05 8:21 
Answer[Message Deleted] Pin
philip_cole3-Sep-05 9:18
philip_cole3-Sep-05 9:18 
AnswerRe: How to Convert this in C# Pin
philip_cole3-Sep-05 9:30
philip_cole3-Sep-05 9:30 
QuestionReflection Emit Pin
Yoyosch3-Sep-05 7:07
Yoyosch3-Sep-05 7:07 
AnswerRe: Reflection Emit Pin
if_mel_yes_else_no3-Sep-05 8:38
if_mel_yes_else_no3-Sep-05 8:38 
QuestionTime change events Pin
monrobot133-Sep-05 6:25
monrobot133-Sep-05 6:25 
AnswerRe: Time change events Pin
if_mel_yes_else_no3-Sep-05 8:29
if_mel_yes_else_no3-Sep-05 8:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.