|
-
Jan 28th, 2006, 02:11 AM
#1
Thread Starter
Frenzied Member
Load Image File [Resolved]
Hi guys good day. I have this problem about loading an image that was saved from the database(convert the image file to byte then saved). When I was going to load it(convert the value to byte then use the memorystream class to convert the byte file to image file) but it throws an exception "Input string was not in a correct format".
Am I missing something here?
Here's the code:
Code:
private void button1_Click(object sender, System.EventArgs e)
{
getimage();
}
//method to get the binary value in the database
void getimage()
{
cn.Open();
SqlCommand cm=new SqlCommand("select * from sample",cn);
byte[] img={byte.Parse(cm.ExecuteScalar().ToString())};<---line that throws an error.
cn.Close();
loadimage(img);
}
//method to load the image
Image ximage;
void loadimage(byte[] img)
{
System.IO.MemoryStream im=new System.IO.MemoryStream(img);
im.Flush();
ximage=Image.FromStream(im);
im.Close();
this.pictureBox1.Image=ximage;
}
Last edited by mar_zim; Jan 29th, 2006 at 11:17 PM.
-
Jan 28th, 2006, 03:39 AM
#2
Re: Load Image File
You've misunderstood what Byte.Parse does. It attempts to convert a string representation of the numbers from 0 to 255 into their 8-bit numerical equivalent. It is exactly the same as Integer.Parse except it produces an 8-bit number instead of a 32-bit number. What you want, I'm assuming, is the Bytes that represent the characters in your string. To do that you would use the GetBytes method of an Encoding object.
-
Jan 28th, 2006, 03:59 AM
#3
Thread Starter
Frenzied Member
Re: Load Image File
Ok I'll try your sug's. Thank you jm.
-
Jan 28th, 2006, 04:17 AM
#4
Thread Starter
Frenzied Member
Re: Load Image File
Hi Jm it still throw an error says "Invalid parameter used". I've try all the methods of encoding object but still no luck. 
Code:
byte[] img=System.Text.Encoding.ASCII.GetBytes(cm.ExecuteScalar().ToString());
//no luck
byte[] img=System.Text.Encoding.Unicode.GetBytes(cm.ExecuteScalar().ToString());
-
Jan 28th, 2006, 04:25 AM
#5
Re: Load Image File
Maybe it's because the "string" wasn't saved using that encoding in the first place. I've never saved an image to a database so I'm no expert. You are trying to get the returned value as a String object, but is that the right thing to do? Was it saved as a String object in the first place.
-
Jan 28th, 2006, 04:32 AM
#6
Thread Starter
Frenzied Member
Re: Load Image File
I convert the image file to bytes before saving it to the database.
Here's the code:
Code:
private void btnsave_Click(object sender, System.EventArgs e)
{
image(this.pictureBox1.Image);
cn.Open();
SqlCommand cm=new SqlCommand("insert into sample values('+ximg+')",cn);
cm.ExecuteNonQuery();
cn.Close();
MessageBox.Show("Saved");
}
byte[] ximg;
void image(Image img)
{
System.IO.MemoryStream im=new System.IO.MemoryStream();
img.Save(im,System.Drawing.Imaging.ImageFormat.Jpeg);
im.Flush();
ximg=im.GetBuffer();
}
Actually it's the code of Aaron Young originally written in vb and i tried to convert it in c# but it wont run.
-
Jan 28th, 2006, 04:48 AM
#7
Re: Load Image File
So you're actually saving a byte array into the database so presumably you will be getting a byte array back again. You could try casting the return value of ExecuteScalar stright to a byte array. If that doesn't work you could use a DataReader and call its GetBytes method.
-
Jan 29th, 2006, 11:16 PM
#8
Thread Starter
Frenzied Member
Re: Load Image File
Sorry JM for the late reply. Actually I've already give up.
It will make my application crawl like a baby when saving/retrieving BLOBs. I just saved the path of the image then retrieve it pass to the image object then put in a picturebox.
-
Jan 29th, 2006, 11:46 PM
#9
Fanatic Member
Re: Load Image File [Resolved]
Btw people, how do I upload files on another unit? Say I know the IP address. What's the prerequisite? Right now, I'm saving the byte[] to the db. Sounds not a good idea. So anyone has good links about this one? Or any code examples if you may share? Thanks a bunch.
Edit: Or do I need to post another topic for this one? Sorry for bothers.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|