View image stored in VARBINARY(MAX) column in SQL SERVER 2008

  • Hello,

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

    Thanks!

  • 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[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • 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!

  • it depends on how the image stored.

    if you insert it as byte then you need to retrieve and convert it into bitmap:-)

  • 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[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • 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!

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply