0

How to add uma image to a database in SQLite with Button?

2
  • I'm confused about what you are asking. Could you please details your issue? What "how introduced image" mean? Commented Jul 24, 2017 at 1:53
  • I want the Image to come out in the SqLite Commented Jul 24, 2017 at 12:09

2 Answers 2

1

If you just want to insert images to SQLite, you can convert the image to byte array and save it in a blob column of a SQLite table. SQLite3 can store a blob datatype. Details about how to do you may reference this blog. And you could also encode the image byte array to Base64 string and save it to table as string column. Details for how to encode and decode Base64 please reference this document.

And I recommend you to only save the image paths to the database, not the image itself.

More details please reference this similar thread.

Sign up to request clarification or add additional context in comments.

Comments

1

I do not recommend saving a byte[] in the database. Saving the image directly in the database only makes sense if the database is accessible by other users with other users who do not have a local file of the image. Converting the image data into a byte[], or Base64, is slower than using the file system.

Instead, as Sunteen mentions, save the image to LocalFolder and then you can save a simple string of the file path in the database.

You can easily get the file path from the StorageFile object.

As an example, I've updated your code so that it saves the StorageFile returned from CaptureFileAsync in the app's LocalFolder. Then you can just use that path to set the Image in the UI and save that string to the database item being added.

Using your code, here's the approach:

// a private field on the class level
private string _currentImageFilePath;

private async void btcapture_Click(object sender, RoutedEventArgs e)
{
    var cameraUI = new CameraCaptureUI();
    cameraUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
    cameraUI.PhotoSettings.CroppedSizeInPixels = new Size(150, 150);
    var photo = await cameraUI.CaptureFileAsync(CameraCaptureUIMode.Photo);

    if (photo == null) return;

    // Create a unique filename
    var uniqueFileName = $"{Guid.NewGuid()}.jpg";

    // Save the photo file in the app's Local folder
    var savedFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(uniqueFileName, CreationCollisionOption.GenerateUniqueName);

    // -- Now you have an easy to store string using the file's Path property -- //

    // 1 - Use this string to save to the database
    _currentImageFilePath = savedFile.Path;

    // 2 - You can also use the same file path to set the <Image/> source
    // you do NOT need to decode the image manually
    Capture.Source = new BitmapImage(new Uri(savedFile.Path));
}

private async Task SaveToDatabase()
{
    var selectedDate = Data1.Date.UtcDateTime;

    var Db_Pet = new DataBaseHelper();

    if (txtnombre.Text != "" & txtchip.Text != "" & txtraza.Text != "" & Selectedgenero.Text != "" & txtcolor.Text != "")
    {
        var petToInsert = new Mascota
        {
            Name = txtnombre.Text,
            Chip = txtchip.Text,
            ImageFilePath = _currentImageFilePath
        };

        Db_Pet.Insert(petToInsert);
    }
    else
    {
        await new MessageDialog("Enter your pet").ShowAsync();
    }
}

Note that there are some scenarios where saving the data directly in the database makes sense: if you're using that database where the app doesnt have the image files saved locally (think SQL Server with many users, or restore scenarios)

5 Comments

This Code private task save () does not have anything to do with the Add button
Correct, it isn't meant to be. You'd call that SaveToDatabaseAsync Task later, when the user is ready to save. I wrote it to highlight that you use_currentImageFilePath when it's time to store the record.
If that isn't clear, just move all the code into your Save buttons click event handler, but architecturally speaking it's good to have a Task that does the saving work. Then just call 'await SaveToDatabaseAsync()' in your SaveButton_OnClick
Now part of saving the image as I can show it in SQLite
I'm sorry, I'm having a hard time understanding what you're asking. If you're asking how to show the saved image after it's been saved, use the same approach as after the save: MyImageControl.Source = new BitmapImage(new Uri("FilePathThatWasSavedToDisk"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.