troy
troy
Reputation Top 5%
troy
2 Snippets  (293rd place)
Published
1 Channel
Created
2 Channels
Following
51 points  (511st place)
Reputation
Junior Code Generator
Junior Publisher
Junior Judge
Junior Popular Coder
Junior Autobiographer
Serious Autobiographer

Recent Snippets See all snippets by troy

public by troy  110  2  5  2

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  370  1  6  1

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;
	}
}						
;