Reputation Top 5%
3 Snippets  (193rd place)
2 Channels
1 Channel
Sep 25, 2019
Last Visit
Sep 25, 2019
81 points  (328th place)
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 created Sep 25, 2019  32  0  4  0

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 created Sep 25, 2019  37  0  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 created Sep 25, 2019  41  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());
		await biSource.SetSourceAsync(stream);

	return new Image { Source = biSource };