簡體   English   中英

如何在Windows Phone 8.1 C#中將位圖圖像插入sqlite數據庫

[英]How to Insert bitmap image to sqlite database on Windows phone 8.1 c#

我設法從電話資料庫中選取一張圖片並顯示出來。 這是代碼。 問題是如何將該圖像插入到我的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.LocalFolder並保存路徑。

如果您真的想在SQLite中存儲BitmapImage,只需將其轉換為字節數組: 將bitmapimage轉換為byte []數組以存儲在sqlite數據庫中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM