11

I wrote a project in C# that uses a lot of images, Milk models and openGL and i want to pack everything in one exe so i can upload it to my site. Right now i got an exe that is depended on other files like jpgs etc'. I've tried using ILMerge but couldn't get it to work. Is there a simpler solution? thanks.

Segev
  • 19,035
  • 12
  • 80
  • 152

5 Answers5

8

You can put all your files/images into the exe as Embedded Resources.

See How to embed and access resources by using Visual C# (This link currently 404s)

PhonicUK
  • 13,486
  • 4
  • 43
  • 62
logicnp
  • 5,796
  • 1
  • 28
  • 32
  • I don't have "Bitmap File" in the add new item menu. I have Txt file but no bitmap – Segev Aug 08 '12 at 10:38
  • You don't want to do "Add New Item", use "Add Existing item" since you already have the files. – logicnp Aug 08 '12 at 10:41
  • I've changed all my pics to Embedded and i can see that my exe is bigger in the debug folder but if i delete the pics from the debug folder and try to run the exe i get an error "this app must close.." It's like the exe is still depended on the other files although it contains them – Segev Aug 08 '12 at 12:01
  • The how-to embed part of this answer has a link that returns HTTP 404 Not Found. – EmpathicSage Apr 03 '19 at 03:12
  • Looks like an issue on MSs end, because the VB and VC++ versions of the page still link to there. If I can find an updated link I will update this post. – PhonicUK Apr 03 '19 at 12:58
6

Add that as an embedded resource.

Inside Visual Studio :

  1. Go to Solution Explorer,
  2. Right click the image,
  3. GO to Build Actions: Select Embedded Resource.

You will have that image inside the exe. Later you can use Reflection and get the image when you run your application.

========= Getting the Embedded image from the Application =========

First solve the first problem: by putting images as embedded resource.

Second problem: Access the images by using Reflection:

private void Form1_Load(System.Object sender, System.EventArgs e)
{
    System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
    Stream myStream = myAssembly.GetManifestResourceStream("EmbeddingExample.image1.bmp");
    Bitmap image = new Bitmap(myStream);

    this.ClientSize = new Size(image.Width, image.Height);

    PictureBox pb = new PictureBox();
    pb.Image = image;
    pb.Dock = DockStyle.Fill;
    this.Controls.Add(pb);
}

Borrowed Source Code from here:

Rajesh
  • 1,459
  • 10
  • 17
  • What do you mean by Reflection ? I've changed all my pics to Embedded and i can see that my exe is bigger in the debug folder but if i delete the pics from the debug folder and try to run the exe i get an error "this app must close.." – Segev Aug 08 '12 at 11:43
  • It's like the exe is still depended on the other files although it contains them. – Segev Aug 08 '12 at 11:52
  • 1
    Now that you have compiled your Images, you have your images inside the exe and the size of the exe will obviously increase. Since it provided you with the facility of putting your image inside the exe. Now you can take your exe everywhere you want and run your application anywhere without worrying about your images. The images are actually embedded inside the exe, where only the exe itself can access it. If you just want to upload images you can zip and upload it into your website, if its just for uploading stuff. – Rajesh Aug 08 '12 at 12:05
  • 1
    In this context, Reflection is a way to access images, texts, or anything that you build as an embedded resource. Reflection has a broad usage. One of the use is to access your images from the Exe itself. – Rajesh Aug 08 '12 at 12:07
  • That's my problem. The exe is bigger but wont run in another folder. It will only run in the folder with the pics – Segev Aug 08 '12 at 12:07
  • I'll try to explain my self better. Lets say my exe is 1 mb and a.jpg is 4 mb. Did the embedded thing and now my exe is 5 mb. The exe will only run inside a folder with a.jpg. If i'll delete a.jpg the exe will crash to a windows error "this app must close" – Segev Aug 08 '12 at 12:10
  • The files are inside the exe. How do i access them with reflection? i need to add the code above to mine? – Segev Aug 08 '12 at 12:16
  • 1
    http://msdn.microsoft.com/en-us/library/aa984367(v=vs.71).aspx might be helpful for you in that case.. – Rajesh Aug 08 '12 at 12:19
  • I'm using something like this to load my pics. Trying to figure out how to use reflections from your link. `void GenerateTextures() { GL.glGenTextures(5, Textures); string[] imagesName = { "L.jpg", "M.jpg", "RR.jpg", "R.jpg", "floor2.jpg" }; for (int i = 0; i < 5; i++) {` – Segev Aug 08 '12 at 12:33
  • 1
    You should get the images from Reflection : Try something like : myAssembly.GetManifestResourceStream("YOURASSEMBLYNAMESPACE.L.jpg"); also check the link http://msdn.microsoft.com/en-us/library/aa984367(v=vs.71).aspx – Rajesh Aug 08 '12 at 12:37
  • Congratulations!! You did it. Now, please tick my answer as answer :) for all this effort of 2 hours :D – Rajesh Aug 08 '12 at 13:09
3

ilmerge is only for merging .net CLR binaries together, usually for bundling libraries into your main executable.

For things like art assets, you want to embed them as resources into your application. From a resource you can get a stream which lets you work with the data as if it were in a file.

See this MSDN article for information on embedding resources: http://support.microsoft.com/kb/319292

PhonicUK
  • 13,486
  • 4
  • 43
  • 62
2

When you add an image to the project in properties you can set it as Embedded Resource, then it'll be added to the binary file (dll or exe)

Fedor Hajdu
  • 4,657
  • 3
  • 32
  • 50
1

I shall prefer to create a satellite assembly for resource files. http://msdn.microsoft.com/en-us/library/21a15yht%28v=vs.71%29.aspx

Tinku
  • 1,592
  • 1
  • 15
  • 27