working on it ...

Filters

Explore Public Snippets

Sort by

Found 5 snippets

    public by troy modified Sep 25, 2019  40  1  4  1

    How can I get an image from raw binary pixel content?

    Given a raw binary content, this gives me a stream that can be easily turned into an Image
    public static async Task<InMemoryRandomAccessStream> ImageFromBytes(this byte[] imagePixels, int width, int dpi, Rect frame)
    {
    	//BitmapImage bitmap = new BitmapImage();
    	int xStart = (int)frame.X;
    	int yStart = (int)frame.Y;
    	int frameWidth = (int)frame.Width;
    	int frameHeight = (int)frame.Height;
    	byte[] framePixels = new byte[4 * frameWidth * frameHeight];
    	for (int y = 0; y < frameHeight; y++)
    	{
    		for (int x = 0; x < frameWidth; x++)
    		{
    			int frameOffset = (4 * y * frameWidth) + (4 * x);
    			int imageOffset = (4 * (y + yStart) * width) + (4 * (x + xStart));
    			framePixels[frameOffset] = imagePixels[imageOffset];
    			framePixels[frameOffset + 1] = imagePixels[imageOffset + 1];
    			framePixels[frameOffset + 2] = imagePixels[imageOffset + 2];
    			framePixels[frameOffset + 3] = imagePixels[imageOffset + 3];
    		}
    	}
    
    	InMemoryRandomAccessStream bitmapStream = new InMemoryRandomAccessStream();
    	BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, bitmapStream);
    	encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, (uint)frameWidth, (uint)frameHeight, dpi, dpi, framePixels);
    
    	try
    	{
    		await encoder.FlushAsync();
    	}
    	catch
    	{
    		return bitmapStream;
    	}
    
    	return bitmapStream;
    }				

    public by troy modified Sep 25, 2019  154  0  5  0

    What is the size of this file?

    Tells me the size of the given file using the basic properties of UWP StorageFile.
    public static async Task<ulong> GetFileSize(this string filename)
    {
    	StorageFile file = await GetFile(filename);
    	if (file == null)
    		return 0;
    	try
    	{
    		BasicProperties fileSize = await file.GetBasicPropertiesAsync();
    		return fileSize.Size;
    	}
    	catch (Exception e)
    	{
    		s_log.ErrorFormat("Cannot read file {0} due to {1}", filename, e.Message);
    		return 0;
    	}
    }						

    public by guantalamo56 modified Sep 25, 2019  28  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 modified Sep 25, 2019  33  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 modified Sep 25, 2019  36  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 };
    }					

    List Complete ( 5 snippets total )

    • Public Snippets
    • Channels Snippets