Results 1 to 9 of 9

Thread: Load Image File [Resolved]

  1. #1

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Load Image File

    Ok I'll try your sug's. Thank you jm.

  4. #4

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    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());

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    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.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    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.

  9. #9
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    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
  •  



Click Here to Expand Forum to Full Width