如何在asp.net网站中使用图片资源?

发布于 2024-07-08 13:13:07 字数 500 浏览 6 评论 0原文

我有一个 ac# 网站,它使用了大量带有嵌入英文文本的图像。

如何使用标准资源文件根据语言交换图像?

我的 App_GlobalResources 目录中有一个 resx 文件,但我似乎无法将其正确插入 imageurl 的 asp:image 控件中。

有想法吗?

更新:

有关更多信息,这里是图像标签代码:

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

客户端的结果是:

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

请注意,源显然不是我所期望的......

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

唯憾梦倾城 2024-07-15 13:13:07

您可以将图像的网址存储在资源文件中,并在控件中使用以下内联代码

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

更新

链接可以对您想要完成的任务有所帮助

,或者

您也可以尝试将资源存储为字符串并将值设置为 url 位置,而不是将图像存储在资源文件中。

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.

烟雨凡馨 2024-07-15 13:13:07

您可能尝试做的一件事是创建一个简单的“图像服务”,它可以从嵌入式资源以正确的格式提供图像。

您不必创建 Web 服务本身,您只需创建一个 aspx 页面,然后在后面的代码中将 Response.ContentType 更改为“image/png”或您喜欢的任何格式。 这还需要页面本身的 URL 中包含一个 get 参数,但可以轻松过滤。 因此,图像服务的 Page_Load 方法可能如下所示:

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

然后回到 ASPX 页面,您...

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

哦,不要忘记在页面中导入 System.Drawing 和 System.Drawing.Imaging。

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.

我们的影子 2024-07-15 13:13:07

如果您使用的是全局资源文件,您需要像这样添加它

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

,因为我使用 img 控件,所以我为其添加了 runatserver 和 id

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文