简体   繁体   English

在ASP.NET的WebResource中嵌入位图

[英]Embedding a Bitmap in ASP.NET's WebResource

I am generating a System.Drawing.Bitmap on the fly in an ASP.NET Custom Web Server Control, and then I want to serve this bitmap as part of the WebResource, because I do not want to save it on the hosting computer. 我正在ASP.NET自定义Web服务器控件中动态生成System.Drawing.Bitmap ,然后我希望将此位图作为WebResource的一部分提供,因为我不想将其保存在宿主计算机上。

Is there a way to instruct ASP.NET to serve the generated System.Drawing.Bitmap as part of it's WebResource? 有没有一种方法可以指示ASP.NET将生成的System.Drawing.Bitmap作为其WebResource的一部分提供服务? (therefore making it an "Embedded Resource") (因此使其成为“嵌入式资源”)

Use an HTTP Handler. 使用HTTP处理程序。 The handler is basically a piece of code executed when a resource with a specified extension is requested from your server. 处理程序基本上是从服务器请求具有指定扩展名的资源时执行的一段代码。 Since you want a dynamically generated image, you would do that in the code for the handler and return it as a response. 由于需要动态生成的图像,因此可以在处理程序的代码中执行此操作,并将其作为响应返回。 Embedding wouldn't work, because you can only embed static information. 嵌入将不起作用,因为您只能嵌入静态信息。 Here are some links: 以下是一些链接:

I did that in several ASP.NET projects without an HTTP handler. 我在几个没有HTTP处理程序的ASP.NET项目中做到了这一点。

Say you have an image named "Fingerprint.jpg" and you put it in a subfolder named "Images" in your web control library whose namespace is "MyNamespace". 假设您有一个名为“ Fingerprint.jpg”的图像,并将其放在其名称空间为“ MyNamespace”的Web控件库中的一个名为“ Images”的子文件夹中。 Effectively the computed namespace of your image would be "MyNamespace.Images". 实际上,图像的计算名称空间将是“ MyNamespace.Images”。 Mark that image with build action "Embedded Resource". 使用构建动作“ Embedded Resource”标记该映像。

Now let's assume that in this web control library you have a web control class named "MyNamespace.SampleWebControl". 现在,假定在此Web控件库中有一个名为“ MyNamespace.SampleWebControl”的Web控件类。

In AssemblyInformation.cs I added something like this for each image: 在AssemblyInformation.cs中,我为每个图像添加了以下内容:

[assembly: System.Web.UI.WebResource("MyNamespace.Images.Fingerprint.jpg", "image/jpg")] [程序集:System.Web.UI.WebResource(“ MyNamespace.Images.Fingerprint.jpg”,“ image / jpg”)

where in the example above the Fingerpint.jpg image is stored in the folder named "Images" right underneath the root directory of the web control library. 在上面的示例中,Fingerpint.jpg图像存储在Web控件库的根目录下面的名为“ Images”的文件夹中。

Then in the asp.net page's codebehind you could use something like this: 然后,在asp.net页面的代码后面,您可以使用如下代码:

string imgName = "MyNamespace.Images.Fingerprint.jpg";
Type ctrlType = typeof(MyNamespace.SampleWebControl);
string imageUrl = Page.ClientScript.GetWebResourceUrl(ctrlType, imgName);

And then you can use that imageUrl value as the image's URL in an Image Control or an HTML IMG tag. 然后,您可以使用该imageUrl值作为图像控件或HTML IMG标签中的图像URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM