/// <summary> /// UI Thrad callback when generating fractal part is done /// Part will be converted to bitmap and added to UI /// </summary> public async Task addFractalPartToUIAsync(FractalPart part) { Windows.UI.Xaml.Controls.Image img = sp.Children[part.getPos()] as Windows.UI.Xaml.Controls.Image; WriteableBitmap w = new WriteableBitmap(part.getWidth(), part.getHeight()); using (Stream stream = w.PixelBuffer.AsStream()) { await stream.WriteAsync(part.imageArray, 0, part.imageArray.Length); } img.Source = w; }
/// <summary> /// Calculate and generate the fractalpart pixels and store in pixels array /// </summary> private async void generateFractalPart(FractalPart part, FractalTemplate.FractalTemplate fractal, Action<FractalPart> callback) { part.imageArray = new byte[part.getWidth() * part.getHeight() * 4]; //Call the calculat fractal method int[,] pixels = fractal.calculate(part); //Generate bitmap from pixeldata int x = 0; int y = 0; for (int i = 0; i < part.imageArray.Length; i += 4) { //BGRA format int R = 0, G = 0, B = 0; if (fractal.getColor().R != 0 && fractal.getColor().G != 0 && fractal.getColor().B != 0) { R = pixels[x, y] % this.fractal.getColor().R; G = pixels[x, y] % this.fractal.getColor().G; B = pixels[x, y] % this.fractal.getColor().B; } else { R = pixels[x, y]; G = pixels[x, y]; B = pixels[x, y]; } part.imageArray[i] = Convert.ToByte(B); //blue part.imageArray[i + 1] = Convert.ToByte(G); //green part.imageArray[i + 2] = Convert.ToByte(R); //red part.imageArray[i + 3] = 255; // Alpha x++; if (x == part.getWidth()) { y++; x = 0; } } //Callback to UI thread when generating is done, add to list and refresh current fractal parts on screen await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { //callback to notify we are done callback(part); }); }