ImageSourceBlob - lucyberryhub/WPF.Tutorial GitHub Wiki

πŸ’ Lucy Berry’s Super Sweet Guide: How to Store and Load Images in WPF Like a Pro! πŸ’

Hey, my lovely little berries! πŸ“πŸ’•
Today, we are going to learn how to turn images into Blob data and then bring them back to life as adorable, colorful pictures in WPF! 🌸✨

You might have an Image Control (ImagePreview02) in WPF and want to store the image in a database as a Blob (a big chunk of binary data 🍫). But…uh oh! 😡 You got an error like this:

🚨 System.ArgumentException: 'Object of type 'System.String' cannot be converted to type 'Fw.Data.Blob'.'

No worries, my berry besties! πŸ’ Let’s break it down step by step in a super simple, cute, and fun way! πŸ’–


πŸ“ Step 1: Understanding the Magical Image Flow! ✨

Before we jump into coding, let’s visualize how an image travels through our program! πŸƒβ€β™€οΈπŸŽ¨

πŸ“Έ Step 1: User selects or loads an image into ImagePreview02.
πŸ”„ Step 2: We convert it into a byte array (Blob) and store it safely in a database.
πŸ“₯ Step 3: When we need it again, we retrieve it from the database and turn it back into a displayable image! πŸ’•

πŸ‘€ Here’s a cute little diagram! πŸŽ€

[Image in UI] β†’ πŸŽ€ Convert to Bytes (Blob) πŸŽ€ β†’ [Stored in Database]
[Retrieve from Database] β†’ πŸŽ€ Convert Back to Image πŸŽ€ β†’ [Image in UI]

Awww! Now our image can go on a cute little adventure into the database and back! 🎠🌈✨


πŸ’ Step 2: Convert ImageSource to Blob Data (byte[])

Now, let’s create a magical method to turn our pretty images into a Blob before we store them! 🍩

πŸŽ€ CherryBerryImageConverter.cs πŸ’

using System;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;

public class CherryBerryImageConverter πŸ’
{
    public static byte[] ConvertImageToBlob(ImageSource berryImage) πŸ“
    {
        if (berryImage == null) return null; // If no image, return nothing 😒

        BitmapSource berryBitmap = berryImage as BitmapSource;
        if (berryBitmap == null) return null;

        using (MemoryStream cherryStream = new MemoryStream()) πŸ’
        {
            BitmapEncoder berryEncoder = new PngBitmapEncoder(); // We use PNG, but you can use JPEG too!
            berryEncoder.Frames.Add(BitmapFrame.Create(berryBitmap));
            berryEncoder.Save(cherryStream);
            return cherryStream.ToArray(); // Convert it into a byte array πŸ“
        }
    }
}

πŸ“ What’s Happening Here?

✨ We take the ImageSource (berryImage) and turn it into a BitmapSource.
✨ We save it into a MemoryStream as a PNG file.
✨ Then, we convert it to a byte array (byte[]), which is a Blob! πŸ’


πŸ“ Step 3: Store the Blob in Our Cute Little Database! πŸŽ€

We need a cute and cozy home for our image Blob in the database! 🏑

πŸŽ€ CherryBerryImageModel.cs πŸ’

using Fw.Data;

public class CherryBerryImageModel πŸ’
{
    public int Id { get; set; } // Unique ID for each cherry berry picture! πŸ“
    public Blob BerryBlob { get; set; } // Our magical image data! ✨
}

πŸŽ€ CherryBerryDatabaseHelper.cs πŸ’

public class CherryBerryDatabaseHelper πŸ’
{
    public void SaveBerryImageToDatabase(byte[] berryBlob) πŸ“
    {
        if (berryBlob == null) return;

        var berryData = new CherryBerryImageModel
        {
            BerryBlob = new Blob(berryBlob) // Store the berryBlob safely πŸ“
        };

        using (var dbContext = new CherryBerryDbContext()) πŸ’
        {
            dbContext.CherryBerryImages.Add(berryData);
            dbContext.SaveChanges(); // Save the berry to the database! πŸ’βœ¨
        }
    }
}

πŸ“ What’s Happening Here?

✨ We create a model (CherryBerryImageModel) to store images in the database!
✨ We save the byte array (berryBlob) into a Blob field in the database!


πŸ’ Step 4: Retrieve the Blob and Show the Image Again!

Now that our image is safely stored, let’s bring it back to life and display it in ImagePreview02! πŸ“πŸŽ€

πŸŽ€ Convert Blob Back to Image

public static ImageSource ConvertBlobToImage(byte[] berryBlob) πŸ“
{
    if (berryBlob == null || berryBlob.Length == 0) return null;

    using (MemoryStream berryStream = new MemoryStream(berryBlob)) πŸ’
    {
        BitmapImage berryImage = new BitmapImage();
        berryImage.BeginInit();
        berryImage.StreamSource = berryStream;
        berryImage.CacheOption = BitmapCacheOption.OnLoad;
        berryImage.EndInit();
        return berryImage; // Return a beautiful image again! πŸŽ€
    }
}

πŸŽ€ Display Image in ImagePreview02

byte[] berryBlobFromDb = myDatabaseHelper.GetBerryBlobFromDatabase();
ImagePreview02.Source = ConvertBlobToImage(berryBlobFromDb);

πŸ“ ✨ Final Summary: The Super Sweet Image Flow! πŸ’

βœ” We turned an Image into a Blob (byte[]) using ConvertImageToBlob! πŸ“
βœ” We stored the Blob in a database with SaveBerryImageToDatabase! πŸ’
βœ” We retrieved the Blob from the database and converted it back into an Image! πŸŽ€
βœ” We displayed the Image back in ImagePreview02 with ConvertBlobToImage! πŸ“βœ¨


πŸŽ€ Lucy Berry’s Final Sweet Words! πŸ’

✨ Great job, my cute little coders! πŸŽ€ You now know how to store and retrieve images as Blobs in WPF like a real Cherry Berry Developer! πŸ“

πŸ’– Now go ahead and add some adorable images to your WPF app! 🎨✨ Let me know if you have any berry cute questions! πŸ’πŸ’Œ