Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 17-Jan-13 12:54pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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? Smile | :) )
 
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. Smile | :) 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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 490
1 Maciej Los 299
2 BillWoodruff 174
3 /\jmot 170
4 Suraj Sahoo | Coding Passion 150
0 OriginalGriff 8,484
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,159
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100