guantalamo56
guantalamo56
Reputation Top 5%
guantalamo56
3 Snippets  (193rd place)
Published
2 Channels
Created
1 Channel
Following
81 points  (329th place)
Reputation
Junior Code Generator
Junior Publisher
Junior Topic Creator
Junior Topic Hub
Junior Trend Maker
Junior Autobiographer
Serious Autobiographer

Recent Snippets See all snippets by guantalamo56

public by guantalamo56  86  0  4  -2

UWP: Get an image from local resources

Build an Image object from local resources. The image should be stored as content in the Assets folder of the project.
public static Image ImageFromName(this string imageName)
{
	if (imageName == null)
		return null;
	if (!imageName.StartsWith("ms-appx:///Assets/"))
		imageName = "ms-appx:///Assets/" + imageName;
	return new Image
	{
		Source = new BitmapImage(new Uri(imageName, UriKind.RelativeOrAbsolute))
	};
}				
;

public by guantalamo56  88  1  4  0

UWP: retrieve the binary content of a file, given its URL

This is meant to be used with images, but can be applied to any kind of binary content...
public static async Task<byte[]> ToByteArrayImage(this Uri imageUri)
{
	RandomAccessStreamReference streamRef = RandomAccessStreamReference.CreateFromUri(imageUri);
	IRandomAccessStreamWithContentType streamWithContent = await streamRef.OpenReadAsync();
	byte[] buffer = new byte[streamWithContent.Size];
	await streamWithContent.ReadAsync(buffer.AsBuffer(), (uint)streamWithContent.Size, InputStreamOptions.None);
	return buffer;
}				
;

public by guantalamo56  90  0  4  0

UWP: turn byte array into image

Given a byte array, let's turn it into an Image that can be used in UWP UI contexts.
public static async Task<Image> ToImage(this byte[] input)
{
	if (input == null)
		return null;
	BitmapImage biSource = new BitmapImage();
	using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
	{
		await stream.WriteAsync(input.AsBuffer());
		stream.Seek(0);
		await biSource.SetSourceAsync(stream);
	}

	return new Image { Source = biSource };
}					
;