0

I don't have permission to do that How to store image in SQL Server database tables column (to insert an image directly from a dialog), so I created a little program for open a image and get his hex code for insert into the table manually but it doesn't work.

I think that it can be a conversion, I had tried with that

    byte[] arrBytes;

    //convert image to bytearray
    public byte[] imgToByteArray(Image img)
    {
        using (MemoryStream mStream = new MemoryStream())
        {
            img.Save(mStream, img.RawFormat);
            return mStream.ToArray();
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            Image imagen = Image.FromFile(openFileDialog1.FileName);
            pictureBox1.Image = imagen;
            arrBytes = imgToByteArray(imagen);         
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        string hex = BitConverter.ToString(arrBytes).Replace("-", string.Empty);
        //string hex = BitConverter.ToString(arrBytes);
        //string hex = Convert.ToBase64String(arrBytes);

        richTextBox1.Text = hex;
    }

And i get this code:

"89504E470D0A1A0A0000000D49484452000000100000001008060000001FF3FF610000000467414D410000B18F0BFC6105000000097048597300000B1100000B11017F645F910000002B744558744372656174696F6E2054696D6500446F203134204E6F7620323030322032323A32333A3037202B30313030291FDE570000000774494D4507D3021B00051B52B47A760000024A49444154384FB5CBDB4BD3510007F01FBDD6837F416FBDA4385B399D09C99CAD6933736889DAB6D239947236342F78D960984E1C64B689166E53576DEA26B63615A2594EFC41170AEF97E6E68548BB42606DDF461E82580F06F581C3E17CCFF74BFD57D7A6CEE814E3020D79FE9D8B4F62129D0B03C1FED7C6A0C8151945E2BDBBFA28ED1EEDA331B9EA81D496AC27F1DEE48C441FBCE951ED8C2D3A30BA388C46A7E2A3B02732827C87CB1D89112ADC8276152D1A503D2D702B478BDE8EAED8609FEB836DAE27749B5061116F945BF3DCC5DD197DE734499AB4BA683E9953543B5D1358D8A131B5EDC2E3CD21B8FC16D8970CB0CE77E1FE5C07FA176E6368D500C7BA01CE77064C04ADA83649BF903945E55BE22A06966F05C7B7FBE158EB86CDDB09EBB20E96451D0697BBD031D380E65732B4CDCB61DF6A836EA236C097B36464BE2BC7CC2ABEF3521D18DBEEC5D0BA1E76BF0EC6153564341B399E4390D05128996121EF6EDCB7D452F60532FBDDE9668658395E10ECF635A0695602E9B358943C3F0EF90B0E2A66F9281C4C09F08AE233493DDCA9AAE8FD97CCDC60FD5216AAA63351332DFC796A67B2A1F6E643A2E77F0DD5F6EDB6FF8057C9E4D44D48A0F59542E32D46EBDA656843A7E54D096EF8E5A87488919CCD66927AB8D4F2634DBD9F5B60FCD008BDAF1622AD6053A44DDFEADC50C2F8E93A4CEF35883F79A49AD4C34975671D6ED850FFF0CA776E01AB959375F80047C088E0E62674281D6581490C2343CA33937A387E19BB3053916C3F91CF384AA25F98493189E922DE838494D8F324FA1728EA0782CF363D8BC35D1B0000000049454E44AE426082" (Open the image with https://hexed.it/ I had the same code)

but when I made the insert in the sql server management, the codes changes!

"0x3839353034453437304430413141304130303030303030443439343834343532303030303030313030303030303031303038303630303030303031464633464636313030303030303034363734313444343130303030423138463042464336313035303030303030303937303438353937333030303030423131303030303042313130313746363435463931303030303030324237343435353837343433373236353631373436393646364532303534363936443635303034343646323033313334323034453646373632303332333033303332323033323332334133323333334133303337323032423330333133303330323931464445353730303030303030373734343934443435303744333032314230303035314235324234374137363030303030323441343934343431353433383446423543424442344244333531303030374630314642444436383337463431364642444134333835423339394430394339394341443639333337333638383944414236443233393934373233363334324637384439363039383445314336344236383931363645353335373644454132364236333631354132353934454643343131373041454639374536453638353438424234323630364444463436314538323538304630364635383143334531374343464637344246443537443741364345453831344533303230443739464539443842344636323132394430423033433146454437433641304338313531393435453242444242464132384544314545444133333142394541383144343936414332374631444545343843343431464243453935314544384332443341333042413338384334364137453241334230323733323832374338374342314438393131324144433832373631353244314135303344324437303242343738424445384541454438363039464542383336444145323737343942353036313131364639343542463344434335444431393744453733343439394142344241363833453939353335343342354431333538443841313331423545444332453343443231423846433136443839373043423043453737453146453543303746413137364536333638443530304337424130314345373730363443303441444138333634394246393033393435453535424532324130363936364630354337423746424531353845423836434444423039454242323045393634353144303639374242443033314433383045363537333242344344434236314446364138333645413233364330393742333634363442453242433743433241424546333532314431384442454543354430424131453736424630454336313533353634333431423339394534333930443035313238393936313231454636454443423744343532463630353332464244444539363638363538333935453130454346363335413036393536303245394233353839343343334630454639304230453241363646393238314334433039463038414532333334393344444341394141453846443937434344433630464435323136414141363333353133333244464337393641363742324131463645363433413245373746304444354636454442364646383035374339453444343444343841304635393534324533324434364542444136353638343341374535344430393645463845354138373438383931394343443636393237414238443446323633344442443946354236304643443030384244414631363232414436303533413434444446454144433530433246384539334134434546333538383346373941343941443443333439373536373144364544383530464646304341373736453031414239353933373546383030343743303838453045363236373432383144363538313439304332333433434133333933374133383745313942423330353339313643334639314346333834414132354639383439333138394539323244453833383439344438463332344641313732384541303738324346333633443842433335443142303030303030303034393435344534344145343236303832"

My idea is to read the data after with an array of bytes like

Dim mStream As System.IO.MemoryStream = New System.IO.MemoryStream(abyIcono)
nuevaPbx.Image = Image.FromStream(mStream)

but doesn't work because the array is not the same! (i take an error of wrong parameter)

Community
  • 1
  • 1
Rubén Aroca
  • 85
  • 1
  • 1
  • 7
  • 2
    You dont have permission to do *what*, exactly? If the source is a file, `FileReadAllBytes` will give you the byte array without using an image or memstream, which can be saved to the db. I guess we need to know what type column you are trying to save to – Ňɏssa Pøngjǣrdenlarp May 30 '16 at 16:47
  • http://stackoverflow.com/questions/3548401/how-to-save-image-in-database-using-c-sharp – mohsen May 30 '16 at 17:36
  • To insert a image from a dialog using "insert into tableName (ImageColumn) SELECT BulkColumn FROM Openrowset( Bulk 'image..Path..here', Single_Blob) as img" The column is a type image – Rubén Aroca May 31 '16 at 06:32
  • 1
    "when I made the insert in the sql server management, the codes changes" - that's because you've put it into SSMS as a *string* literal (surrounded by single quotes) rather than as a *binary* literal (unquoted, start with 0x. Therefore, first, SQL Server has converted the *string* to binary, and what you're seeing is the ASCII representation of each *character* from that string. – Damien_The_Unbeliever May 31 '16 at 08:04
  • You are right Damien! I didn 't see this conversion. Thank you – Rubén Aroca May 31 '16 at 08:19

0 Answers0