UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> C# Retrieve SQL Image Into Picture Box    
 
   
kbrewster
post Jul 31 2013, 10:46 AM
Post#1


UtterAccess VIP
Posts: 4,504
Joined: 18-August 03
From: NH


I am trying to retrieve an SQL image field and show the image in a picture box on a form.
CODE
private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=server;database=db;User ID=ui;Password=pw");
            SqlCommand cmd = new SqlCommand("select a.annotation_blob from document d INNER JOIN ics_annotation a ON d.document_id = a.document_id", con);
            cmd.CommandType = CommandType.Text;
          
            con.Open();
            
            byte[] pic = (byte[])cmd.ExecuteScalar();
nbsp;           MemoryStream ms = new MemoryStream(pic, 0, pic.Length);
            pictureBox1.Image = Image.FromStream(ms,true,false);
            con.Close();
        }

The code is erroring out on pictureBox1.Image = Image.FromStream(ms,true,false); and the error is "Parameter is not valid.". When I step through the code I can see the pic.length is 959, so it is not blank. I have serached high and low for two days and I was unable to find a solution. Hoping someone here has solved this issue in the past.
Thanks!
Go to the top of the page
 
datAdrenaline
post Jul 31 2013, 01:01 PM
Post#2


UtterAccess Editor
Posts: 17,929
Joined: 4-December 03
From: Northern Virginia, USA


Do you know what format the image is stored in? I ask because .FromStream() is expecting the MemoryStream to be in BMP, GIF, JPEG, PNG, or TIFF
Go to the top of the page
 
kbrewster
post Jul 31 2013, 01:55 PM
Post#3


UtterAccess VIP
Posts: 4,504
Joined: 18-August 03
From: NH


I asked the database vendor which format the image was stored in and this is the answer I received...
Does this mean I will not be able to view it in a picture box?
Go to the top of the page
 
datAdrenaline
post Jul 31 2013, 04:26 PM
Post#4


UtterAccess Editor
Posts: 17,929
Joined: 4-December 03
From: Northern Virginia, USA


Not necessarily, but you may have to convert your byte[] arrary into a bitmap then set the .Image property with that bitmap object. I don't have code at the moment (or time to find it ), but its likely easy to find with a search on bing/google. If you can't find something, I will help later --- I just don't have the time.
Go to the top of the page
 
Swatz
post Jul 31 2013, 10:27 PM
Post#5


UtterAccess VIP
Posts: 1,330
Joined: 26-November 02
From: Manitowoc, WI


I have not tried this, but see if it gets you in the right direction: blob
Go to the top of the page
 
kbrewster
post Aug 14 2013, 09:50 AM
Post#6


UtterAccess VIP
Posts: 4,504
Joined: 18-August 03
From: NH


I have been trying quite a few examples I have found but I am still getting an error when I try to set the BLOB to the picturebox. datAdrenaline are you able to locate any code you may be able to help me with?
Go to the top of the page
 
kbrewster
post Aug 14 2013, 03:27 PM
Post#7


UtterAccess VIP
Posts: 4,504
Joined: 18-August 03
From: NH


When I create image fields in SQL I am used to see <Binary data> when I run a select statement on the table. But when I run a select statement on this table I am seeing this...
CODE
0x840000009C5FA4D2BA0202003D0600000100000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000100000005000000A40000000900000030000000200000002
C0200006E0000000000FF00000000000000000000000000000000000000000000000000F3FFFFFF00
0000000000000000000000BC0200000000000100000000417269616C0000000000000000000000000
000000000000000000000000000000000000000000000010000003FF80F0000000000000000000000
00000000000000000000000000000000000000000000000000000000000006000000A40000004F694
16E546578744D0000000000000000000000000000003D000000456C656374726F6E6963616C6C7920
5369676E65642042793A2048617264696E672C204B72697374656E20382F31342F3230313320343A3
03520504D0006000000A40000004F69496E6465780002000000310006000000A40000004F6947726F
7570000A0000005369676E61747572650005000000A40000000A000000EB00000076000000EA00000
07500000000FFFF000000FF000000000000000000010000000000000000000000F5FFFFFF00000000
0000000000000000900100000000000100000000417269616C0000000000000000000000000000000
000000000000000000000000000000000000000010000003FF80F0000000000000000000000000000
00000000000000000000000000000000000000000000000000000006000000A40000004F69416E546
5787411000000000000000000000000000000010000000006000000A40000004F69496E6465780002
000000300006000000A40000004F6947726F7570000A0000005369676E61747572650005000000A40
000000600000066000000D40000003F0100001A0100000000FF000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000010000
003FF80F0000000000000000000000000000000000000000000000000000000000000000000000000
00000000006000000A40000004F69496E6465780002000000320006000000A40000004F6947726F75
70000A0000005369676E61747572650005000000A400000003020000B80000007E0000003C0100007
D00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000010000003FF80F000000000000000000000000000000
000000000000000000000000000000000000000000000000000006000000A40000004F69416E54657
87482000000000000000000000000000000720000007B5C727466315C616E73695C616E7369637067
313235325C64656666305C6465666C616E67313033337B5C666F6E7474626C7B5C66305C666E696C5
C66636861727365743020417269616C3B7D7D0D0A5C766965776B696E64345C7563315C706172645C
667332335C7061720D0A7D0D0A0006000000A40000004F69496E6465780002000000330006000000A
40000004F6947726F7570000A0000005369676E61747572650005000000A400000007000000B60000
007E00000045010000920000000000FF0000000000000000000000000000000000000000000000000
0F5FFFFFF000000000000000000000000900100000000000100000000417269616C00000000000000
00000000000000000000000000000000000000000000000000000000010000003FF80F00000000000
0000000000000000000000000000000000000000000000000000000000000000000000006000000A4
0000004F69416E5465787421000000000000000000000000000000110000007465787420676F65732
06865726521210006000000A40000004F69496E6465780002000000340006000000A40000004F6947
726F7570000A0000005369676E617475726500

I have never seen anything like this before so I am not sure if I can convert this to an actual image in c#.
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    10th December 2017 - 11:13 PM