1:为控件绑定图片
BitmapImage bitmapImage; bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = System.IO.File.OpenRead(@"E:\2.jpg"); bitmapImage.EndInit(); p_w_picpath.Source = bitmapImage;//p_w_picpath是XAML页面上定义的Image控件
2:把图片保存至p_w_picpathData数组中并保存至数据库中
byte[] p_w_picpathData = new byte[bitmapImage.StreamSource.Length]; // now, you have get the p_w_picpath bytes array, and you can store it to SQl Server bitmapImage.StreamSource.Seek(0, System.IO.SeekOrigin.Begin); //very important, it should be set to the start of the streambitmapImage.StreamSource.Read(p_w_picpathData, 0, p_w_picpathData.Length); 3:从数据库中读取数据保存为字节组,显示在页面上 System.IO.MemoryStream ms = new System.IO.MemoryStream(p_w_picpathData);//p_w_picpathData是从数据库中读取出来的字节数组ms.Seek(0, System.IO.SeekOrigin.Begin); BitmapImage newBitmapImage = new BitmapImage(); newBitmapImage.BeginInit(); newBitmapImage.StreamSource = ms; newBitmapImage.EndInit(); p_w_picpath2.Source = newBitmapImage;
转载于:https://blog.51cto.com/lixinya/1768947