Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted

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.
 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900