C# 如何在WindowsPhone8.1C上将位图图像插入sqlite数据库#

C# 如何在WindowsPhone8.1C上将位图图像插入sqlite数据库#,c#,windows-phone-8.1,C#,Windows Phone 8.1,我已经设法从手机库中挑选了一张图片并显示出来。这是代码。问题是如何将此图像插入我的Sqlite联系人数据库,并在获取图像后检索并再次显示?这是我的密码。请在此提供详细的分步说明 namespace Mobile_Life.Pages { public sealed partial class NextOFKin_Update_Delete : Page { int Selected_ContactId = 0; DatabaseHelperCl

我已经设法从手机库中挑选了一张图片并显示出来。这是代码。问题是如何将此图像插入我的Sqlite联系人数据库,并在获取图像后检索并再次显示?这是我的密码。请在此提供详细的分步说明

     namespace Mobile_Life.Pages
{

public sealed partial class NextOFKin_Update_Delete : Page
    {
        int Selected_ContactId = 0;
        DatabaseHelperClass Db_Helper = new DatabaseHelperClass();
        Contacts currentcontact = new Contacts();
        CoreApplicationView view;

        public NextOFKin_Update_Delete()
        {
            this.InitializeComponent();
            HardwareButtons.BackPressed += HardwareButtons_BackPressed;
            view = CoreApplication.GetCurrentView();

        }

        private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
        {
            if (Frame.CanGoBack)
            {
                e.Handled = true;
                Frame.GoBack();
            }
        }

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            await StatusBar.GetForCurrentView().ShowAsync();
            Selected_ContactId = int.Parse(e.Parameter.ToString());
            currentcontact = Db_Helper.ReadContact(Selected_ContactId);//Read selected DB contact
            namestxt.Text = currentcontact.Name;//get contact Name
            relationtxt.Text = currentcontact.Relation;//get contact relation
            phonetxt.Text = currentcontact.PhoneNumber;//get contact PhoneNumber

        }

        private void Update_click(object sender, RoutedEventArgs e)
        {
            currentcontact.Name = namestxt.Text;
            currentcontact.Relation = relationtxt.Text;
            currentcontact.PhoneNumber = phonetxt.Text; 
            Db_Helper.UpdateContact(currentcontact);//Update selected DB contact Id
            Frame.Navigate(typeof(NextOfKin));
        }

        private void Delete_click(object sender, RoutedEventArgs e)
        {
            Db_Helper.DeleteContact(Selected_ContactId);//Delete selected DB contact Id.
            Frame.Navigate(typeof(NextOfKin));
        }

        private void profile_img(object sender, TappedRoutedEventArgs e)
        {
            FileOpenPicker filePicker = new FileOpenPicker();
            filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            filePicker.ViewMode = PickerViewMode.Thumbnail;

            // Filter to include a sample subset of file types
            filePicker.FileTypeFilter.Clear();
            filePicker.FileTypeFilter.Add(".bmp");
            filePicker.FileTypeFilter.Add(".png");
            filePicker.FileTypeFilter.Add(".jpeg");
            filePicker.FileTypeFilter.Add(".jpg");

            filePicker.PickSingleFileAndContinue();
            view.Activated += viewActivated;
        }

        private async void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1)
        {
            FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs;

            if (args != null)
            {
                if (args.Files.Count == 0) return;

                view.Activated -= viewActivated;
                StorageFile storageFile = args.Files[0];
                var stream = await storageFile.OpenAsync(FileAccessMode.Read);
                var bitmapImage = new BitmapImage();
                await bitmapImage.SetSourceAsync(stream);

                var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
                profile.ImageSource = bitmapImage;


            }
        }

    }

最好的方法是不要将图像存储在SQLite中。只需将其复制到您的
ApplicationData.Current.local文件夹
,然后保存路径


如果您真的想在SQLite中存储位图图像,只需将其转换为字节数组:

为什么不将手机库中图像的路径保存到SQLite数据库中,而不是尝试与图像本身相同?我们将尝试一下。虽然万一用户从手机上删除图像,它不会受到影响吗?