How to use image resource in asp.net website?

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…

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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.

Method 3

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x