Skip to content

Instantly share code, notes, and snippets.

@IshamMohamed
Created September 25, 2014 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IshamMohamed/855e643f87c55dd69b52 to your computer and use it in GitHub Desktop.
Save IshamMohamed/855e643f87c55dd69b52 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556
namespace Word_Finder
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class ShareResultPage : Page
{
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
DispatcherTimer _timer;
public ShareResultPage()
{
this.InitializeComponent();
_timer = new DispatcherTimer();
_timer.Interval = new TimeSpan(0, 0, 1); // 1 second
_timer.Tick += _timer_Tick;
int x = int.Parse(localSettings.Values["CurrLevel"].ToString());
int y = int.Parse(localSettings.Values["CurrPoints"].ToString());
txtResultSharing.Text = string.Format("I have earned {0} points in {1} levels", y.ToString(), x.ToString());
txtResultSharing1.Text = string.Format("I have earned {0} points in {1} levels", y.ToString(), x.ToString());
ResultGrid.Visibility = Visibility.Visible;
ResultGrid.UpdateLayout();
_timer.Start();
}
private void _timer_Tick(object sender, object e)
{
_timer.Stop();
DrawToFile();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private async Task DrawToFile()
{
Random rnd = new Random();
string[] heroImages = { "ms-appx:///Assets/result/h (1).png",
"ms-appx:///Assets/result/h (2).png",
"ms-appx:///Assets/result/h (3).png",
"ms-appx:///Assets/result/h (4).png",
"ms-appx:///Assets/result/h (5).png",
"ms-appx:///Assets/result/h (6).png",
"ms-appx:///Assets/result/h (7).png",
"ms-appx:///Assets/result/h (8).png",
"ms-appx:///Assets/result/h (9).png",
"ms-appx:///Assets/result/h (10).png",
"ms-appx:///Assets/result/h (11).png",
"ms-appx:///Assets/result/h (12).png",
"ms-appx:///Assets/result/h (13).png",
"ms-appx:///Assets/result/h (14).png",
"ms-appx:///Assets/result/h (15).png",
"ms-appx:///Assets/result/h (16).png",
"ms-appx:///Assets/result/h (17).png",
"ms-appx:///Assets/result/h (18).png",
"ms-appx:///Assets/result/h (19).png",
"ms-appx:///Assets/result/h (20).png",
"ms-appx:///Assets/result/h (21).png",
"ms-appx:///Assets/result/h (22).png",
"ms-appx:///Assets/result/h (23).png" };
string[] backImages = { "ms-appx:///Assets/result/b (1).png",
"ms-appx:///Assets/result/b (2).png",
"ms-appx:///Assets/result/b (3).png",
"ms-appx:///Assets/result/b (5).png",
"ms-appx:///Assets/result/b (6).png" };
var imageBrushX = new ImageBrush
{
ImageSource = new BitmapImage(new Uri(backImages[rnd.Next(5)]))
};
var imageBrushT = new ImageBrush
{
ImageSource = new BitmapImage(new Uri(heroImages[rnd.Next(22)]))
};
SuperHeroName.Background = imageBrushT;
ResultGrid.Background = imageBrushX;
RenderTargetBitmap renderTargetBitmapZ = new RenderTargetBitmap();
await renderTargetBitmapZ.RenderAsync(ResultGrid);
var pixels = await renderTargetBitmapZ.GetPixelsAsync();
var file = await KnownFolders.PicturesLibrary.CreateFileAsync("picresult.jpg", CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await
BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
byte[] bytes = pixels.ToArray();
encoder.SetPixelData(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)renderTargetBitmapZ.PixelWidth,
(uint)renderTargetBitmapZ.PixelHeight,
96, 96, bytes);
await encoder.FlushAsync();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment