Beispiel #1
0
        private void OnSave(object sender, RoutedEventArgs args)
        {
            args.Handled = true;

            // show preview and wait for input
            SaveGridDialog dialog = new SaveGridDialog()
            {
                Owner = this
            };

            dialog.VisualHost.VisualChild = CreateVisual(new PointD());
            if (dialog.ShowDialog() != true)
            {
                return;
            }

            // ask user for PNG file name
            string file = GetSaveFile();

            if (String.IsNullOrEmpty(file))
            {
                return;
            }

            // grow by (1,1) to ensure all lines are fully visible
            SizeD size   = Grid.DisplayBounds.Size;
            int   width  = Fortran.Ceiling(size.Width + 1),
                  height = Fortran.Ceiling(size.Height + 1);

            // render grid to standard ARGB bitmap
            RenderTargetBitmap bitmap = new RenderTargetBitmap(
                width, height, 96, 96, PixelFormats.Pbgra32);

            bitmap.Render(CreateVisual(new PointD()));
            bitmap.Freeze();

            try {
                // encode bitmap as PNG file
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Interlace = PngInterlaceOption.Off;
                encoder.Frames.Add(BitmapFrame.Create(bitmap));

                using (FileStream stream = new FileStream(file, FileMode.Create))
                    encoder.Save(stream);
            }
            catch (Exception e) {
                string message = String.Format(
                    "An error occurred while attempting\n" +
                    "to save the generated bitmap.\n\n{0}",
                    e.Message);

                MessageBox.Show(this, message, "Save Grid Error",
                                MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Ensures that the <see cref="PaintBuffer"/> covers the specified minimum size.</summary>
        /// <param name="required">
        /// The new minimum size, in device-independent pixels, for the <see cref="PaintBuffer"/>.
        /// </param>
        /// <returns>
        /// <c>true</c> if the <see cref="PaintBuffer"/> must be cleared by the caller; otherwise,
        /// <c>false</c>.</returns>
        /// <remarks>
        /// To avoid frequent buffer reallocations, <b>EnsureBufferSize</b> grows the <see
        /// cref="PaintBuffer"/> to cover twice its current area or to the specified <paramref
        /// name="required"/> size, whichever is bigger. However, <b>EnsureBufferSize</b> will not
        /// grow the <see cref="PaintBuffer"/> beyond the device-independent size of the current
        /// virtual screen, unless the <paramref name="required"/> size is greater.</remarks>

        private bool EnsureBufferSize(SizeI required)
        {
            int   width, height;
            SizeI estimate = required;

            if (PaintBuffer != null)
            {
                width  = PaintBuffer.PixelWidth;
                height = PaintBuffer.PixelHeight;

                // quit if existing buffer is large enough
                if (width >= required.Width && height >= required.Height)
                {
                    return(true);
                }

                // new estimate: double current buffer area
                estimate = new SizeD(width * 1.4, height * 1.4).Round();
            }

            // default to virtual screen size
            SizeI screen = new SizeI(
                Fortran.Ceiling(SystemParameters.VirtualScreenWidth),
                Fortran.Ceiling(SystemParameters.VirtualScreenHeight));

            SizeI minimum = new SizeI(
                Math.Min(screen.Width, estimate.Width),
                Math.Min(screen.Height, estimate.Height));

            // choose maximum of screen size or required size
            width  = Math.Max(minimum.Width, required.Width);
            height = Math.Max(minimum.Height, required.Height);

            // reallocate buffer with new size
            PaintBuffer = new WriteableBitmap(width, height, 96, 96, PixelFormats.Pbgra32, null);
            return(false);
        }
Beispiel #3
0
 public void Ceiling()
 {
     Assert.AreEqual(-3, Fortran.Ceiling(-3.4m));
     Assert.AreEqual(-3, Fortran.Ceiling(-3.4d));
     Assert.AreEqual(-3, Fortran.Ceiling(-3.4f));
 }