9

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...

NotMe
  • 87,343
  • 27
  • 171
  • 245
  • had you tried stored the resource as string and set the value to the url location instead of storing the image in the resouce file if not then you will need to buffer the content – Oscar Cabrero Oct 28 '08 at 22:12
  • It's looking like that's the only option. Update your answer and I'll mark it as accepted. – NotMe Oct 28 '08 at 22:19

3 Answers3

6

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
  • 4,168
  • 8
  • 29
  • 49
  • 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 Oct 28 '08 at 21:27
  • 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... – NotMe Oct 28 '08 at 21:46
  • Okay, past that. However, the image doesn't load and emits the following for the url: http://localhost:32672/System.Drawing.Bitmap – NotMe Oct 28 '08 at 21:55
3

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
  • 29,277
  • 14
  • 101
  • 140
0

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

innaM
  • 47,505
  • 4
  • 67
  • 87