private void SetTextureBitmap(WriteableBitmap textureBitmap = null) { if (timer.IsEnabled == true) { IsDefaultVectorL = true; } if (IsTextureFill == false) { //AnotherTextureFillRadioButtonClick = false; widthBitmap = Globals.DefaultBitmapWidth; heightBitmap = Globals.DefaultBitmapHeight; PictureWriteableBitmap = new WriteableBitmap(widthBitmap, heightBitmap, 96, 96, PixelFormats.Bgra32, null); var colInfo = FillColorComboBox.SelectedItem as ColorInfo; Paint.FillBitmap(PictureWriteableBitmap, colInfo.color); } else { //AnotherTextureFillRadioButtonClick = true; PictureWriteableBitmap = textureBitmap; widthBitmap = PictureWriteableBitmap.PixelWidth; heightBitmap = PictureWriteableBitmap.PixelHeight; } RemoveGrid(MainCanvas); writeableBitmap = new WriteableBitmap(PictureWriteableBitmap); writeableBitmapColor = new SimpleColor[heightBitmap, widthBitmap]; PictureBitmapColor = new SimpleColor[heightBitmap, widthBitmap]; Paint.ReadColorsFromBitmap(PictureWriteableBitmap, PictureBitmapColor); Paint.ReadColorsFromBitmap(writeableBitmap, writeableBitmapColor); triangleFillingMode.writeableBitmap = writeableBitmap; triangleFillingMode.PictureBitmapColor = PictureBitmapColor; triangleFillingMode.writeableBitmapColor = writeableBitmapColor; MainCanvas.Width = widthBitmap; MainCanvas.Height = heightBitmap; this.MaxHeight = heightBitmap + 4 * Globals.GridMargin; image = new Image { Source = writeableBitmap, IsHitTestVisible = false, Width = widthBitmap, Height = heightBitmap }; Panel.SetZIndex(image, Globals.BitmapZIndex); MainCanvas.Children.Add(image); MainCanvas.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); MainCanvas.Arrange(new Rect(0, 0, MainCanvas.DesiredSize.Width, MainCanvas.DesiredSize.Height)); SetNormalVectorBitmap(NormalWriteableBitmap); GenerateGrid(MainCanvas); GenerateTimerPathList(); }
/// <summary> /// Сохраняет текущее содержимое рабочего холста в файле, находящемуся или вновь создаваемому по переданному пути, в виде изображения в формате PNG /// </summary> /// <param name="filePath">Путь, по которому предполагается сохранить текущее содержимое рабочего холста</param> void SaveCanvasToPath(string filePath) { Transform transform = MainCanvas.LayoutTransform; MainCanvas.LayoutTransform = null; Size size = new Size(MainCanvas.ActualWidth, MainCanvas.ActualHeight); MainCanvas.Measure(size); MainCanvas.Arrange(new Rect(size)); RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96.0, 96.0, PixelFormats.Pbgra32); renderBitmap.Render(MainCanvas); MainCanvas.LayoutTransform = transform; WriteableBitmap writableBitmap = new WriteableBitmap(renderBitmap); using (FileStream fileStream = new FileStream(filePath, FileMode.Create)) { PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(writableBitmap)); encoder.Save(fileStream); } }
/// <summary> /// Saves the image of a fractal as a PNG file /// </summary> private void ExportPngSecondClick(object sender, MouseButtonEventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog { DefaultExt = "png", Filter = "*PNG(*.png)|*.png", OverwritePrompt = true, Title = "Save the fractal as a PNG file" }; try { double horizontalOffSet = mainCanvasScroll.HorizontalOffset; double verticalOffSet = mainCanvasScroll.VerticalOffset; saveFileDialog.ShowDialog(); if (saveFileDialog.FileName != string.Empty) { Transform transform = MainCanvas.LayoutTransform; //reset current transform (in case it is scaled or rotated) MainCanvas.LayoutTransform = null; Size size = new Size(MainCanvas.Width, MainCanvas.Height); MainCanvas.Measure(size); MainCanvas.Arrange(new Rect(size)); RenderTargetBitmap rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96d, 96d, PixelFormats.Pbgra32); rtb.Render(MainCanvas); // Create a file stream for saving image using (FileStream outStream = (FileStream)saveFileDialog.OpenFile()) { // Use png encoder for our data PngBitmapEncoder encoder = new PngBitmapEncoder(); // push the rendered bitmap to it encoder.Frames.Add(BitmapFrame.Create(rtb)); // save the data to the stream encoder.Save(outStream); } // Restore previously saved layout MainCanvas.LayoutTransform = transform; Scroll(mainCanvasScroll.ScrollableWidth / 2, mainCanvasScroll.ScrollableHeight / 2); MessageBox.Show("The fractal is saved", "Soft's message", MessageBoxButton.OK, MessageBoxImage.Information); } } catch (FileNotFoundException ex) { MessageBox.Show(ex.Message, "Soft's message", MessageBoxButton.OK, MessageBoxImage.Information); } catch (NullReferenceException ex) { MessageBox.Show(ex.Message, "Soft's message", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { MessageBox.Show(ex.Message, "Soft's message", MessageBoxButton.OK, MessageBoxImage.Information); } }
private void DetectScreens() { var monitors = Monitor.AllMonitorsGranular(); _minLeft = monitors.Min(m => m.NativeBounds.Left); _minTop = monitors.Min(m => m.NativeBounds.Top); _maxRight = monitors.Max(m => m.NativeBounds.Right); _maxBottom = monitors.Max(m => m.NativeBounds.Bottom); MainCanvas.Children.Clear(); foreach (var monitor in monitors) { var rect = new Rectangle { Width = monitor.NativeBounds.Width, Height = monitor.NativeBounds.Height, StrokeThickness = 6 }; rect.SetResourceReference(Shape.StrokeProperty, "Element.Foreground"); rect.SetResourceReference(Shape.FillProperty, monitor.AdapterName == Monitor.AdapterName ? "Element.Background.Checked" : "Element.Background.Hover"); var textBlock = new TextBlock { Text = monitor.AdapterName, TextAlignment = TextAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, FontSize = 26, TextWrapping = TextWrapping.Wrap, Margin = new Thickness(15) }; textBlock.SetResourceReference(TextBlock.ForegroundProperty, "Element.Foreground"); var viewbox = new Viewbox { Child = textBlock, Width = monitor.NativeBounds.Width, Height = monitor.NativeBounds.Height, }; MainCanvas.Children.Add(rect); MainCanvas.Children.Add(viewbox); Canvas.SetLeft(rect, monitor.NativeBounds.Left); Canvas.SetTop(rect, monitor.NativeBounds.Top); Canvas.SetLeft(viewbox, monitor.NativeBounds.Left); Canvas.SetTop(viewbox, monitor.NativeBounds.Top); Panel.SetZIndex(rect, 1); Panel.SetZIndex(viewbox, 2); } MainCanvas.Width = Math.Abs(_minLeft) + Math.Abs(_maxRight); MainCanvas.Height = Math.Abs(_minTop) + Math.Abs(_maxBottom); MainCanvas.Measure(new Size(MainCanvas.Width, MainCanvas.Height)); MainCanvas.Arrange(new Rect(MainCanvas.DesiredSize)); SetViewPort(_minLeft, _maxRight, _minTop, _maxBottom); }
private void Initialize() { CanvasRect = new Rect(0, 0, MainCanvas.ActualWidth, MainCanvas.ActualHeight); RenderTargetBitmap = new RenderTargetBitmap((int)CanvasRect.Width, (int)CanvasRect.Height, 96, 96, PixelFormats.Pbgra32); WriteableBitmap = new WriteableBitmap(RenderTargetBitmap); MainImage.Source = WriteableBitmap; MainCanvas.Measure(new Size(RenderTargetBitmap.Width, RenderTargetBitmap.Height)); MainCanvas.Arrange(new Rect(new Size(RenderTargetBitmap.Width, RenderTargetBitmap.Height))); DispatcherTimer.AutoReset = true; DispatcherTimer.Elapsed += DispatcherTimerTick; SetParameter(framePerSecond: FramePerSecond, fontSize: (int)RenderingEmSize); Drops = new int[(int)(CanvasRect.Width / LetterAdvanceWidth)]; for (var x = 0; x < Drops.Length; x++) { Drops[x] = 1; } }
/// <summary> /// Save/Render a world to PNG. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SaveImage_Click(object sender, RoutedEventArgs e) { //Apply default scale to allow 1:1 pixel world saving. MainCanvas.LayoutTransform = new ScaleTransform(defaultMatrix.M11, defaultMatrix.M22); MainCanvas.UpdateLayout(); SaveFileDialog dialog = new SaveFileDialog() { Filter = "PNG Files (*.png)|*.png", DefaultExt = "png", FileName = "World.png", RestoreDirectory = true }; Nullable <bool> Selected = dialog.ShowDialog(); string path = dialog.FileName; if (Selected == true) { RenderTargetBitmap renderBitmap = new RenderTargetBitmap( (int)MainCanvas.ActualWidth, (int)MainCanvas.ActualHeight, 96d, 96d, PixelFormats.Pbgra32); MainCanvas.Measure(new Size((int)MainCanvas.ActualWidth, (int)MainCanvas.ActualHeight)); MainCanvas.Arrange(new Rect(new Size((int)MainCanvas.ActualWidth, (int)MainCanvas.ActualHeight))); renderBitmap.Render(MainCanvas); PngBitmapEncoder imageEncoder = new PngBitmapEncoder(); imageEncoder.Frames.Add(BitmapFrame.Create(renderBitmap)); using (var fs = File.OpenWrite(path)) { imageEncoder.Save(fs); } MessageBox.Show("Image saved successfully at\n" + path, "Success", MessageBoxButton.OK, MessageBoxImage.Information); } }