HttpHandler 从字节数组中渲染 png

发布于 2025-01-06 01:45:39 字数 369 浏览 2 评论 0原文

好吧,这应该很简单,但我就是无法让它发挥作用。我有一个字节数组,从 png 文件中读取。我正在尝试编写一个(非常)简单的 HttpHandler 来呈现图像:

context.Response.AddHeader("Content-Type", "image/png")
context.Response.BinaryWrite(bytes)
context.Response.End()

当我在浏览器中打开页面时,我只是得到乱码,

�PNG IHDR���X��sRGB���gAMA���a PHYs���o�d`...

这显然是我做错的标题信息。有什么建议吗?

Okay, this should be simple, but I just can't get it to work. I have an array of bytes, read from a png file. I'm trying to write a (very) simple HttpHandler to render the image:

context.Response.AddHeader("Content-Type", "image/png")
context.Response.BinaryWrite(bytes)
context.Response.End()

When I open the page in a browser, I just get gibberish,

�PNG IHDR���X��sRGB���gAMA�� �a pHYs���o�d` ...

It's obviously something with the header information I'm doing wrong. Any suggestions?

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

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

发布评论

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

评论(1

葬心 2025-01-13 01:45:39

尝试使用 ContentType 属性而不是 AddHeader

context.Response.ContentType = "image/png";
...

另外两个诊断:

  • 如果您在浏览器中使用“保存”并将其保存到 png 文件,是否可以正确渲染?
  • 使用 Wireshark 查看响应中真正返回的内容(具体字节等)

Try using the ContentType property instead of AddHeader:

context.Response.ContentType = "image/png";
...

Two more diagnostics:

  • If you use "save" in the browser and save it to a png file, does that render properly?
  • Use Wireshark to see what's really coming back in the response (which exact bytes etc)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文