在本地数据库中保存大图片

时间:2014-08-03 10:11:17

标签: c# database image windows-phone-7 windows-phone-8

我可以在本地数据库中保存地址簿中联系人的图像。但是当我在Visual Studio中的Windows Phone模拟器中用相机拍照时,我可以做到这一点。当我向不同相册的联系人添加照片时,我的应用程序无法启动。这是我在数据库中添加图片的方法。但它适用于小尺寸照片。如何为所有尺寸的照片做到这一点?

foreach (var result in e.Results)
{
    using (ContactsListDataContext db = new ContactsListDataContext(DBConnectionstring))
    {
        var stream = result.GetPicture();

        if (stream != null)
        {
            BitmapImage _newPhoto = new BitmapImage();
            _newPhoto.SetSource(stream);

            MemoryStream _memoryStream = new MemoryStream();
            WriteableBitmap _writingNewPhoto = new WriteableBitmap(_newPhoto);
            _writingNewPhoto.SaveJpeg(_memoryStream, _writingNewPhoto.PixelWidth, _writingNewPhoto.PixelHeight, 0, 95);

            byte[] _arrayForDB = _memoryStream.ToArray();
            _memoryStream.Close();

            var _newEmail = result.EmailAddresses.FirstOrDefault().EmailAddress;
            var _newName = result.DisplayName;
            db._contacts.InsertOnSubmit(new MyContactsList { Name = _newName, Email = _newEmail, ItemImage = _arrayForDB });

            _listContactData.Add(new ContactsModel()
                        {
                            ContactName = result.DisplayName,
                            ContactEmail = result.EmailAddresses.FirstOrDefault().EmailAddress,
                            ContactImage = ByteArraytoBitmap(_arrayForDB)
                        });
            db.SubmitChanges();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以将图片作为BLOB直接插入数据库,最多允许65535个字节。如果您需要更多考虑使用MEDIUMBLOB用于16777215字节或LONGBLOB用于4294967295字节。有关详细信息,请参阅 Storage Requirements for String Types