30 lines
917 B
C#
30 lines
917 B
C#
namespace Phonebook.Utils;
|
|
|
|
public class ImageHelper
|
|
{
|
|
public static ImageSource Base64ToStreamImageSource(string base64String)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(base64String))
|
|
return null;
|
|
|
|
// Remove data URI prefix if present (e.g., "data:image/png;base64,")
|
|
string cleanBase64 = base64String.Contains(',')
|
|
? base64String.Substring(base64String.IndexOf(',') + 1)
|
|
: base64String;
|
|
|
|
byte[] imageBytes;
|
|
try
|
|
{
|
|
imageBytes = Convert.FromBase64String(cleanBase64);
|
|
}
|
|
catch (FormatException)
|
|
{
|
|
// Invalid Base64 string
|
|
return null;
|
|
}
|
|
|
|
// MAUI calls this delegate each time the image is rendered.
|
|
// Always return a NEW stream; MAUI will dispose it after loading.
|
|
return ImageSource.FromStream(() => new MemoryStream(imageBytes));
|
|
}
|
|
} |