如何在Web服务器控件中嵌入静态图像资源?

发布于 2024-08-21 08:20:51 字数 1924 浏览 10 评论 0原文

我将创建一个代表树视图的 Web 服务器控件。所以我想使用 2 个图像作为 + 和 - 来展开/折叠。如何将其构建到控件中,以便在页面上呈现时可以用作图像源?

由于这将位于已编译的 Web 控件库中,因此我不想在 Web 应用程序中依赖外部图像。

编辑:
基于这个答案 by Andre Kraemer 我执行了以下操作:

在 AssemblyInfo.vb 中:

<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.Resources.plus.gif", "image/gif")> 
<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.Resources.minus.gif", "image/gif")> 

在我的 RenderContents 覆盖中:

Dim lPlusImage As New WebControls.Image()
Dim lMinusImage As New WebControls.Image()
lPlusImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyCompany.MyWebControls.Resources.plus.gif")
lMinusImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyCompany.MyWebControls.Resources.minus.gif")
lPlusImage.RenderControl(output)
lMinusImage.RenderControl(output)

我的程序集名称是 MyWebControls

我的根命名空间是MyCompany.MyWebControls

图像 plus.gifminus.gif 位于名为 Resources 的文件夹中,并且图像具有 Build Action 设置为嵌入式资源

它仍然不起作用。我没有收到任何错误。我已经尝试直接在浏览器中生成的图像网址,机器人没有任何反应,只是一个空白页面。

注意:
我尝试在资源名称中使用无效路径,结果完全相同,这让我想知道是否需要做一些特殊的事情来将实际资源映射到资源名称。仅当我在代码中使用的名称与 AssemblyInfo 中指定的名称不同时,我才会收到 404 Not Found 错误,它与指向实际资源的路径无关!

编辑:

我找到了解决方案

我发现这是C#和VB之间的区别。请参阅我自己的答案 这个问题。

I am going to create a web server control representing a treeview. So I want to use 2 images for + and - for expand/collapse. How can I build this into the control in a way that can be used as image source when rendered on the page?

Since this will be in a compiled web controls library, I don't want to rely on external images in the web application.

Edit:
Based on this answer by Andre Kraemer I did the following:

In AssemblyInfo.vb:

<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.Resources.plus.gif", "image/gif")> 
<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.Resources.minus.gif", "image/gif")> 

In my RenderContents override:

Dim lPlusImage As New WebControls.Image()
Dim lMinusImage As New WebControls.Image()
lPlusImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyCompany.MyWebControls.Resources.plus.gif")
lMinusImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyCompany.MyWebControls.Resources.minus.gif")
lPlusImage.RenderControl(output)
lMinusImage.RenderControl(output)

My Assembly name is MyWebControls.

My Root Namespace is MyCompany.MyWebControls.

The images plus.gif and minus.gif are located in a folder named Resources, and the images have Build Action set to Embedded Resource.

It still does not work. I get no errors. I have tried the generated image url directly in the browser, bot nothing happens, just a blank page.

Note:
I tried to use an invalid path in the resource name, and the result was exactly the same, which made me wonder if I need to do something special to map the actual resource to the resource name. I got a 404 Not Found error only if I used different name in the code than what was specified in AssemblyInfo, it had nothing to do with path was pointing to an actual resource!

Edit:

I found the solution!

I found out that it is a difference between C# and VB. See my own answer to this question.

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

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

发布评论

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

