Link to home
Start Free TrialLog in
Avatar of Robinsonx6
Robinsonx6Flag for Mauritius

asked on

URGENT :custom control nested image

I have an image in my custom control (web) is there any way for me to not to have to copy the image over to the different web folders which use the control, it seems very messy to me that the whole point of custom controls is to make things easyer for deployment, cant i embed the image somehow so it gets stored within the custom controls dll?  what is the best way of referencing an image?

Help please?

cant an image be stored as a binary string or something?
Avatar of nauman_ahmed
nauman_ahmed
Flag of United States of America image

You can use image in the .resx file. Check out reseditor that can be found at the following location under default installation of VS.Net: To read from resource you can use ResourceManager class. Checkout the MSDN topic Resources in MSDN library.

C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Samples\Tutorials\resourcesandlocalization

For example to retrieve an image that is stored in the an assembly use the following code:

System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file =
    thisExe.GetManifestResourceStream("AssemblyName.ImageFile.jpg");
this.pictureBox1.Image = Image.FromStream(file);


Best, Nauman.
Avatar of mAjKoL
mAjKoL

The best way to keep images in web appz is to keep them in their directories and giving the source in controls such as "~/images/my.jpg".
~ will indicate your application root so you'll have no problem when deplying your app.
Avatar of Robinsonx6

ASKER

nauman ahmed, how do i get the image into the Assembly in the first place? and how does this save me from having to copy the image into the image directories?

majKoL, i am trying to avoid this method as the control is used in many different webapps i know i can keep the image in a seperate folder for all webapps to referense in the normal way but this isnt what i am trying to acheive, i dont want to have to reference an actual file.

if this works i want to do the same with xml files aswell, why im not sure but it has become a mission to learn how to do it!
Avatar of Bob Learned
If you have the control in an referenced assembly, with an embedded resource, you can then access the control from any web page, and not have to worry about having a file any where.

Bob
hi again TheLearnedOne, your answer is what i am trying to acheive, How do get the image into the referenced assembly?

thanks
Thought you'd never ask:

The coolest way to embed icons in an assembly is this:

(1) Add an icon file to the project.
(2) Select the icon entry in the solution explorer
(3) Change the 'Build Action' in the properties window from 'Content' to 'Embedded Resource'
(4) Add the following code to a module, class, or whatever you want:

   Public Function GetEmbeddedIcon(ByVal strName As String) As Icon

      Return New Icon(System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(strName))

   End Function


   Public Function GetEmbeddedResourceNames() As String()

      Return System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceNames

   End Function

(5) Add the following lines to the Form_Load event:

      Dim arrayResources As String() = GetEmbeddedResourceNames()

      ' Use whatever index you would like to extract:  If you have two icons embedded, then the values would be 0 and 1.
      Me.Icon = GetEmbeddedIcon(arrayResources(1))


Bob
thanks Bob, itl take me a while to get this one as its 8:30 at night here and my wife is kicking my but to go to the restaurant! on my return ill sort this one then, award points

thanks Bob
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Robinsonx6,

The utility that I told you simplifies adding and retrieving the resources in your .dll or .exe file of the project. In your asp.net project in VS.Net:

Select Project -> Add new Item
Select Resources -> Assembly Resource File
name your resource say images.resx and select open.
Images.resx will open in VS.Net. Close the file.
browse to C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Samples\Tutorials\resourcesandlocalization\reseditor and select build if reseditor.exe is not there.
Open reseditor.exe
Select File -> Open and browse to Images.resx of your project
Type MyImage in the Add field and select Add.
In the resource list pane, select the browse button and select your image.
Select File -> Save As and browse to your project folder and overwrite Images.resx.

ResEditor do not allow saving the .resx file but I think MS will update it in future :). As far as retrieving a resource is concern, you can use the ResourceManager class. I will try to post some code for that later today.

