Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arctouch-williamrodriguez/404be5b600d594a4d2466c6f3bbd3d0f to your computer and use it in GitHub Desktop.
Save arctouch-williamrodriguez/404be5b600d594a4d2466c6f3bbd3d0f to your computer and use it in GitHub Desktop.
ImageSource From Local Resource
//How to use
var imageAsByteArray = GetImageFromFile("Folder.Imagename.jpg");
var imageToBase64String = Convert.ToBase64String(imageAsByteArray, 0, imageAsByteArray.Length);
//Loading into a Xamarin Forms Image Source
theImage.Source = ImageSource.FromStream(() => new MemoryStream(imageAsByteArray));
private byte[] GetImageFromFile(string fileName)
{
var applicationTypeInfo = Application.Current.GetType().GetTypeInfo();
byte[] buffer = null;
using (var stream = applicationTypeInfo.Assembly.GetManifestResourceStream($"{applicationTypeInfo.Namespace}.{fileName}"))
{
if (stream != null)
{
long length = stream.Length;
buffer = new byte[length];
stream.Read(buffer, 0, (int)length);
}
}
return buffer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment