您的位置:首页 > 数据库

图片保存至数据库的方法

2016-04-29 11:40 369 查看
1:为控件绑定图片
BitmapImage bitmapImage;
bitmapImage = new BitmapImage();

bitmapImage.BeginInit();

bitmapImage.StreamSource = System.IO.File.OpenRead(@"E:\2.jpg");

bitmapImage.EndInit();

image.Source = bitmapImage;//image是XAML页面上定义的Image控件
2:把图片保存至imageData数组中并保存至数据库中

byte[] imageData = new byte[bitmapImage.StreamSource.Length];
// now, you have get the image 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(imageData, 0, imageData.Length);
3:从数据库中读取数据保存为字节组,显示在页面上
System.IO.MemoryStream ms = new System.IO.MemoryStream(imageData);//imageData是从数据库中读取出来的字节数组ms.Seek(0, System.IO.SeekOrigin.Begin);

BitmapImage newBitmapImage = new BitmapImage();

newBitmapImage.BeginInit();

newBitmapImage.StreamSource = ms;

newBitmapImage.EndInit();

image2.Source = newBitmapImage;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息