【问题标题】:How to use image resource in asp.net website?如何在asp.net网站中使用图片资源?
【发布时间】:2008-10-28 20:44:32
【问题描述】:

我有一个 c# 站点,它使用大量嵌入英文文本的图像。

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

我的 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;" />

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

【问题讨论】:

  • 您是否尝试将资源存储为字符串并将值设置为 url 位置而不是将图像存储在资源文件中,如果没有,那么您将需要缓冲内容
  • 看起来这是唯一的选择。更新您的答案,我会将其标记为已接受。

标签: asp.net resources globalization


【解决方案1】:

你可以将图片的url存储在你的资源文件中,并在控件中使用下面的内联代码

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

更新

这个link 可能对您想要完成的工作有所帮助

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

【讨论】:

  • 确保将 cs_logo 键添加到所有特定和默认资源页面中,即。 Default.aspx.resx 不仅在您的特定文化页面中
  • 我有一个 Resources.resx 文件,其中包含 1 个资源,即 cs_logo 我只是想让它以默认英语工作...
  • 好的,到此为止。但是,图像不会加载并为 url 发出以下内容:localhost:32672/System.Drawing.Bitmap
【解决方案2】:

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

您不必自己创建 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。

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2010-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多