1

I'm developing a winform application. It has a reference to a dll library. I want to use a function 'PDFImage' in this library. This function is used to put images into a PDF documnent. The function 'PDFimage' has an argument 'FileName' of type String which takes the file location of the image.

Now I have to put the image as a separate file with the .exe file created after the project is built. This is not convenient for me. What I do now is I mention the file name of the image as the function parameter like 'Flower.jpg'. And I have kept the image in the \bin\debug folder. I don't want to do it like this as this needs the image file to be placed seperately with the executable file.

What I am trying to do is as follows: I added the image files to the Resources folder as existing item. Now, to call the function PDFImage, I need to pass the file name as argument. How can I do this?

I have the source code of dll with me. Is it better to modify the source code as required and create another dll rather than what I am doing now?

Foreever
  • 7,099
  • 8
  • 53
  • 55
  • You should show some code of what isn't working. At a quick glance it sounds like you simply should store the file in a different folder. – Justin Helgerson Sep 16 '13 at 21:24
  • @Foreever You cannot pass an Image Resource as a file path,both differ in Types.The Image Resource is of `Image` type and `filename` is `string` type.If possible try to modify the function parameter and make is as `SomeFunction(Image image)` i.e change the string argument to `Image` type and then you can use the Image Resource as a function parameter. – devavx Sep 16 '13 at 21:26
  • @Ek0nomik I'm not sharing any code because I haven't done anything. I Have no idea where to start. I am storing the same in a folder. But don't I need to keep the folder outside of the application while running. As I mentioned, I can only pass the file location of the image to the function. I believe it's not possible to pass the file location of an image in resource folder. Does embedding this images do any good? – Foreever Sep 16 '13 at 21:30
  • @AviralSingh But Aviral the function belongs to a dll. So, I think I can't modify it as I want. – Foreever Sep 16 '13 at 21:31
  • @Foreever Well if the code is in a DLL,then sadly you've no other way than to leave your image as a loose resource in some folder. – devavx Sep 16 '13 at 21:34
  • @Foreever Although there's no straightforward method to deal with this,however there's a trick you can try to make this work.Try to save the resource as a local file when your application needs it and then you can use the path of this file as a string parameter to that function. – devavx Sep 16 '13 at 21:38
  • @AviralSingh I think that is what i am doing now. But I want to distribute the software as a single executable file without any installation. Is it possible? – Foreever Sep 16 '13 at 21:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37481/discussion-between-aviral-singh-and-foreever) – devavx Sep 16 '13 at 21:44

2 Answers2

1

All you can do with that is get the resource, save it to a file (temporary one may be) and then pass the filename to the function. Most function that take a file in .net also take a stream, so if you have control of both sides, I'd do that and then you don't have to mess about with the file system.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
1

See if this helps;

    string apppath = Application.StartupPath;
    string resname = @"\Resource.bmp";
    string localfile = "";
    localfile = apppath + resname;//Create the path to this executable.
    //Btw we are going to use the "Save" method of Bitmap class.It
    //takes an absolute path as input and saves the file there.

    //We accesed the Image resource through Properties.Resources.Settings and called Save method
    //of Bitmap class.This saves the resource as a local file in the same folder as this executable.
    Properties.Resources.Image.Save(localfile);

    MessageBox.Show("The path to the local file is : " + Environment.NewLine + localfile + Environment.NewLine +
    "Go and check the folder where this executable is.",
    this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);


    //localfile is the path you need to pass to some function like this;
    //SomeClass.Somefunction(localfile);

Hope this helps and here is a sample if you need.

devavx
  • 1,035
  • 9
  • 22
  • I see in your sample project you added the image simply, not as embedded. I also added the image as an existing item and didn't make it as an embedded resource. But the file is not listed in Properties. What should I set the build action for this file? – Foreever Sep 17 '13 at 07:12
  • I found the answer myself [here](http://stackoverflow.com/questions/90697/how-to-create-and-use-resources-in-net) – Foreever Sep 17 '13 at 07:24