使用c#将BLOB转换为Jpg。

时间:2020-12-21 21:16:38

I am currently developing a web application that receives data from an on-site database. The database developers have developed some web-services that I am able to call and send/receive data to and from the the database.

我目前正在开发一个从现场数据库接收数据的web应用程序。数据库开发人员开发了一些web服务,我可以调用和发送/接收数据到数据库。

When I want to display an image the method returns the image in BLOB format. My question is: what is the best way to convert BLOB to .jpg or .bmp so I can display the image correctly? If someone could point me in the right direction that would be great!

当我想要显示图像时,该方法以BLOB格式返回图像。我的问题是:将BLOB转换成.jpg或.bmp的最佳方式是什么?如果有人能给我指明正确的方向那就太好了!

Cheers, Tristan

欢呼,特里斯坦

3 个解决方案

#1


3  

You can create a generic handler (ashx file) and have it return the byte array.

您可以创建一个通用的处理程序(ashx文件),并让它返回字节数组。

For example, the url http://www.example.com/GetImage.ashx?imageId=1 could contain code to write the bytes received from the image with id of 1.

例如,url http://www.example.com/GetImage.ashx?imageId=1可以包含代码来编写id为1的图像接收的字节。

You will need to implement the handler code yourself to do the database query, etc. and write the bytes out to the response. You will also need to set the response mime type to jpg, png or whatever format your images are in.

您需要自己实现处理程序代码来执行数据库查询等等,并将字节写入响应。您还需要将响应mime类型设置为jpg、png或任何格式的图像。

Check out this * post for more details: How to bind a MemoryStream to asp:image control?

查看这篇*文章了解更多细节:如何将一个MemoryStream绑定到asp:图像控制?

#2


0  

byte[] array and mimetype are the key words :)

字节[]数组和mimetype是关键字:)

#3


0  

There is no one image format associated with BLOB. It's simply a way of putting raw binary in a database. You need to figure out which format it actually is (libmagic may help), then you can convert if needed.

没有一个图像格式与BLOB关联。它只是将原始二进制文件放入数据库的一种方式。您需要弄清楚它实际上是哪种格式(libmagic可能会有所帮助),然后您可以在需要时进行转换。

#1


3  

You can create a generic handler (ashx file) and have it return the byte array.

您可以创建一个通用的处理程序(ashx文件),并让它返回字节数组。

For example, the url http://www.example.com/GetImage.ashx?imageId=1 could contain code to write the bytes received from the image with id of 1.

例如,url http://www.example.com/GetImage.ashx?imageId=1可以包含代码来编写id为1的图像接收的字节。

You will need to implement the handler code yourself to do the database query, etc. and write the bytes out to the response. You will also need to set the response mime type to jpg, png or whatever format your images are in.

您需要自己实现处理程序代码来执行数据库查询等等,并将字节写入响应。您还需要将响应mime类型设置为jpg、png或任何格式的图像。

Check out this * post for more details: How to bind a MemoryStream to asp:image control?

查看这篇*文章了解更多细节:如何将一个MemoryStream绑定到asp:图像控制?

#2


0  

byte[] array and mimetype are the key words :)

字节[]数组和mimetype是关键字:)

#3


0  

There is no one image format associated with BLOB. It's simply a way of putting raw binary in a database. You need to figure out which format it actually is (libmagic may help), then you can convert if needed.

没有一个图像格式与BLOB关联。它只是将原始二进制文件放入数据库的一种方式。您需要弄清楚它实际上是哪种格式(libmagic可能会有所帮助),然后您可以在需要时进行转换。