Best, nauman.
ok guys i have the following bit of code now as a class, Bob i know you like VB but could you answer in c# please

public  System.Drawing.Image GetEmbeddedImage()
{
      System.Drawing.Image image;
      System.Reflection.Assembly thisExe;
      thisExe = System.Reflection.Assembly.GetExecutingAssembly();
      System.IO.Stream file = thisExe.GetManifestResourceStream("AssemblyName.Ttl_userreg.gif");
      image = System.Drawing.Image.FromStream(file);

      return image;
}
nauman or Bob I think you are both missing the point that this a webControl so the control i need to use is a 'System.Web.UI.WebControls.Image' which does not have a property called
 'Image' only ImageURL there for:-

System.Web.UI.WebControls.Image image;
image.ImageUrl=GetEmbeddedImage();

doesnt work

How do i now get this 'System.Drawing.Image' into my Control as a System.Web.UI.WebControls.Image

Thanks in advance guys!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok i think im lost totlaly here guys:- i have tried now t create myown imagecontrol and this is its code
            public  void GetEmbeddedImage()
            {
                  System.Drawing.Image image;
                  System.Reflection.Assembly thisExe;
                  thisExe = System.Reflection.Assembly.GetExecutingAssembly();
                  System.IO.Stream file =
                        thisExe.GetManifestResourceStream("lookb4controls.Ttl_userreg.gif");
                  image = System.Drawing.Image.FromStream(file);

                  
                  long lFileSize;
                  lFileSize = file.Length;
                  byte[] bBuffer = new byte[(int)lFileSize];
                  file.Read(bBuffer, 0, (int)lFileSize);

                  System.Web.HttpContext.Current.Response.ClearContent();
                  System.Web.HttpContext.Current.Response.ContentType = "image/gif";
   
                  System.Web.HttpContext.Current.Response.BinaryWrite(bBuffer);
                  file.Close();
                  System.Web.HttpContext.Current.Response.End();

                  
            }

this actually displays the empty image box you know the one which is a blank litle box with a red cross in it, but it doesnt display the image!!

Please help guys those last two articles have confused me, surely i am just trying to output the image held in the Resource Stream?
Thanks
help guys please dont forget about me!
Too many projects, too little time.  There are some confusing signals here, so are your mixing up things.  Where is the file now (embedded or referenced in a resource file)?

Bob
thanks Bob

embedded!  and yes i have got very confused over this!

lFileSize = file.Length; is returning a large number so i assume its there and im getting it, or am i confused to the point of stupidity!

im just trying to get the embedded image into an <asp:image > control

Help please if you have time

regards
chris
Robinsonx6,

Please download the sample solution from the following URL:

http://www.techinet.com/downloads/resourcedemo.zip

Unzip the file to d:\Projects\resourcedemo or update ResourceDemo.csproj.webinfo file and change the location of the project to a suitable path.

I have used reseditor to embed my resources. This program is the part of MSDN and can be found at the following location:

C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Samples\Tutorials\resourcesandlocalization

Embed your image resource using the ResEditor and make sure you build your program each time you update the AppRes.resx file. Refer to webform1.aspx to see how I retrieve the image in asp.net image control. Its just one line:

this.Image1.ImageUrl = "GetImage.aspx?ResourceID=APP_IMAGE";


Best, Nauman.
thank you nouman im knackered as ive been doing a 23 hr shift and my wife is definately kicking my but this time, so i will look at this tommorow as it is 20:50 here in mauritius

thanks will respond tommorrow
Robinsonx6,

Any luck with embedding your image?

Best, Nauman.
no not yet sorry guys, we lost all internet and phone lines due to a lorry crashing into the telecom pole, and this is mauritius so it took forever to get online again, i will be looking at this again once i have caught up my work load
Sorry guys i have had no time on this one but i am going to assign points to both of you anyway as you allways responded quickly and you have given me a broader understanding of Control buiding, thanks guys for your patience