Click here to Skip to main content
12,244,489 members (40,835 online)
Rate this:
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
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.
Rate this: bad
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:[^],[^].

You will read the blob data from your database as an array of bytes, using ADO.NET data reader. Please see this short tutorial:[^].

The raw data in the form of byte array use used to initialize memory stream System.IO.MemoryStream,[^].

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,

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2016
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