且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在sql server数据库中设置图像控件上的图像

更新时间:2023-02-08 16:17:30

您好b $ b下面的文章可能会解决您的问题。



http://***.com/questions/9564174/convert-byte-array-to-image-in-wpf [ ^ ]
Hi Below article might solve your problem.

http://***.com/questions/9564174/convert-byte-array-to-image-in-wpf[^]


其实我是插入空字节数组



现在插入代码是

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