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! π πβ¨
ImageSource
to Blob Data (byte[])
π Step 2: Convert 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! π
}
}
ImagePreview02
π Display Image in 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! ππ