![Conn Conn]()
本文介绍了如何在sql server数据库中设置图像控件上的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想在wpf中设置图片控件上的图片。 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显示图像的宽度和高度但不显示。它显示黑屏并引发异常.. - 元数据''PhotoImage.Source.Metadata''引发了类型''System.NotSupportedException'的异常''System.Windows.Media.ImageMetadata {System.NotSupportedException} 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}推荐答案 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(); }} 这篇关于如何在sql server数据库中设置图像控件上的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 06:36