Пример #1
0
        /// <summary>
        /// Reads <paramref name="sourceStream"/> as an easly source code and from it create a bitmap using the BMP format in <paramref name="destinationStream"/>.
        /// </summary>
        /// <param name="sourceStream">The source code.</param>
        /// <param name="destinationStream">The destination bitmap.</param>
        /// <returns></returns>
        private static int PreviewFile(FileStream sourceStream, FileStream destinationStream)
        {
            // Create a serializer than can read text or binary formats.
            Serializer Serializer = new Serializer();

            Serializer.Format = SerializationFormat.BinaryPreferred;

            // Reads the source stream as an easly source code.
            INode RootNode = Serializer.Deserialize(sourceStream) as INode;

            // Create a controller for this source code.
            ILayoutRootNodeIndex RootIndex  = new LayoutRootNodeIndex(RootNode);
            ILayoutController    Controller = LayoutController.Create(RootIndex);

            Size ViewSize;

            // Create and open a visual on which to render.
            DrawingVisual DrawingVisual = new DrawingVisual();

            using (DrawingContext dc = DrawingVisual.RenderOpen())
            {
                // Create a draw context using default fonts and brushes.
                DrawContext DrawContext = DrawContext.CreateDrawContext(new Typeface("Consolas"), 10, CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, null, null, hasCommentIcon: true, displayFocus: true);

                // Create a view using custom frames (for how code is organized).
                ILayoutControllerView ControllerView = LayoutControllerView.Create(Controller, EaslyEdit.CustomLayoutTemplateSet.LayoutTemplateSet, DrawContext);
                ControllerView.SetCommentDisplayMode(EaslyController.Constants.CommentDisplayModes.All);

                // Run the measure step to obtain the bitmap size.
                ControllerView.MeasureAndArrange();
                ViewSize = ControllerView.ViewSize;

                // Draw a white background.
                dc.DrawRectangle(Brushes.White, null, new System.Windows.Rect(0, 0, ViewSize.Width.Draw, ViewSize.Height.Draw));

                // Draw the source code.
                DrawContext.SetWpfDrawingContext(dc);
                ControllerView.Draw(ControllerView.RootStateView);
            }

            // At this stage, the visual is ready with drawing data.

            // Create a bitmap and renders the visual on it.
            RenderTargetBitmap Bitmap = new RenderTargetBitmap((int)ViewSize.Width.Draw, (int)ViewSize.Height.Draw, Dpi, Dpi, PixelFormats.Default);

            Bitmap.Render(DrawingVisual);

            // Save the bitmap to the destination file with a BMP format encoder.
            BmpBitmapEncoder Encoder = new BmpBitmapEncoder();
            BitmapFrame      Frame   = BitmapFrame.Create(Bitmap);

            Encoder.Frames.Add(Frame);
            Encoder.Save(destinationStream);

            return(0);
        }
Пример #2
0
        /// <summary>
        /// When overridden in a derived class, participates in rendering operations that are directed by the layout system. The rendering instructions for this element are not used directly when this method is invoked, and are instead preserved for later asynchronous use by layout and drawing.
        /// </summary>
        /// <param name="dc">The drawing instructions for a specific element. This context is provided to the layout system.</param>
        protected override void OnRender(DrawingContext dc)
        {
            base.OnRender(dc);

            if (ControllerView != null)
            {
                DrawContext.SetWpfDrawingContext(dc);

                Brush WriteBrush = Brushes.White;
                Rect  Fullrect   = new Rect(0, 0, ActualWidth, ActualHeight);
                dc.DrawRectangle(Background, null, Fullrect);

                ControllerView.Draw(ControllerView.RootStateView);
            }
        }