The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

MS-SQL Image data type

rfaliasrfalias Registered User regular
What is the easiest way to retrieve the data for this field type? I've never encountered the 'Image' or 'Picture' data type before...

Can it be read directly into HTML if I query that field and return it into img tags?

rfalias on

Posts

  • JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited August 2009
    No, you have to query it into a page that serves up as a mime type of the images you plan to deliver.

    Alternatively, don't store binary data in SQL.

    It makes baby Jesus cry.

    Jasconius on
    this is a discord of mostly PA people interested in fighting games: https://discord.gg/DZWa97d5rz

    we also talk about other random shit and clown upon each other
  • rfaliasrfalias Registered User regular
    edited August 2009
    Jasconius wrote: »
    No, you have to query it into a page that serves up as a mime type of the images you plan to deliver.

    Alternatively, don't store binary data in SQL.

    It makes baby Jesus cry.

    Well, it is not by my choice. I'm just a victim of the crime.

    Additionally, is there a way to determine the mime type that field is, or am I going to have to try every extension until I get it?
    I don't know why we have this system in the first place, but our DBA's don't know a goddamn so I'm stuck figuring it out


    Double edit: What is even gayer is that the field on the front end is TEXT.

    rfalias on
  • exoplasmexoplasm Gainfully Employed Near Blizzard HQRegistered User regular
    edited August 2009
    I have some code from a website that has to do this. This is the code that makes the image output in the browser. I don't know what language you are using but maybe this will help give you an idea. Obviously caching is important and I know it is setup somewhere. This is just an application of some code found online for ASP.NET.

    To get the image in the page:
    <img src="/image.aspx?pictureid=123" />
    
    The behind-the-scenes code:
                if (PictureID > 0)
                {
                    Picture picture = PictureManager.GetPictureByID(PictureID);
                    if (picture != null)
                    {
                        using (MemoryStream ImageStream = new MemoryStream(picture.PictureBinary))
                        {
                            using (System.Drawing.Image MyImage = System.Drawing.Image.FromStream(ImageStream))
                            {
                                using (MemoryStream OutputStream = new MemoryStream())
                                {
                                    
    
                                    if (Width > 0 || Height > 0)
                                    {
                                        _GetImageDimensions(ref Width, ref Height, MyImage);
    
                                        Bitmap bmp = new Bitmap(Width, Height);
                                        Graphics g = Graphics.FromImage(bmp);
                                        g.DrawImage(MyImage, 0, 0, bmp.Width, bmp.Height);
                                        bmp.Save(OutputStream, ImageFormat.Gif);
                                    }
                                    else
                                    {
                                        MyImage.Save(OutputStream, ImageFormat.Gif);
                                    }
    
    
                                    //img.Save(ImageStream, ImageFormat.Png );
                                    Response.ContentType = "image/gif";
                                    Response.BinaryWrite(OutputStream.ToArray());
                                    Response.End();
                                }
                            }
                        }
                    }
                }
    

    exoplasm on
    1029386-1.png
    SC2 NA: exoplasm.519 | PA SC2 Mumble Server | My Website | My Stream
  • rfaliasrfalias Registered User regular
    edited August 2009
    Well, I was able to retrieve the field but it ended up not even being binary, ADO.Stream wouldn't write it out as binary, but as soon as I changed it to writetext it was fine. Maybe it's some sort of encrypted text, which is stupid.

    rfalias on
  • potNPanpotNPan Registered User regular
    edited August 2009
    rfalias wrote:
    Additionally, is there a way to determine the mime type that field is, or am I going to have to try every extension until I get it?

    Which version of MS SQL are you using? I'm using 2005 where I work and I can see the data stored in a binary field if I SELECT the field from a query window. The part of the the data near the beginning should give you some clue, for example, I'm looking at a PNG file in a text editor right now and I'm seeing "PNG" appear twice.
    exoplasm wrote: »
    To get the image in the page:
    stuff
    Is all that really necessary though? I don't know if this is the right way to do things, but a few years ago I wrote a PHP page that basically just echo'd out the value from a text type MySQL field and set the header mime type as some image type (can't remember).

    In the case of ASP (I assume, from the mention of ADO) couldn't you just set the response.contenttype and response.write everything out?

    potNPan on
  • JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited August 2009
    potNPan wrote: »
    exoplasm wrote: »
    To get the image in the page:
    stuff
    Is all that really necessary though? I don't know if this is the right way to do things, but a few years ago I wrote a PHP page that basically just echo'd out the value from a text type MySQL field and set the header mime type as some image type (can't remember).

    In the case of ASP (I assume, from the mention of ADO) couldn't you just set the response.contenttype and response.write everything out?

    His code is not as complex as it looks, and he's got an extra if block in there that the OP probably won't need.

    That's the standard way of doing it in .NET. There are some technical reasons as to why you have to jump through all those object hoops, but regardless, it's probably 4 times faster than the equivalent PHP function.

    Probably the #1 reason for using his code is that you can also run an image off the filesystem through that function without any changes to the code, instead of *just* SQL queried binary.

    Jasconius on
    this is a discord of mostly PA people interested in fighting games: https://discord.gg/DZWa97d5rz

    we also talk about other random shit and clown upon each other
  • potNPanpotNPan Registered User regular
    edited August 2009
    Jasconius wrote: »
    potNPan wrote: »
    exoplasm wrote: »
    To get the image in the page:
    stuff
    Is all that really necessary though? I don't know if this is the right way to do things, but a few years ago I wrote a PHP page that basically just echo'd out the value from a text type MySQL field and set the header mime type as some image type (can't remember).

    In the case of ASP (I assume, from the mention of ADO) couldn't you just set the response.contenttype and response.write everything out?

    His code is not as complex as it looks, and he's got an extra if block in there that the OP probably won't need.

    That's the standard way of doing it in .NET. There are some technical reasons as to why you have to jump through all those object hoops, but regardless, it's probably 4 times faster than the equivalent PHP function.

    Probably the #1 reason for using his code is that you can also run an image off the filesystem through that function without any changes to the code, instead of *just* SQL queried binary.

    Ah OK, thanks for that explanation.

    potNPan on
  • rfaliasrfalias Registered User regular
    edited August 2009
    It ended up being some encrypted text in the blob field. Well it wasn't exactly large, so maybe like BSOB.
    When I set the ADO Stream to Binary, it would return no output, as soon as I move to text, it worked fine but was garbage about

    I suppose that's what I get when it's proprietary crap.
    Thanks for the help though!

    For fun here is what it returned!
    湕扡敬琠档牡⁴湯愠瀠瑡敩瑮椠䍓⹍吠捩敫⁴㘲〱㔰ⴠ匠整敶獮湯‬慊敲
    Maybe it says, "Me love you long time"

    rfalias on
Sign In or Register to comment.