Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I want to set image on picture control in wpf.
 

private void button7_Click(object sender, RoutedEventArgs e)
        {
            byte[] bitmapimage;
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
            conn.ConnectionString = @"Data Source=SUMIT\SQLEXPRESS;Initial Catalog=fingerdatabase;Integrated Security=True;Pooling=False";
            System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
            cmd.Connection = conn;
            cmd.CommandText = "SELECT * FROM Table1";
            conn.Open();
            string name;
            System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
name = dataRow[0].ToString();
                 bitmapimage = dataRow[1] as byte[];
                   ImageSource bi = ByteToBitmapSource(bitmapimage);
 
                   PhotoImage.Source = bi;
}
public ImageSource ByteToBitmapSource(byte[] image)
        {
           
           MemoryStream strm = new MemoryStream();
 

 
            strm.Write(image, 0, image.Length);
            strm.Position = 0;
            System.Drawing.Image img = System.Drawing.Image.FromStream(strm);
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            ms.Seek(0, SeekOrigin.Begin);
            bi.StreamSource = ms;
            bi.EndInit();
            ImageSource imgSrc=bi as ImageSource;
            return imgSrc;
          
        }
photoImage.Sourse show width and height of image but does not displayed.It display black screen and it throw a exception..
- Metadata 'PhotoImage.Source.Metadata' threw an exception of type 'System.NotSupportedException' System.Windows.Media.ImageMetadata {System.NotSupportedException}
Posted 2-Apr-13 22:53pm
Edited 3-Apr-13 19:47pm
v4
Comments
RedDK at 3-Apr-13 11:35am
   
Just off hand I'd say there might be a problem in yyour SQL clause where you select "*" (whatever). If there was an image stored as type ... something ..., here in a table, it would be a binary or ntext possibly? Question: can you access this "Table1" through some means ... VS2008?
Member 7909353 at 4-Apr-13 0:48am
   
yes i can get Table1 because i can get name = dataRow[0].ToString();
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Member 7909353 at 3-Apr-13 4:43am
   
control gets black screen.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Actually I was inserting the empty byte array
 
Now insert code is
Bitmap OriginalImage = new Bitmap(LiveImage.Image, LiveImage.Width, LiveImage.Height);
                Bitmap _img = new Bitmap(cropWidth, cropHeight);
                Graphics g = Graphics.FromImage(_img);
                g.DrawImage(OriginalImage, 0, 0, rect, GraphicsUnit.Pixel);   
                Img = _img;
                BitmapSource ass = BitmapSourceFromImage(Img);
                PhotoImage.Source = ass;
                byte[] bitmapimage = ImageToByte(Img);
  System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
                conn.ConnectionString = @"Data Source=SUMIT\SQLEXPRESS;Initial Catalog=fingerdatabase;Integrated Security=True;Pooling=False";
                System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "INSERT INTO Table1 VALUES(@fingername,@image)";
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@fingername", "imran"));
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@image", bitmapimage));
                conn.Open();
                int rows = cmd.ExecuteNonQuery();
 
                conn.Close();
 

 private void button7_Click(object sender, RoutedEventArgs e)
        {
 System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
            try
            {
                byte[] bitmapimage;
 
                conn.ConnectionString = @"Data Source=SUMIT\SQLEXPRESS;Initial Catalog=fingerdatabase;Integrated Security=True;Pooling=False";
 
                System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
 
                cmd.Connection = conn;
 
                cmd.CommandText = "SELECT * FROM Table1";
 
              
                conn.Open();
                string name;
                SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Table1", conn);
                System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
 
                    dr.Close();
                    DataSet ds = new DataSet();
                    conn.Close();
                    ad.Fill(ds);
                    foreach (DataRow dataRow in ds.Tables[0].Rows)
                    {
                        name = dataRow[0].ToString();
                        bitmapimage = dataRow[1] as byte[];
                     MemoryStream ms = new MemoryStream(bitmapimage);
                    Image i= Image.FromStream(ms);
                         BitmapSource ass = BitmapSourceFromImage(i);
                         PhotoImage.Source = ass;
               
                }
 
                else
                {
                    System.Windows.MessageBox.Show("Haven't find the id in sql server");
                }
            }
            catch (NotSupportedException ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }
            finally
            {
            
            conn.Close();
            }
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 504
1 Maciej Los 349
2 Kornfeld Eliyahu Peter 325
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,303
1 DamithSL 4,764
2 Maciej Los 4,306
3 Kornfeld Eliyahu Peter 3,914
4 Sergey Alexandrovich Kryukov 3,538


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 4 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100