Robinsonx6
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?
Help please?
cant an image be stored as a binary string or something?
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.
~ will indicate your application root so you'll have no problem when deplying your app.
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!
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!
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
Bob
ASKER
hi again TheLearnedOne, your answer is what i am trying to acheive, How do get the image into the referenced assembly?
thanks
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.Ass embly.GetE xecutingAs sembly.Get ManifestRe sourceStre am(strName ))
End Function
Public Function GetEmbeddedResourceNames() As String()
Return System.Reflection.Assembly .GetExecut ingAssembl y.GetManif estResourc eNames
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(arrayResou rces(1))
Bob
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.Ass
End Function
Public Function GetEmbeddedResourceNames()
Return System.Reflection.Assembly
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(arrayResou
Bob
ASKER
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
thanks Bob
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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\Tuto rials\reso urcesandlo calization \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.
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\Tuto
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.
ASKER
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 .GetExecut ingAssembl y();
System.IO.Stream file = thisExe.GetManifestResourc eStream("A ssemblyNam e.Ttl_user reg.gif");
image = System.Drawing.Image.FromS tream(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=GetEmbedded Image();
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!
public System.Drawing.Image GetEmbeddedImage()
{
System.Drawing.Image image;
System.Reflection.Assembly
thisExe = System.Reflection.Assembly
System.IO.Stream file = thisExe.GetManifestResourc
image = System.Drawing.Image.FromS
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' only ImageURL there for:-
System.Web.UI.WebControls.
image.ImageUrl=GetEmbedded
doesnt work
How do i now get this 'System.Drawing.Image' into my Control as a System.Web.UI.WebControls.
Thanks in advance guys!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This article is also great:
http://authors.aspalliance.com/stevesmith/articles/displayimage.asp
-Nauman.
http://authors.aspalliance.com/stevesmith/articles/displayimage.asp
-Nauman.
ASKER
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 .GetExecut ingAssembl y();
System.IO.Stream file =
thisExe.GetManifestResourc eStream("l ookb4contr ols.Ttl_us erreg.gif" );
image = System.Drawing.Image.FromS tream(file );
long lFileSize;
lFileSize = file.Length;
byte[] bBuffer = new byte[(int)lFileSize];
file.Read(bBuffer, 0, (int)lFileSize);
System.Web.HttpContext.Cur rent.Respo nse.ClearC ontent();
System.Web.HttpContext.Cur rent.Respo nse.Conten tType = "image/gif";
System.Web.HttpContext.Cur rent.Respo nse.Binary Write(bBuf fer);
file.Close();
System.Web.HttpContext.Cur rent.Respo nse.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
public void GetEmbeddedImage()
{
System.Drawing.Image image;
System.Reflection.Assembly
thisExe = System.Reflection.Assembly
System.IO.Stream file =
thisExe.GetManifestResourc
image = System.Drawing.Image.FromS
long lFileSize;
lFileSize = file.Length;
byte[] bBuffer = new byte[(int)lFileSize];
file.Read(bBuffer, 0, (int)lFileSize);
System.Web.HttpContext.Cur
System.Web.HttpContext.Cur
System.Web.HttpContext.Cur
file.Close();
System.Web.HttpContext.Cur
}
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
ASKER
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
Bob
ASKER
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
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.webinf o 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\Tuto rials\reso urcesandlo calization
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.
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.webinf
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\Tuto
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=
Best, Nauman.
ASKER
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
thanks will respond tommorrow
Robinsonx6,
Any luck with embedding your image?
Best, Nauman.
Any luck with embedding your image?
Best, Nauman.
ASKER
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
ASKER
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
C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Samples\Tuto
For example to retrieve an image that is stored in the an assembly use the following code:
System.Reflection.Assembly
thisExe = System.Reflection.Assembly
System.IO.Stream file =
thisExe.GetManifestResourc
this.pictureBox1.Image = Image.FromStream(file);
Best, Nauman.