How to put images into a seperate DLL file outside of .exe

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Samishii23
    New Member
    • Sep 2009
    • 246

    How to put images into a seperate DLL file outside of .exe

    So Im going to have a 15mb .exe file if I can't manage to get all the images put into a seperate file. Whether its a .dat, .db, or .dll file. It don't matter.

    I've googled for this question a few times. The only thing I've found is...
    in order to put an image inside a dll you need todo the following
    right click on the image-> Properties -> now insted of Content put the image as Embeded Resource and thats it :-)
    That did not work for me, and I'm pretty new to the language and using Visual Studio '05.

    So any ideas please?
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    It worked fine for me... here's what I did.

    1) Created a test project that was a windows form
    2) Created a test project that was a class library
    3) Right-clicked on the class library and selected Add->Existing Item
    4) Selected the image I wanted to use
    5) Clicked the image to bring up the properties for it
    6) Under Build Action, I selected Embedded Resource
    7) In my class library, I did the following...

    Code:
        public static class Class1
        {
            public static Image GetBulletImage()
            {
                Image img = new Bitmap(@"F:\My Documents\Visual Studio 2008\Projects\TEST\ClassLibrary1\BULLET.JPG");
    
                return img;
            }
        }
    (Node, to get the path I just dragged the image from my Solution Explorer to the code window)

    8) In my test form, I added a reference to my class library
    9) In the constructor of my main form, I did...

    Code:
    this.BackgroundImage = Class1.GetBulletImage();
    10) Build and run... the background image is set to the image I loaded.

    I hope that helps, good luck! :)

    Comment

    • Samishii23
      New Member
      • Sep 2009
      • 246

      #3
      Ty for the detailed outline of your process. Only 1 problem now, when I go to "Add Reference" I can't find the class library, and I can not build the thing because I can't put using System.Drawing; in, im assume because its a class library and not a Windows Application.

      The error the compiler gives me is "The type or namespace name 'Drawing' does not exist in the namespace 'System' (are you missing an assembly reference?)"

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Class library doesn't come from adding a reference, it comes from adding a new project. Right click your Solution and select Add->New Project. Unless you mean you can't add a reference to your class library from your main form, in which case see below...

        Also, you need to add a reference to System.Drawing before you can put a using statement in. In the project that needs the reference added, right-click references and select Add Reference.

        If you want to add a reference to a project in your solution, once you've done the above, click the Projects tab. It should be in there :)

        Comment

        • Samishii23
          New Member
          • Sep 2009
          • 246

          #5
          Well. After not understanding what the h3ll you were getting at. I stumbled upon my answers with your guidance.

          Damn references from h3ll. Lol.
          Reference this, reference that.. Lol. Wish Microsoft would make that a little easier. =)

          Thanks again Gary

          Comment

          • Samishii23
            New Member
            • Sep 2009
            • 246

            #6
            Crap. Ok sorry. Not done. meh.
            Having issues accessing the images from the Class Project.

            Before I had them setup under properties, and I could simply access them by their name.
            Code:
            someimage.Image = @"C:\Image.jpg";
            Fix something, and more things break because of the fix. lol

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              Re-check my code above again, you're missing something vital ;)

              Hint: You're trying to assign a "string" type to what is an "image" type.

              Comment

              • Samishii23
                New Member
                • Sep 2009
                • 246

                #8
                Well from your code, i've gotten it down to:
                Code:
                Image[] NewImage = new Bitmap(@"C:\Image.jpg");
                But like I was saying. When the images were directly imbedded into the Form resources, I could access the Images through their name. Not I'm not having such luck.

                Also what I get from your code is that I have to use functions to pass things.
                One thing I would like to pass from the Class Project to the main Form Project, but arrays are annoying....... ...

                Comment

                • GaryTexmo
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1501

                  #9
                  I'm not sure if you can still use that... you're assigning a single Bitmap to an array.

                  Anyway, as for just using the name... I don't know. I know how to do it with XNA and LoadContent<> but I don't know of a .NET equivalent. Do you need to access it like that?

                  Comment

                  • Samishii23
                    New Member
                    • Sep 2009
                    • 246

                    #10
                    I don't need to access it exactly like that. If I could figure out how to return the array of Bitmap objects that would be fine, but the compiler has a fit, and won't let me return the array variable. It wants me to return a single item out of the array.

                    Although accessing it at out would be nice rather then having to call a function for every image call, know what I mean?

                    Comment

                    • GaryTexmo
                      Recognized Expert Top Contributor
                      • Jul 2009
                      • 1501

                      #11
                      Errr no, it's because...
                      Code:
                      Image[] NewImage = new Bitmap(@"C:\Image.jpg");
                      ... should be...

                      Code:
                      Image NewImage = new Bitmap(@"C:\Image.jpg");
                      If you want to put it into a list...

                      Code:
                      List<Image> imageList = new List<Image>();
                      imageList.Add(NewImage);
                      If the list was previously empty, then that image would now be the first element...

                      Code:
                      Image firstImage = imageList[0];

                      Comment

                      • Samishii23
                        New Member
                        • Sep 2009
                        • 246

                        #12
                        Is that List callable to the Windows Form project rather then just the Class Project or can I return the entire list variable via a function?

                        Sorry about all the dumb questions. Really I'm just looking for more clarification. Because Im new, and Im not good with C Sharp let alone cross-project referencing.

                        Comment

                        • GaryTexmo
                          Recognized Expert Top Contributor
                          • Jul 2009
                          • 1501

                          #13
                          The list would be in your windows form... you load it from the dll that gets your image for you, which is where you said (per your title) you want to keep them.

                          Maybe step back a little here and go through the code I posted throughout this thread, then experiment around with it. It's easy to get overwhelmed with new information. Take the time to familiarize yourself with it, and try a few things. For more information on how projects work, dive right into google. There's tons of great information and tutorials out there :)

                          Also, sorry if I'm not explaining things very clearly. If anybody else around here can think of a better way of putting it, please feel free to correct me. I'm guilty of tunnel vision at times ;)

                          Comment

                          Working...