ASP.NET字节到图像文件 [英] ASP.NET Byte to Image File

查看:59
本文介绍了ASP.NET字节到图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL Server 2005数据库中有许多图像文件,这些文件当前正在转换为网站上的图像。我们希望用它们相应的图像文件名(字符串)替换所有当前图像字节类型,这样我们就可以消除大量数据库中所有这些图像。更不用说正在删除Image类型。有没有办法在我们选择的目录中创建一个实际的图像文件?

I have a number of Image files in a SQL Server 2005 database which are currently being converted into the images on a website. We want to replace all of the current image byte types with their corresponding image file names (string) so we can eliminate the bulk of holding all of these images in the database. Not to mention that the Image type is being removed. Is there a way to create an actual image file in a directory of our choosing from the bytes?

推荐答案

除非有一些SQL函数我不知道,这很可能,你将不得不编写一个自定义应用程序来为你做这件事。最简单的形式是一个控制台应用程序,它具有所需的保存目录和硬编码的连接字符串。然后循环遍历表中的所有记录,并使用您想要的文件名逻辑将文件保存到所需的目录。



我个人会添加一个新的将要保存文件名的表格的列,然后在应用程序保存图像后,我将使用该值更新记录,这样您就不会得到一堆具有正确名称但没有链接的图像。来自记录的实际文件名。



这可能不是最佳解决方案,但这是我将采取的方法。
Unless there is some SQL function i''m not aware of, which is more than likely, you are going to have to write a custom app to do that for you. The simplest form would be a console app that has the desired save directory and connection strings hard coded. Then just loop through all the records in your table and save the files to the desired directory using whatever logic you want for your file names.

Personally i would add a new column to the table that is going to hold the file names and then after the app saves the image i would update the record with that value so you don''t end up with a bunch of images with the proper name but no link to the actual file name from the record.

This might not be the best solution but it is the approach i would take.


关于图像类型的删除数据,我们有一个非常乐观的谚语:当你肾功能衰竭时开始饮用矿泉水为时已晚。你以前去过哪里? (让我猜一下:不是在这家公司?:-))



我担心有人能够很好地记住图像类型可能有什么可能存储在过去,试图查看第一个字节来找出图像类型,并使用最有效的反复试验方法来恢复这种骶骨知识。在某种程度上,您可以通过编写一些方法来自动完成这项复杂的工作,这些方法将按顺序尝试将图像加载为不同的格式,直到不失败为止。 :-)但是,更严肃的说明:如果您只是完整地存储了数据库中某些图像文件的完整内容,则文件的开头包含元数据,有助于正确加载它。



从技术上讲,这很简单:你可以使用 System.Drawing.Bitmap 类并创建实例使用带有 Stream 参数的构造函数:

http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/z7ha67kw.aspx [ ^ ]。



您将从数据库中读取blob数据作为数组o f字节,使用ADO.NET 数据读取器。请参阅这个简短的教程:

http://www.akadia.com/services/dotnet_read_write_blob。 html [ ^ ]。



以字节数组形式使用的原始数据用于初始化内存流 System.IO.MemoryStream http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx [ ^ ]。



这篇非常简短的CodeProject文章展示了它的示例代码: C#Image to Byte Array和Byte Array to Image Converter Class [ ^ ]。



祝你好运,

-SA
As to the removed data on the image types, we have a very optimistic proverb: "It''s too late to start drinking mineral water when you have a kidney failure". Where have you been before? (Let me guess: not in this company? :-))

I''m afraid that someone will have a nice comfortable job of remembering what image type could have been possibly stored in the past, trying to look at the first byte to figure out the image types and using the most effective trial-and-error method of restoring of this sacral knowledge. To some extend, you can automate this intricate work by writing some method which would sequentially try to load the image as different formats until if does not fail. :-) But, on more serious note: if you just stored the full content of some image files in the database in full, the beginning of the file contains metadata helping to load it properly.

Technically, it''s pretty simple thing: you can use the System.Drawing.Bitmap class and create the instance with the constructor with a Stream argument:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx[^],
http://msdn.microsoft.com/en-us/library/z7ha67kw.aspx[^].

You will read the blob data from your database as an array of bytes, using ADO.NET data reader. Please see this short tutorial:
http://www.akadia.com/services/dotnet_read_write_blob.html[^].

The raw data in the form of byte array use used to initialize memory stream System.IO.MemoryStream, http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx[^].

This really short CodeProject article shows the sample code for it: C# Image to Byte Array and Byte Array to Image Converter Class[^].

Good luck,
—SA


这篇关于ASP.NET字节到图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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