将图像手动存储在SQL Server中的列图像

时间:2016-05-30 16:31:31

标签: c# sql-server vb.net

我没有获得许可 How to store image in SQL Server database tables column(直接从对话框中插入图像),所以我创建了一个用于打开图像的小程序,并将其十六进制代码手动插入到表中,但它不起作用。

我认为它可以是转换,我曾尝试过

    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;
    }

我得到了这段代码:

" 89504E470D0A1A0A0000000D49484452000000100000001008060000001FF3FF610000000467414D410000B18F0BFC6105000000097048597300000B1100000B11017F645F910000002B744558744372656174696F6E2054696D6500446F203134204E6F7620323030322032323A32333A3037202B30313030291FDE570000000774494D4507D3021B00051B52B47A760000024A49444154384FB5CBDB4BD3510007F01FBDD6837F416FBDA4385B399D09C99CAD6933736889DAB6D239947236342F78D960984E1C64B689166E53576DEA26B63615A2594EFC41170AEF97E6E68548BB42606DDF461E82580F06F581C3E17CCFF74BFD57D7A6CEE814E3020D79FE9D8B4F62129D0B03C1FED7C6A0C8151945E2BDBBFA28ED1EEDA331B9EA81D496AC27F1DEE48C441FBCE951ED8C2D3A30BA388C46A7E2A3B02732827C87CB1D89112ADC8276152D1A503D2D702B478BDE8EAED8609FEB836DAE27749B5061116F945BF3DCC5DD197DE734499AB4BA683E9953543B5D1358D8A131B5EDC2E3CD21B8FC16D8970CB0CE77E1FE5C07FA176E6368D500C7BA01CE77064C04ADA83649BF903945E55BE22A06966F05C7B7FBE158EB86CDDB09EBB20E96451D0697BBD031D380E65732B4CDCB61DF6A836EA236C097B36464BE2BC7CC2ABEF3521D18DBEEC5D0BA1E76BF0EC6153564341B39 9E4390D05128996121EF6EDCB7D452F60532FBDDE9668658395E10ECF635A0695602E9B358943C3F0EF90B0E2A66F9281C4C09F08AE233493DDCA9AAE8FD97CCDC60FD5216AAA63351332DFC796A67B2A1F6E643A2E77F0DD5F6EDB6FF8057C9E4D44D48A0F59542E32D46EBDA656843A7E54D096EF8E5A87488919CCD66927AB8D4F2634DBD9F5B60FCD008BDAF1622AD6053A44DDFEADC50C2F8E93A4CEF35883F79A49AD4C34975671D6ED850FFF0CA776E01AB959375F80047C088E0E62674281D6581490C2343CA33937A387E19BB3053916C3F91CF384AA25F98493189E922DE838494D8F324FA1728EA0782CF363D8BC35D1B0000000049454E44AE426082" (使用https://hexed.it/打开图像我有相同的代码)

但是当我在sql server管理中进行插入时,代码会改变!

" 0x383935303445343730443041314130413030303030303044343934383434353230303030303031303030303030303130303830363030303030303146463346463631303030303030303436373431344434313030303042313846304246433631303530303030303030393730343835393733303030303042313130303030304231313031374636343546393130303030303032423734343535383734343337323635363137343639364636453230353436393644363530303434364632303331333432303445364637363230333233303330333232303332333233413332333333413330333732303242333033313330333032393146444535373030303030303037373434393444343530374433303231423030303531423532423437413736303030303032344134393434343135343338344642354342444234424433353130303037463031464244443638333746343136464244413433383542333939443039433939434144363933333733363838394441423644323339393437323336333432463738443936303938344531433634423638393136364535333537364445413236423633363135413235393445464334313137304145463937453645363835343842423432363036444446343631453832353830463036463538314333453137434346463734424644353744 3741364345453831344533303230443739464539443842344636323132394430423033433146454437433641304338313531393435453242444242464132384544314545444133333142394541383144343936414332374631444545343843343431464243453935314544384332443341333042413338384334364137453241334230323733323832374338374342314438393131324144433832373631353244314135303344324437303242343738424445384541454438363039464542383336444145323737343942353036313131364639343542463344434335444431393744453733343439394142344241363833453939353335343342354431333538443841313331423545444332453343443231423846433136443839373043423043453737453146453543303746413137364536333638443530304337424130314345373730363443303441444138333634394246393033393435453535424532324130363936364630354337423746424531353845423836434444423039454242323045393634353144303639374242443033314433383045363537333242344344434236314446364138333645413233364330393742333634363442453242433743433241424546333532314431384442454543354430424131453736424630454336313533353634333431423339394534 3339304430353132383939363132314546364544434237443435324636303533324642444445393636383635383339354531304543463633354130363935363032453942333538393433433346304546393042304532413636463932383143344330394630384145323333343933444443413941414538464439374343444336304644353231364141413633333531333332444643373936413637423241314636453634334132453737463044443546364544423646463830353743394534443434443438413046353935343245333244343645424441363536383433413745353444303936454638453541383734383839313943434436363932374142384434463236333444424439463542363046434430303842444146313632324144363035334134344444464541444335304332463845393341344345463335383833463739413439414434433334393735363731443645443835304646463043413737364530314142393539333735463830303437433038384530453632363734323831443635383134393043323334334341333339333741333837453139424233303533393136433346393143463338344141323546393834393331383945393232444538333834393444384633323446413137323845413037383243463336334438424333354431423030303030303030343934 35344534344145343236303832"

我的想法是使用像

这样的字节数组读取数据
Dim mStream As System.IO.MemoryStream = New System.IO.MemoryStream(abyIcono)
nuevaPbx.Image = Image.FromStream(mStream)

但不起作用,因为阵列不一样! (我接受了错误的参数错误)

0 个答案:

没有答案