Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

View image stored in VARBINARY(MAX) column in SQL SERVER 2008 Expand / Collapse
Author
Message
Posted Friday, November 16, 2012 4:31 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, February 11, 2014 2:13 AM
Points: 34, Visits: 175
Hello,

How to select and view the .jpg image stored in VARBINARY(MAX) column in SQL SERVER 2008?

Thanks!
Post #1385882
Posted Friday, November 16, 2012 5:20 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:18 AM
Points: 6,258, Visits: 7,445
Inside of SQL Server? You don't, it doesn't render binaries to native format for you.

You'll need to extract it again back to its native format and then use other software/plugins/the front end that dropped it off to see it.

Is that what you're asking how to do? Extract it back to a .jpg file?



- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1385889
Posted Friday, November 16, 2012 5:27 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, February 11, 2014 2:13 AM
Points: 34, Visits: 175
Exactly, that is what I was asking for.
Can you please explain to me on how exactly to achieve that?
Which tool should I be using?

Thanks!
Post #1385891
Posted Friday, November 16, 2012 8:59 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, December 19, 2013 12:26 AM
Points: 14, Visits: 52
it depends on how the image stored.
if you insert it as byte then you need to retrieve and convert it into bitmap
Post #1385897
Posted Monday, November 26, 2012 1:49 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:18 AM
Points: 6,258, Visits: 7,445
Anon, Holidays ate me for a while, apologies. Did you still need help with this? How did the files get loaded to this in the first place?


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1388832
Posted Monday, November 26, 2012 2:15 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, February 11, 2014 2:13 AM
Points: 34, Visits: 175
Image was inserted into the table using simple UPDATE statement of actual VARBINARY value which I got from another table.
Not sure how the image was inserted into that table to begin with.
There is a free software called SSMSBoost which is an extension for SSMS.
This allows to view the image right from SSMS.

Another way to view the image is to export it from table to file system using bcp command.
Below link explains how to do that:
http://madhuottapalam.blogspot.com/2008/08/creating-files-from-images-stored-in.html

Thanks everybody!
Post #1388846
Posted Monday, November 26, 2012 2:40 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:54 AM
Points: 13,468, Visits: 12,323
I think the important concept here is that you want to view it, that means it is in the front end. It relatively trivial to do this in .NET or any number of other programming languages. The basic gist is that when you select the binary column you are returned a byte array. Then you just need to assemble that byte array into something usable by your presentation layer.

Here is an example of this in C# from some of our web code. In this particular case it is only dealing with images and I have a column that stores the ContentType. In my case I don't bother with the original file name because these are streamed directly to the response and never intended to be saved. You could some sort of method for determining which type of image you may have. If you have the filename stored that would be a good place to start.


DataTable dt = Some method to fill your datatable here

if (dt.Rows.Count > 0)
{
if (dt.Rows[0]["Image"] != DBNull.Value)
{
Bitmap bmp = new Bitmap(new MemoryStream((byte[])(dt.Rows[0]["Image"])));
if (bmp == null)
{
Response.Clear();
Response.StatusCode = 404;
Response.End();
return;
}

Response.ContentType = dt.Rows[0]["ContentType"].ToString();
if (dt.Rows[0]["ContentType"].ToString() == "image/gif")
{
bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
}
else
{
bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
bmp.Dispose();
}
}



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1388855
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse