自定义服务器控件嵌入图像
我创建了一个自定义服务器控件并尝试使用 System.Web.UI.WebControls.Image 添加图像。我想使用内存流或位图中的图像。我该怎么做,看来 WebControls.Image 需要 ImageURL。
我没有静态图像,图像是动态创建的。谢谢。
I have created a custom server control and trying to add an image using System.Web.UI.WebControls.Image. I would like to use an image from memory stream or bitmap. How can I do this, it seems the WebControls.Image requires ImageURL.
I don't have a static image, the image is created on the fly. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有两个选项:
第一个选项是最常见的方法,如果您的图像大小超过几千字节,则强烈推荐该方法。您将实现一个 HttpHandler 来流回生成的图像。在您的页面中,将页面中的
ImageUrl
设置为指向您的处理程序的 url。您可以在图像 url 中包含数据,以指示处理程序如何呈现图像或使用哪些数据。您应该意识到,任何人都可以调用此处理程序,因此如果您的图像包含敏感或个人信息,请确保检查该请求是否获得授权。
第二个选项是将图像作为 base64 数据直接编码到页面中。由于 Base64 编码会将数据大小增大 137%,因此仅适用于小图像。您也不会从标准服务器控件获得任何支持,因此您必须从头开始实现它。
There's two options here:
The first option is the most common method and highly recommended if your image is more than a few kilobytes in size. You'd implement a HttpHandler that streams back the generated image. In your page set the
ImageUrl
in the page to a url pointing to your handler. You could include data in the image url to instruct the handler on how to render the image or what data to use.You should realize though that anyone can call this handler so if your images contain sensitive or personal information, make sure you check if the request is authorized.
The second option is to encode the image as base64 data directly into your page. Since base64 encoding blows up the data size by 137% it's suitable for small images only. You'll also get no support at all from the standard server controls so I you'll have to implement this from scratch.
我认为通过图像控制这是不可能的。您可以做的是实现一个自定义 ashx 处理程序,该处理程序将动态生成图像并将其写入响应流。
然后你可以按照编辑的方式做一些事情
:请参阅以下如何绑定一个 MemoryStream 到 asp:image 控件?
I do not think this is possible from the image control. What you can do is implement a custom ashx handler that would generate the image on the fly and write it to the response stream.
Then you could do something along the lines of
Edit: See following How to bind a MemoryStream to asp:image control?
我相信您最好的选择是将图像构建操作设置为嵌入资源并使用ClientScript.RegisterClientScriptResource将图像注册到页面中
您需要通知程序集该资源通过使用 WebResource 属性存在:
通过嵌入资源,您还有机会以流的形式读取图像内容:
但是如果不使用上面提到的 HttpHandler,您就无法将图像推送到管道中
I believe your best bet is to set the image Build Action to
Embedded Resource
and useClientScript.RegisterClientScriptResource
to register the image into the pageYou need to inform the assembly that the resource exists by using WebResource attribute:
By embedding the resource you'll also have the chance to read image content as an stream :
but there's no way you can push the image into the pipeline without using an HttpHandler as mentioned above