评论(3

逆蝶 2024-08-28 08:20:51

将这两个图像添加到树视图控件项目的名为 images 的子文件夹中。然后将属性网格中的构建操作从内容更改为嵌入资源。

除此之外,您还必须在 assemblyinfo.cs 文件中将这两个图像注册为程序集的嵌入式资源,如下所示:

[assembly: System.Web.UI.WebResource("YourProjectsNameSpace.Images.plus.gif", "img/gif")]
[assembly: System.Web.UI.WebResource("YourProjectsNameSpace.Images.minus.gif", "img/gif")]

由于这些图像现在将嵌入到您的控件程序集中,您可以使用 ClientScriptManager 访问它们,如下所示:

string plusImageUrl  = Page.ClientScript.GetWebResourceUrl(this.GetType(), "YourProjectsNameSpace.Images.plus.gif");
string minusImageUrl  = Page.ClientScript.GetWebResourceUrl(this.GetType(), "YourProjectsNameSpace.Images.minus.gif");

add the two images to a subfolder called images of your treeview control project. Then change their build action in the property grid from content to embedded resource.

In addition to that, you have to register those two images as embedded resources for your assembly in the assemblyinfo.cs file like this:

[assembly: System.Web.UI.WebResource("YourProjectsNameSpace.Images.plus.gif", "img/gif")]
[assembly: System.Web.UI.WebResource("YourProjectsNameSpace.Images.minus.gif", "img/gif")]

As those images will be embedded into your controls assembly now, you can access them using the ClientScriptManager like this:

string plusImageUrl  = Page.ClientScript.GetWebResourceUrl(this.GetType(), "YourProjectsNameSpace.Images.plus.gif");
string minusImageUrl  = Page.ClientScript.GetWebResourceUrl(this.GetType(), "YourProjectsNameSpace.Images.minus.gif");
梦途 2024-08-28 08:20:51

好的,我在 本文。我认为安德烈·克雷默(Andre Kraemer)是公认的答案,因为他为我指明了正确的方向。他不可能知道C#和VB的名字结构有细微的差别!

以下是适用于 VB 的解决方案:

重要的区别是在 VB 中,文件路径不包含在名称中。

在我的示例中,项目中的根命名空间是 MyCompany.MyWebControls,图像文件位于名为 Resources 的子文件夹中。在 C# 中,子文件夹是资源名称结构的一部分,但在 VB 中不是。

在 C# 中,这将是:

[assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.Resources.plus.gif", "image/gif")]

在 VB 中,我们必须忽略资源名称路径中的文件路径:

<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.plus.gif", "image/gif")> 

因此,当我们知道这一点时,我的完整示例将是:

在 AssemblyInfo.vb 中:

<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.plus.gif", "image/gif")> 
<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.minus.gif", "image/gif")> 

在我的 RenderContents 覆盖中:

Dim lPlusImage As New WebControls.Image()
Dim lMinusImage As New WebControls.Image()
lPlusImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyCompany.MyWebControls.plus.gif")
lMinusImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyCompany.MyWebControls.minus.gif")
lPlusImage.RenderControl(output)
lMinusImage.RenderControl(output)

瞧,它有效了!

OK, I found the answer in this article. I give credit to Andre Kraemer as the accepted answer, because he pointed me in the right direction. He could not know that there is a slight difference in the name structure between C# and VB!

Here is the solution that works for VB :

The important difference is that in VB, the file path is not included in the name.

In my example, the root namespace in the project is MyCompany.MyWebControls and the image files is in a subfolder named Resources. In C#, the subfolder is part of the resource name structure, but not in VB.

In C#, this would be:

[assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.Resources.plus.gif", "image/gif")]

In VB, we must ignore the file path in the resource name path:

<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.plus.gif", "image/gif")> 

So when we know this, my full example will be:

In AssemblyInfo.vb:

<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.plus.gif", "image/gif")> 
<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.minus.gif", "image/gif")> 

In my RenderContents override:

Dim lPlusImage As New WebControls.Image()
Dim lMinusImage As New WebControls.Image()
lPlusImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyCompany.MyWebControls.plus.gif")
lMinusImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyCompany.MyWebControls.minus.gif")
lPlusImage.RenderControl(output)
lMinusImage.RenderControl(output)

And voilá, it works!

涙—继续流 2024-08-28 08:20:51

您的形象->属性->构建动作 ->嵌入式资源。它将与您的控件或库一起编译。

Your image -> Properties -> Build Action -> Embedded Resource. It will be compiled with your control or library.

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