byte[] from datarow raise exception

  • Thread starter Thread starter sonvu
  • Start date Start date
S

sonvu

Hello,

I want to read image (string) from XML file to picture box
Here my code:


// Listbox doube click event
private void lbImages_DoubleClick(object sender, System.EventArgs e)
{
string currentItem;

currentItem = lbImages.Text;
if (currentItem == null) return;
string expr = "name = '" + currentItem + "'";
DataRow dr = m_DataSet.Tables["data"].Select(expr)[0];

byte[] img = (byte[])(dr["value"]); ///// ---> Error here

System.IO.MemoryStream ms = new System.IO.MemoryStream();
int offset = 78;
ms.Write(img, offset, img.Length - offset);
Bitmap bm = new Bitmap(ms);
ms.Close();
frmImageViewer viewer = new frmImageViewer(bm);
viewer.Show();
}

Compiled find but when runtime, exception raised ""Specified cast is not valid".
Please help me

Thanks,

sonvu
 
sonvu said:
I want to read image (string) from XML file to picture box
Here my code:


// Listbox doube click event
private void lbImages_DoubleClick(object sender, System.EventArgs e)
{
string currentItem;

currentItem = lbImages.Text;
if (currentItem == null) return;
string expr = "name = '" + currentItem + "'";
DataRow dr = m_DataSet.Tables["data"].Select(expr)[0];

byte[] img = (byte[])(dr["value"]); ///// ---> Error here

System.IO.MemoryStream ms = new System.IO.MemoryStream();
int offset = 78;
ms.Write(img, offset, img.Length - offset);
Bitmap bm = new Bitmap(ms);
ms.Close();
frmImageViewer viewer = new frmImageViewer(bm);
viewer.Show();
}

Compiled find but when runtime, exception raised ""Specified cast is not valid".
Please help me

And what is the type of dr["value"] at runtime? That's the crucial
thing.
 
Jon said:
And what is the type of dr["value"] at runtime? That's the crucial
thing.

Hello Jon,
(Sorry my bad English)
Yeah, I have a resX file and I want to get it's images and display in a picturebox
i.e
<data name="pictureBox1.Image" type="System.Drawing.Bitmap, System.Drawing, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEPERET
FhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/2wBDAQUFBQcGBw4ICA4eFBEUHh4eHh4eHh4eHh4e
Hh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh7/wAARCAEsAZADASIAAhEBAxEB/8QA
.............
.............
.............
</value>

The dr["value"].ToString() = content of value tag

Thanks,

sonvu
 
sonvu said:
Jon said:
And what is the type of dr["value"] at runtime? That's the crucial
thing.

(Sorry my bad English)
Yeah, I have a resX file and I want to get it's images and display in a picturebox
i.e
<data name="pictureBox1.Image" type="System.Drawing.Bitmap, System.Drawing,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEPERET
FhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/2wBDAQUFBQcGBw4ICA4eFBEUHh4eHh4eHh4eHh4e
Hh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh7/wAARCAEsAZADASIAAhEBAxEB/8QA
.............
.............
.............
</value>

The dr["value"].ToString() = content of value tag

So what's the type of dr["value"] itself? What happens if you look at
dr["value"].GetType() in the debugger?
 
Jon said:
So what's the type of dr["value"] itself? What happens if you look at
dr["value"].GetType() in the debugger?

Ah, my bad. I missunderstood your question, dr["value"] is "System.String" type.

Thanks for relply,

sonvu
 
sonvu said:
Jon said:
So what's the type of dr["value"] itself? What happens if you look at
dr["value"].GetType() in the debugger?

Ah, my bad. I missunderstood your question, dr["value"] is
"System.String" type.

Right. In that case, you need to work out how to convert it into a byte
array. It looks like it's base64-encoded data, so use
Convert.FromBase64String.
 
Hello Jon,
Got it, heh he.
My code now as below:
byte[] img = (byte[])(Convert.FromBase64String(dr["value"].ToString()));

Thanks again,

sonvu
 
sonvu said:
Got it, heh he.
My code now as below:
byte[] img = (byte[])(Convert.FromBase64String(dr["value"].ToString()));

You don't need to cast the result of Convert.FromBase64String as it's
declared to return a byte array. However, I would cast the result of
dr["value"] to string rather than calling ToString(), as if the value
isn't a string then it's unlikely it's correct to start with.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top