如何让浏览器显示以二进制形式写入客户端的图像? [英] How to make browsers to display image which is written to client in binary form?

查看:35
本文介绍了如何让浏览器显示以二进制形式写入客户端的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ASP 文件中,我以二进制形式读取并输出图像.

In ASP file, I read and output a image in binary form.

我希望浏览器显示图像,而不是下载.

I want browsers to display the image, not download.

我知道标题 Content-Disposition 应该是 inline.

I have known that the header Content-Disposition should be inline.

但我无法确定应该使用什么 Content-Type,因为我无法确定图像的格式.

But I can't determine what Content-Type should be used, because I can't make sure which format the image will be.

我知道 image/png 在 chrome 中适用于 png 和 jpg,但我不确定 image/png 是否适合所有格式的图像时间.

I know that image/png workes for png and jpg in chrome, but I am not sure whether image/png will be fit for all formats of image all the time.

'lngFileLength is the size of image
Response.AddHeader "Content-Length", lngFileLength
Response.ContentType = "image/png"
Response.AddHeader "Content-Disposition", "inline"
'rst1(strImageField).Value is the image in binary form
Response.BinaryWrite rst1(strImageField).Value

感谢您的帮助.

我不认为我的问题与 PHP:二进制图像数据,检查图像类型,因为我的问题是在浏览器中显示图像",而不是检查图像类型".

I don't think my question is duplicate of PHP : binary image data, checking the image type ,because my question is "display image inside browser", not "check image type".

我希望最好的解决方案是我们可以通过设置标题或其他方式告诉浏览器显示而不是下载图像.

The best solution I wish is that we can tell browser to display, not download, image by setting header or other way.

虽然检查图片类型"可以解决这个问题,但它是答案,而不是问题.

Although "check image type" can solve this problem, it is the answer, not the question.

推荐答案

需要根据图片的实际类型设置Content-Type.您不能只是将其设置为任意类型并期望它可靠地工作.

You need to set the Content-Type according to the actual type of the image. You cannot just set it to an arbitrary type and expect it to work reliably.

要从数据库中存储的二进制值中找到图像的实际类型,可以将该值加载到System.Drawing.Image 对象中,然后检查ImageFormat.您可以使用此功能:

To find the actual type of the image from the binary value stored in the database, you can load the value into a System.Drawing.Image object and then check the ImageFormat. You can use this function:

Function GetImageMimeType(ByVal image As System.Drawing.Image)
    Select Case image.RawFormat
        Case System.Drawing.Imaging.ImageFormat.Jpeg
            Return "image/jpg"
        Case System.Drawing.Imaging.ImageFormat.Gif
            Return "image/gif"
        Case System.Drawing.Imaging.ImageFormat.Png
            Return "image/png"
    End Select
End Function

如果您愿意,可以添加其他案例.

You can add other cases if you like.

您也可以像这样直接从图像中获取 MIME 类型:

You can also get the MIME type directly from the image like this:

Function GetImageMimeType(ByVal image As System.Drawing.Image)
   'System.Drawing.Imaging.ImageCodecInfo'
   For Each codec As ImageCodecInfo In ImageCodecInfo.GetImageDecoders()
       If codec.FormatID = image.RawFormat.Guid Then
           Return codec.MimeType
       End If
   Next
   Return "image/unknown"
End Function

但这有点不太可靠,它不适用于动态创建的图像,但适用于从某个源(文件、流、字节数组等)加载的所有图像.

But this is a little less reliable and it won't work for images that were created on the fly, but will work for all images that were loaded from some source (file, stream, byte array, etc...).

如果您不想将二进制文件加载到 System.Drawing.Image 对象中,您可以从前几个字节中找到图像的类型.以下是常见类型标识符的列表:

If you don't want to load your binary into a System.Drawing.Image object, you can find the type of the image from the first few bytes. Here is a list of common type identifiers:

Dim bmp = Encoding.ASCII.GetBytes("BM")
Dim gif = Encoding.ASCII.GetBytes("GIF")
Dim png = New Byte() {137, 80, 78, 71}
Dim tiff = New Byte() {73, 73, 42}
Dim tiff2 = New Byte() {77, 77, 42}
Dim jpeg = New Byte() {255, 216, 255, 224}
Dim jpeg2 = New Byte() {255, 216, 255, 225}

这篇关于如何让浏览器显示以二进制形式写入客户端的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