views:

3140

answers:

3

I have a c# site which makes use of a lot of images with embedded english text.

How can I use a standard resource file to swap out images depending on the language?

I have a resx file in my App_GlobalResources directory, but I can't seem to get it plugged into an asp:image control for the imageurl correctly.

Ideas?

UPDATE:

For some further information, here is the image tag code:

<asp:image runat="server" ID="img2" ImageUrl="<%$Resources: Resource, cs_logo %>" />

The result on the client side is:

<img id="img2" src="System.Drawing.Bitmap" style="border-width:0px;" />

Note that the source is obviously not what I expected...

+1  A: 

you can store the url of the image in your resource file and use the following inline code in the control

<asp:Image ImageUrl="<%$resources:Image1 %>" />

Update

this link could be helpful on what you are trying to accomplish

or

you can also try to stored the resource as string and set the value to the url location instead of storing the image in the resouce file.

Oscar Cabrero
Doesn't work. I get "The resource object with key 'cs_logo' was not found...
Chris Lively
be sure to save add the cs_logo key into all the specific and default resources pages ie. Default.aspx.resx and not only in your specific culuture pages
Oscar Cabrero
I have a Resources.resx file which contains 1 resource that is cs_logo I'm just trying to get it to work in the default english...
Chris Lively
Okay, past that. However, the image doesn't load and emits the following for the url: http://localhost:32672/System.Drawing.Bitmap
Chris Lively
+1  A: 

One thing you might try to do is to create a simple "image service" that can serve up the image in the proper format from the embedded resources.

You don't have to create web service itself, you simply create an aspx page and in the code behind you change the the Response.ContentType to be "image/png" or whatever format you prefer. This also requires a get parameter in the URL to the page itself, but that can be easily filtered. So the Page_Load method of your image service might look something like this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

      Dim FinalBitmap As Bitmap
      Dim strRenderSource As String
      Dim msStream As New MemoryStream()

      strRenderSource = Request.Params("ImageName").ToString()

      ' Write your code here that gets the image from the app resources.
      FinalBitmap = New Bitmap(Me.Resources(strRenderSource))
      FinalBitmap.Save(msStream, ImageFormat.Png)

      Response.Clear()
      Response.ContentType = "image/png"

      msStream.WriteTo(Response.OutputStream)

      If Not IsNothing(FinalBitmap) Then FinalBitmap.Dispose()

End Sub

Then back on your ASPX page you have...

<asp:Image ImageUrl="http://localhost/GetImage.aspx?ImageName=Image1" />

Oh, and don't forget to import System.Drawing and System.Drawing.Imaging in the page.

Dillie-O
A: 

if you are using global resources file you need to add it like this

<img id="WelocmeICon" runat="server"  alt="welcome icon" 
     src="<%$resources:NmcResource,WelcomeIcon %>"  />

and because i use img control i added runatserver and id for it

Waleed Mohamed