how to create resource file

i want to make my programs easier to distribute by including all images, icons, etc., in the exe. does anyone know a non-complicated way to do this?

Comments

  • The only way I know isn't complicated, but I've always thought it more trouble than it'w worth (My personal preference being to ensure that all required images are safely saved in the Bin Folder of the project).
    But, if you want to give it a go, here's the steps:
    1. Add the image file to your project using Add Existing Item. (You may have to choose Show All Files to see it in the Solution Explorer window)
    2. Select the image file in Solution Explorer
    3. In the image file's properties window, change the Build Action from Compile (the default) to EmbeddedResource.
    4. Save the project.
    To access it in code, do this, or something similar:
    [code]
    Dim aStream As System.IO.Stream
    ' Change the namespace name and file name in the next line
    aStream = Me.GetType().Assembly.GetManifestResourceStream("WindowsApplication102.oscar2.bmp")
    Dim aBitmap As New Bitmap(aStream)
    ' Display the image
    Me.PictureBox2.Image = aBitmap
    [/code]

    Enjoy!

    =============================================================

    : i want to make my programs easier to distribute by including all images, icons, etc., in the exe. does anyone know a non-complicated way to do this?
    :


  • how do you make sure files are safely saved in bin?
    I used the show files and all the jpg files are indeed in the bin, yet when i try to run the exe file as standalone in different part of hard drive, the program wont access the jpg.

    also, what you just showed, does it work for jpg files also or do they have to be bitmaps?
  • couple of more questions.
    1. When i add the image file or jpg to my project, do I need to put it in the bin to begin with?
    2. The name of my project is MCR and the name of the jpg file is mike. Now, does that mean the command is Me.GetType().Assembly.GetManifestResourceStream("MCR.mike.jpg")?
    3. If mike.jpg is in the bin folder, then what goes into the GetManifestResourceStream( ) ?

    mikecoon
  • [b][red]This message was edited by kainsworth at 2004-7-8 0:7:16[/red][/b][hr]
    :
    : how do you make sure files are safely saved in bin?
    : I used the show files and all the jpg files are indeed in the bin, yet when i try to run the exe file as standalone in different part of hard drive, the program wont access the jpg.
    :
    : also, what you just showed, does it work for jpg files also or do they have to be bitmaps?
    :


    Hi
    You may have slightly mis-read my original post. When I talked about the files being "safely saved in the Bin Folder", what I was saying was that that is my preference - i.e. I prefer to save the files in the Bin Folder, make sure they are copied to any machine on which the app is deployed, and get the application to access them at run time as necessary. In others words, this is the opposite approach to the one asked about ( embedding files into the application). My personal preferred approach is NOT to embed them.

    If they are successfully embedded, as described in my original answer, then it doesn't matter where the image files are saved. Once successfully embedded then they have become part of the complete code package itself, in just the same way as, for instance, the graphical layout of a form is part of the complete saved package.

    It's not a particularly easy concept to describe briefly here, but I hope the above clears up some of the puzzle.

    You can embed jpgs in the same way as described.




  • : couple of more questions.
    : 1. When i add the image file or jpg to my project, do I need to put it in the bin to begin with?
    : 2. The name of my project is MCR and the name of the jpg file is mike. Now, does that mean the command is Me.GetType().Assembly.GetManifestResourceStream("MCR.mike.jpg")?
    : 3. If mike.jpg is in the bin folder, then what goes into the GetManifestResourceStream( ) ?
    :
    : mikecoon
    :
    =
    Hi Mike
    It's a mighty confusing topic this one (or so I've always found, anyway). In a simple application the Namespace name by default will be the same as the name of the single Project it uses. (You can delve into, and make major changes to, the NameSpaces used in an application but in these early stages I think that maybe the best approach is the "If it ain't broke don't fix it" one) :-)
    The answers to your questions are:
    1. Yes, if you want to point to it in your app in the normal way, eg:
    [code]
    picturebox1.image="Mike.jpg"
    [/code]
    ie. No embedding involved.
    or
    No, it doesn't matter where you save it if you are going to embed it, because it will be embedded (as explained in my earlier reply) and not linked to at runtime.

    2. Yes.

    3. Something like:
    [code]
    Dim aStream As System.IO.Stream
    aStream = Me.GetType().Assembly.GetManifestResourceStream("MCR.mike.jpg")
    Me.PictureBox1.Image = Image.FromStream(aStream)
    [/code]
    will work, BUT only if you have previously embedded the file named mike.jpg as described in my first answer to the original poster.

    Hope this gets you on track

    Ged


  • thanks.

    Plus to make it work , you need these two lines which I did not have at first:

    Imports System.IO
    Imports System.Reflection

    these lines goes right after the option statements liek option explicit.

    mikecoon
  • [b][red]This message was edited by kainsworth at 2004-7-9 2:8:8[/red][/b][hr]
    Good to hear you got it working to your satisfaction.

    Actually, the last example I sent will work without the Imports statements (although I agree that it's good policy to include them as a general rule, so I maybe should have mentioned that). However, by using what is known as the fully qualified reference, eg
    [code]
    Dim aStream As System.IO.Stream
    ' and not
    Dim aStream as Stream
    [/code]
    then you don't strictly need the Imports statement. The additional references to System.IO in that first line automatically do the trick for you.
    I'm pretty sure that the very simple examples I gave will both in fact compile and run without the
    [code]
    Imports System.Reflection
    [/code]
    statement (they just did on my system with VS 2003, anyhow :-)). But as soon as you start moving deeper into this whole tricky area, I agree that you will need it, so again it's maybe a good habit to get into.

    Regards

    Ged



    : thanks.
    :
    : Plus to make it work , you need these two lines which I did not have at first:
    :
    : Imports System.IO
    : Imports System.Reflection
    :
    : these lines goes right after the option statements liek option explicit.
    :
    : mikecoon
    :



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories