locked
PictureBox with Image data store within SQL Server Compact RRS feed

  • Question

  • My project requires to display an image from database. I successful store image file into database using convert image to byte array. But when I use pictureBox with DataBindings features, it seems like the picture does not show up. I did not code anything, just let the wizzard chose the bindingSource and the image field in database. So how do I correct that?
    Tuesday, December 1, 2009 3:10 AM

Answers

  • Hi Nguyen Thai,

    You must be use Image property to display an image.  See below code snippet how to display image in picture box ?

    C#.Net Code


    pictureBox1.Image = LoadPicture();


    private Image LoadPicture()
            {
                string sPath = @"\My Documents\My Pictures\Flower.jpg";
               
                Image imgImage = null;
    
                try
                {
    
                    //Read jpg into file stream, and from there into Byte array.
                    FileStream fsFileStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
    
                    Byte[] byteArray = new Byte[fsFileStream.Length];
    
                    fsFileStream.Read(byteArray, 0, byteArray.Length);
    
                    fsFileStream.Close();
    
                    MemoryStream msMemoryStream = new MemoryStream(byteArray);
    
                    imgImage = new Bitmap(msMemoryStream);
    
                  
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    
                return imgImage;
            }


    Hope this may be help.




    If you find my post is helpful for you then mark as a answer.
    • Marked as answer by Nguyen Thai Tuesday, December 1, 2009 4:13 PM
    Tuesday, December 1, 2009 4:39 AM

All replies

  • I think I can use that way, but what I was talking was I need get the picture from the database and using the dataBindings feature to get this image, not coding anything. When I try debug the application, the Image property of pictureBox cannot view.
    Tuesday, December 1, 2009 4:38 AM
  • Hi Nguyen Thai,

    You must be use Image property to display an image.  See below code snippet how to display image in picture box ?

    C#.Net Code


    pictureBox1.Image = LoadPicture();


    private Image LoadPicture()
            {
                string sPath = @"\My Documents\My Pictures\Flower.jpg";
               
                Image imgImage = null;
    
                try
                {
    
                    //Read jpg into file stream, and from there into Byte array.
                    FileStream fsFileStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
    
                    Byte[] byteArray = new Byte[fsFileStream.Length];
    
                    fsFileStream.Read(byteArray, 0, byteArray.Length);
    
                    fsFileStream.Close();
    
                    MemoryStream msMemoryStream = new MemoryStream(byteArray);
    
                    imgImage = new Bitmap(msMemoryStream);
    
                  
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    
                return imgImage;
            }


    Hope this may be help.




    If you find my post is helpful for you then mark as a answer.
    • Marked as answer by Nguyen Thai Tuesday, December 1, 2009 4:13 PM
    Tuesday, December 1, 2009 4:39 AM