Example #1
0
		/// <summary>
		///     Преобразует XAML Drawing/DrawingGroup в png base64 string
		/// </summary>
		/// <param name="width"></param>
		/// <param name="height"></param>
		/// <param name="drawing"></param>
		/// <returns>Base64 string containing png bitmap</returns>
		public static string XamlDrawingToPngBase64String(int width, int height, Drawing drawing) {
			var bitmapEncoder = new PngBitmapEncoder();
			// The image parameters...
			double dpiX = 96;
			double dpiY = 96;

			// The Visual to use as the source of the RenderTargetBitmap.
			var drawingVisual = new DrawingVisual();
			using (var drawingContext = drawingVisual.RenderOpen()) {
				drawingContext.DrawDrawing(drawing);
			}

			var bounds = drawingVisual.ContentBounds;

			var targetBitmap = new RenderTargetBitmap(
				width * 10, height * 10, dpiX, dpiY,
				PixelFormats.Pbgra32);
			drawingVisual.Transform = new ScaleTransform(width * 10 / bounds.Width, height * 10 / bounds.Height);

			targetBitmap.Render(drawingVisual);

			// Encoding the RenderBitmapTarget as an image.
			bitmapEncoder.Frames.Add(BitmapFrame.Create(targetBitmap));

			byte[] values;
			using (var str = new MemoryStream()) {
				bitmapEncoder.Save(str);
				values = str.ToArray();
			}
			return Convert.ToBase64String(values);
		}
    /// <summary>
    /// Initializes a new instance of the <see cref="DiagramDocumentPaginator"/> class.
    /// </summary>
    /// <param name="source">The source.</param>
    /// <param name="printSize">Size of the print.</param>
    internal DiagramDocumentPaginator(DrawingVisual source, Size printSize)
    {
      this.PageSize = printSize;
      this.contentSize = new Size(this.PageSize.Width - (2 * Constants.Margin), this.PageSize.Height - (2 * Constants.Margin));
      this.frameRect = new Rect(new Point(Constants.Margin, Constants.Margin), this.contentSize);
      this.frameRect.Inflate(1, 1);
      this.framePen = new Pen(Brushes.Black, 0.1);

      // Transformation to borderless print size
      Rect bounds = source.DescendantBounds;
      bounds.Union(source.ContentBounds);
      Matrix m = new Matrix();
      m.Translate(-bounds.Left, -bounds.Top);
      double scale = 1; // hardcoded zoom for printing
      this.pageCountX = (int)((bounds.Width * scale) / this.contentSize.Width) + 1;
      this.pageCountY = (int)((bounds.Height * scale) / this.contentSize.Height) + 1;
      m.Scale(scale, scale);

      // Center on available pages
      m.Translate(((this.pageCountX * this.contentSize.Width) - (bounds.Width * scale)) / 2, ((this.pageCountY * this.contentSize.Height) - (bounds.Height * scale)) / 2);

      // Create a new Visual
      DrawingVisual v = new DrawingVisual();
      using (DrawingContext dc = v.RenderOpen())
      {
        dc.PushTransform(new MatrixTransform(m));
        dc.DrawDrawing(source.Drawing);
        foreach (DrawingVisual dv in source.Children)
        {
          dc.DrawDrawing(dv.Drawing);
        }
      }

      this.diagram = v.Drawing;
    }
        public static IEnumerable<PathGeometry> GetPathGeometries(Drawing drawing)
        {
            var result = new List<PathGeometry>();

            Action<Drawing> HandleDrawing = null;
            HandleDrawing = aDrawing =>
            {
                if (aDrawing is DrawingGroup)
                    foreach (Drawing d in ((DrawingGroup)aDrawing).Children)
                    {
                        HandleDrawing(d);
                    }
                if (aDrawing is GeometryDrawing)
                {
                    var gd = (GeometryDrawing)aDrawing;
                    Geometry geometry = gd.Geometry;
                    if (geometry is PathGeometry)
                    {
                        result.Add((PathGeometry)geometry);
                    }
                }
            };
            HandleDrawing(drawing);

            return result;
        }
Example #4
0
        void Update()
        {
            var score  = ScoreBuilder.FromMelodicPhrase(/*DrillQuiz.LowestTreebleStaffPitch, */ DrillQuiz.TestPhrase);
            var layout = Layout.CreateLayout(score);

            MusicDrawing = MusicDrawingBuilder.BuildDrawing(layout);
            RaiseMusicDrawingChanged();
        }
Example #5
0
        public BitmapSource RenderToBitmap(
            System.Windows.Media.Drawing drawing,
            double dpi)
        {
            var visual = DrawOnVisual(drawing);
            var size   = AlignVisual(visual, drawing);

            return(Render(dpi, size, visual));
        }
        private void WalkVisualTree(List <GlyphRun> textObjects, DependencyObject treeObject)
        {
            System.Windows.Media.Drawing content = VisualTreeHelper.GetDrawing(treeObject as Visual);
            BuildGlypeTree(textObjects, content);
            int childCount = VisualTreeHelper.GetChildrenCount(treeObject);

            for (int i = 0; i < childCount; i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(treeObject, i);
                WalkVisualTree(textObjects, child);
            }
        }
Example #7
0
		static void InternalMakeDrawingSerializable(Drawing drawing, GeometryValueSerializer gvs) {
			var dg = drawing as DrawingGroup;
			if (dg != null)
				for(var i = 0; i < dg.Children.Count; ++i)
					InternalMakeDrawingSerializable(dg.Children[i], gvs);
			else {
				var gd = drawing as GeometryDrawing;
				if (gd != null) {
					var sg = gd.Geometry as StreamGeometry;
					if (sg != null && !gvs.CanConvertToString(sg, null))
						gd.Geometry = PathGeometry.CreateFromGeometry(sg);
				}
			}
		}
Example #8
0
        /// <summary>
        /// Determines whether or not a point exists in a Drawing
        /// </summary>
        /// <param name="drawing"> Drawing to hit-test</param>
        /// <param name="point"> Point to hit-test for </param>
        /// <returns>
        /// 'true' if the point exists within the drawing, 'false' otherwise
        /// </returns>
        internal static bool HitTestPoint(Drawing drawing, Point point)
        {
            if (drawing != null)
            {
                HitTestDrawingContextWalker ctx = new HitTestWithPointDrawingContextWalker(point);

                drawing.WalkCurrentValue(ctx);

                return ctx.IsHit;
            }
            else
            {
                return false;
            }
        }
 private void BuildGlypeTree(List <GlyphRun> textObjects, System.Windows.Media.Drawing drawing)
 {
     if (drawing is GlyphRunDrawing)
     {
         textObjects.Add((drawing as GlyphRunDrawing).GlyphRun);
     }
     else if (drawing is DrawingGroup)
     {
         DrawingCollection children = (drawing as DrawingGroup).Children;
         for (int i = 0; i < children.Count; i++)
         {
             BuildGlypeTree(textObjects, children[i]);
         }
     }
 }
Example #10
0
        /// <summary>
        /// Hit-tests a Drawing against a PathGeometry
        /// </summary>
        /// <param name="drawing"> The drawing to hit test against </param>
        /// <param name="geometry"> The geometry (in local coordinate space) to hit test. </param>
        /// <returns>
        /// IntersectionDetail that describes the hit result
        /// </returns>
        internal static IntersectionDetail HitTestGeometry(Drawing drawing, PathGeometry geometry)
        {
            if (drawing != null)
            {
                HitTestDrawingContextWalker ctx =
                    new HitTestWithGeometryDrawingContextWalker(geometry);

                drawing.WalkCurrentValue(ctx);

                return ctx.IntersectionDetail;
            }
            else
            {
                return IntersectionDetail.Empty;
            }
        }
Example #11
0
        public static BitmapSource DrawingToBitmap(Drawing drawing, int size)
        {
            DrawingVisual drawingVisual = new DrawingVisual();
            DrawingContext drawingContext = drawingVisual.RenderOpen();

            drawingContext.PushTransform(new ScaleTransform((double)size / 100, (double)size / 100));
            drawingContext.DrawDrawing(drawing);
            drawingContext.Pop();
            drawingContext.Close();

            RenderTargetBitmap bmp = new RenderTargetBitmap(size, size, 96, 96, PixelFormats.Default);
            bmp.Render(drawingVisual);
            bmp.Freeze();

            return bmp;
        }
Example #12
0
        public static Bitmap ToBitmap(this System.Windows.Media.Drawing drawing, double width, double height, int dpi, BitmapEncoder encoder)
        {
            System.Windows.Controls.Image drawingImage = new System.Windows.Controls.Image {
                Source = new DrawingImage(drawing)
            };

            drawingImage.Arrange(new Rect(0, 0, width, height));

            var bitmap = new RenderTargetBitmap((int)(width / 96 * dpi), (int)(height / 96 * dpi), dpi, dpi, PixelFormats.Pbgra32);

            bitmap.Render(drawingImage);

            MemoryStream stream = new MemoryStream();

            encoder.Frames.Add(BitmapFrame.Create(bitmap));
            encoder.Save(stream);

            return(new Bitmap(stream));
        }
        /// <summary>
        /// Default constructor.
        /// </summary>
        public GuessingPanel(Drawing.Drawing drawingToGuess)
        {
            InitializeComponent();
            currentDrawing = drawingToGuess;
            drawTimer = new System.Windows.Threading.DispatcherTimer();
            drawTimer.Tick += new EventHandler(DrawFrame);
            drawTimer.Interval = new TimeSpan(0, 0, 0, 0, REFRESH_TIME_DRAW);
            timerIsOver = false;
            // Hide buttons and everything
            ButtonPlayAgain.Visibility = System.Windows.Visibility.Collapsed;
            ButtonCheckScore.Visibility = System.Windows.Visibility.Collapsed;
            ButtonTryAgain.Visibility = System.Windows.Visibility.Collapsed;
            UserAttemptFeedbackRight.Visibility = System.Windows.Visibility.Collapsed;
            UserAttemptFeedbackWrong.Visibility = System.Windows.Visibility.Collapsed;
            UserAttemptFeedbackTooLate.Visibility = System.Windows.Visibility.Collapsed;

            DoDraw();

            // No drawing in this phase
            canvas.EditingMode = SurfaceInkEditingMode.None;
        }
Example #14
0
        /// <summary>
        /// Conerts a drawing into a bitmap object compatible with System.Drawing.
        /// </summary>
        /// <param name="drawing"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="stretch"></param>
        /// <returns></returns>
        public static System.Drawing.Bitmap DrawingToBitmap(Drawing drawing, int width, int height, Stretch stretch = Stretch.None)
        {
            var encoder = new PngBitmapEncoder();
            var drawingImage = new DrawingImage(drawing);

            var image = new Image() { Source = drawingImage };
            image.Stretch = stretch;
            image.HorizontalAlignment = HorizontalAlignment.Left;
            image.VerticalAlignment = VerticalAlignment.Top;
            image.Arrange(new Rect(0, 0, width, height));

            var bitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
            bitmap.Render(image);

            encoder.Frames.Add(BitmapFrame.Create(bitmap));

            var stream = new MemoryStream();
            encoder.Save(stream);

            return new System.Drawing.Bitmap(stream);
        }
Example #15
0
        static Drawing NormalizeDrawing(Drawing drawing, Point location, Size size, double angle)
        {
            DrawingGroup dGroup = new DrawingGroup();
            using (DrawingContext dc = dGroup.Open())
            {
                dc.DrawRectangle(Brushes.Transparent, null, new Rect(new Size(100, 100)));

                dc.PushTransform(new RotateTransform(angle, 50, 50));
                dc.PushTransform(new TranslateTransform(location.X, location.Y));
                dc.PushTransform(new ScaleTransform(size.Width / drawing.Bounds.Width, size.Height / drawing.Bounds.Height));
                dc.PushTransform(new TranslateTransform(-drawing.Bounds.Left, -drawing.Bounds.Top));

                dc.DrawDrawing(drawing);

                dc.Pop();
                dc.Pop();
                dc.Pop();
                dc.Pop();
            }

            return dGroup;
        }
        public AfterGuessingPanel(Drawing.Drawing drawingGuessed, ulong userScore)
        {
            this.InitializeComponent();
            this.userScore = userScore;
            bool isHighscore = false;
            IsHighScoreFeedback.Visibility = System.Windows.Visibility.Collapsed;
            IsNotHighScoreFeedback.Visibility = System.Windows.Visibility.Collapsed;
            SaveFeedbackOK.Visibility = System.Windows.Visibility.Collapsed;
            SaveFeedbackNOTOK.Visibility = System.Windows.Visibility.Collapsed;
            ButtonGoBackHome.Visibility = System.Windows.Visibility.Collapsed;
            currentDrawing = drawingGuessed;
            UserScore.Text = FormatScore(userScore);
            Highscores.Highscore high = App.client.GetHighscoreFromServer(drawingGuessed);
            if (high != null)
            {
                ChampionScore.Text = FormatScore(high.score);
                ChampionIdentity.Text = "By " + high.scorerName + " at " + high.scoreTimestamp;
                isHighscore = Highscores.Highscore.CompareScores(userScore, high.score);
            }
            else
            {
                ChampionScore.Text = FormatScore(userScore);
                ChampionIdentity.Text = "";
                ChampionIdentity.Visibility = System.Windows.Visibility.Collapsed;
                isHighscore = true;
            }

            if (isHighscore)
            {
                IsHighScoreFeedback.Visibility = System.Windows.Visibility.Visible;
                IsNotHighScoreFeedback.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                IsHighScoreFeedback.Visibility = System.Windows.Visibility.Collapsed;
                IsNotHighScoreFeedback.Visibility = System.Windows.Visibility.Visible;
            }
        }
        internal GraphPaginator( DrawingVisual source, Size printSize )
        {
            PageSize = printSize;

            contentSize = new Size( printSize.Width - 2 * Margin, printSize.Height - 2 * Margin );

            myFrameRect = new Rect( new Point( Margin, Margin ), contentSize );
            myFrameRect.Inflate( 1, 1 );
            myFramePen = new Pen( Brushes.Black, 0.1 );

            // Transformation to borderless print size
            var bounds = source.DescendantBounds;
            bounds.Union( source.ContentBounds );

            var m = new Matrix();
            m.Translate( -bounds.Left, -bounds.Top );

            double scale = 16; // hardcoded zoom for printing
            myPageCountX = ( int )( ( bounds.Width * scale ) / contentSize.Width ) + 1;
            myPageCountY = ( int )( ( bounds.Height * scale ) / contentSize.Height ) + 1;
            m.Scale( scale, scale );
        
            // Center on available pages
            m.Translate( ( myPageCountX * contentSize.Width - bounds.Width * scale ) / 2, ( myPageCountY * contentSize.Height - bounds.Height * scale ) / 2 );

            var target = new DrawingVisual();
            using( var dc = target.RenderOpen() )
            {
                dc.PushTransform( new MatrixTransform( m ) );
                dc.DrawDrawing( source.Drawing );

                foreach( DrawingVisual child in source.Children )
                {
                    dc.DrawDrawing( child.Drawing );
                }
            }
            myGraph = target.Drawing;
        }
Example #18
0
        static BuzyWait()
        {
            var drawingVisual = new DrawingVisual ();
            using (var drawingContext = drawingVisual.RenderOpen())
            {
                for (var i = 0; i < Spokes; ++i)
                {
                    var t = ((double)i) / Spokes;

                    drawingContext.PushOpacity (1 - t);
                    drawingContext.PushTransform (new RotateTransform (360.0 * t));

                    drawingContext.DrawRoundedRectangle(
                        Brushes.White,
                        null,
                        new Rect(MinRadius, -WidthRadius/2, RadiusRadius - MinRadius, WidthRadius),
                        WidthRadius,
                        WidthRadius
                        );

                    drawingContext.Pop ();
                    drawingContext.Pop ();
                }

            }

            s_drawing = drawingVisual.Drawing.FreezeObject ();

            s_animationClock = new DoubleAnimation (
                0,
                1,
                new Duration (TimeSpan.FromSeconds(2))
                )
                {
                    RepeatBehavior = RepeatBehavior.Forever
                };
            s_animationClock.FreezeObject ();
        }
Example #19
0
 static void ColorizeDrawing(Drawing drawing, Color tintColor)
 {
     if (drawing is DrawingGroup)
     {
         var dg = (DrawingGroup)drawing;
         foreach (var d in dg.Children)
         {
             ColorizeDrawing(d, tintColor);
         }
     }
     else if (drawing is GeometryDrawing)
     {
         var gd = (GeometryDrawing)drawing;
         if (gd.Brush != null)
             ColorizeBrush(gd.Brush, tintColor);
         if (gd.Pen != null)
             ColorizeBrush(gd.Pen.Brush, tintColor);
     }
     else
     {
         throw new NotImplementedException();
     }
 }
Example #20
0
        public override void SetBitmapPixel(object handle, int x, int y, Drawing.Color color)
        {
            var wpfImage = (WpfImage)handle;
            var img = (BitmapSource)wpfImage.MainFrame;
            if (img == null)
                throw new NotSupportedException ("Invalid image format");
            if (img.Format.BitsPerPixel != 32)
                throw new NotSupportedException ("Image format not supported");

            var bitmapImage = img as WriteableBitmap;

            if (bitmapImage == null) {
                bitmapImage = new WriteableBitmap (img);
                ((WpfImage)handle).MainFrame = bitmapImage;
            }

            wpfImage.AllocatePixelData ();
            var offset = wpfImage.GetPixelOffset (x, y);
            wpfImage.PixelData[offset] = (byte)(color.Blue * 255);
            wpfImage.PixelData[offset + 1] = (byte)(color.Green * 255);
            wpfImage.PixelData[offset + 2] = (byte)(color.Red * 255);
            wpfImage.PixelData[offset + 3] = (byte)(color.Alpha * 255);

            bitmapImage.Lock ();
            bitmapImage.WritePixels (new Int32Rect (x, y, 1, 1), wpfImage.PixelData, wpfImage.Stride, offset);
            bitmapImage.Unlock ();
        }
Example #21
0
 public override void SaveToStream(object backend, Stream stream, Drawing.ImageFileType fileType)
 {
     var image = DataConverter.AsImageSource(backend) as BitmapSource;
     BitmapEncoder encoder;
     switch (fileType) {
         case Drawing.ImageFileType.Png: encoder = new PngBitmapEncoder (); break;
         case Drawing.ImageFileType.Jpeg: encoder = new JpegBitmapEncoder (); break;
         case Drawing.ImageFileType.Bmp: encoder = new BmpBitmapEncoder (); break;
         default: throw new NotSupportedException ("Image format not supported");
     }
     encoder.Frames.Add (BitmapFrame.Create (image));
     encoder.Save (stream);
 }
Example #22
0
        public void AddDrawing(Drawing drawing)
        {
            _drawings.Clear();

            _drawings.Add(drawing);
        }
 public DrawingImage(Drawing drawing)
 {
 }
Example #24
0
		protected override void SetWidgetColor (Drawing.Color value)
		{
			// Do nothing
		}
Example #25
0
 private static DrawingBrush CreateStandardDrawingBrush(Drawing drawing)
 {
     return new DrawingBrush
     {
         Drawing = drawing,
         TileMode = TileMode.Tile,
         Viewport = new Rect(0, 0, 10, 10),
         ViewportUnits = BrushMappingMode.Absolute,
     };
 }
 public DrawingBrush(Drawing drawing)
 {
 }
Example #27
0
 public override void ModifyCTM(object backend, Drawing.Matrix m)
 {
     var c = (DrawingContext)backend;
     MatrixTransform t = new MatrixTransform (m.M11, m.M12, m.M21, m.M22, m.OffsetX, m.OffsetY);
     c.PushTransform (t);
 }
 /// <summary>
 ///     DrawDrawing - 
 ///     Draw a Drawing by appending a sub-Drawing to the current Drawing.
 /// </summary>
 /// <param name="drawing"> The drawing to draw. </param>
 public override void DrawDrawing(
     Drawing drawing)
 {
     _drawingContext.DrawDrawing(
         drawing
         );
 }
Example #29
0
 public void SetEdgeLabel(DrawingEdge edge, Drawing.Label label)
 {
     //find the edge first
     DEdge de = null;
     foreach (DEdge dEdge in Edges)
         if (dEdge.DrawingEdge == edge)
         {
             de = dEdge;
             break;
         }
     edge.Label = label;
     de.Label = new DTextLabel(de, label);
     edge.GeometryEdge.Label = label.GeometryLabel;
     ICurve curve = edge.GeometryEdge.Curve;
     label.GeometryLabel.Center = curve[(curve.ParStart + curve.ParEnd) / 2];
     label.GeometryLabel.GeometryParent = edge.GeometryEdge;
 }
Example #30
0
 public void draw(Drawing dc, int x, int y)
 {
 }
 /// <summary>
 ///     DrawDrawing - 
 ///     Draw a Drawing by appending a sub-Drawing to the current Drawing.
 /// </summary>
 /// <param name="drawing"> The drawing to draw. </param>
 public abstract void DrawDrawing(
     Drawing drawing);
Example #32
0
 internal static DrawingImage DrawingToImage(Drawing drawing)
 {
     return new DrawingImage(drawing);
 }
Example #33
0
        //==========================================================================
        private static void ConvertColors(Drawing drawing)
        {
            if(drawing is DrawingGroup)
              {
            foreach(Drawing child_drawing in (drawing as DrawingGroup).Children)
              ConvertColors(child_drawing);
              }
              else if(drawing is GeometryDrawing)
              {
            ConvertColors((drawing as GeometryDrawing).Brush);
            ConvertColors((drawing as GeometryDrawing).Pen);
              }
              else if(drawing is ImageDrawing)
              {
            if((drawing as ImageDrawing).ImageSource is BitmapSource)
            {
              BitmapSource bitmap_source = (drawing as ImageDrawing).ImageSource as BitmapSource;
              if(bitmap_source != null)
              {
            WriteableBitmap bitmap = new WriteableBitmap(bitmap_source.PixelWidth, bitmap_source.PixelHeight, bitmap_source.DpiX, bitmap_source.DpiY, PixelFormats.Bgra32, null);

            byte[] pixels = new byte[bitmap.PixelWidth * bitmap.PixelHeight * 4];
            bitmap_source.CopyPixels(pixels, (bitmap_source.PixelWidth * bitmap_source.Format.BitsPerPixel + 7) / 8, 0);

            for(int i = 0; i < pixels.Length; i+=4)
            {
              byte r = pixels[i + 0];
              byte g = pixels[i + 1];
              byte b = pixels[i + 2];

              byte max = Math.Max(Math.Max(r, g), b);
              byte min = Math.Min(Math.Min(r, g), b);

              byte a = (byte)(((int)min + (int)max) / 2);

              pixels[i + 3] = a;
            }

            bitmap.WritePixels(new Int32Rect(0,0,bitmap.PixelWidth, bitmap.PixelHeight), pixels,
                                      (bitmap.PixelWidth * bitmap.Format.BitsPerPixel + 7) / 8, 0);

            (drawing as ImageDrawing).ImageSource = bitmap;
              }
            }
            else if((drawing as ImageDrawing).ImageSource is DrawingImage)
            {
              ConvertColors(((drawing as ImageDrawing).ImageSource as DrawingImage).Drawing);
            }
            else
              throw new NotSupportedException();
              }
              else
            throw new NotSupportedException();
        }
Example #34
0
 public static Bitmap ToBitmap(this System.Windows.Media.Drawing drawing, double width, double height)
 {
     return(drawing.ToBitmap(width, height, 96, new PngBitmapEncoder()));
 }
Example #35
0
		public DrawingImage (Drawing drawing)
		{
			Drawing = drawing;
		}
Example #36
0
        public override void DrawDrawing(
            Drawing drawing)
        {
            VerifyApiNonstructuralChange();

            if (drawing == null)
            {
                return;
            }


        #if DEBUG
            MediaTrace.DrawingContextOp.Trace("DrawDrawing(const)");
        #endif

            unsafe
            {
                EnsureRenderData();



                MILCMD_DRAW_DRAWING record =
                    new MILCMD_DRAW_DRAWING (
                        _renderData.AddDependentResource(drawing)
                        );

                // Assert that the calculated packet size is the same as the size returned by sizeof().
                Debug.Assert(sizeof(MILCMD_DRAW_DRAWING) == 8);

                _renderData.WriteDataRecord(MILCMD.MilDrawDrawing,
                                            (byte*)&record,
                                            8 /* sizeof(MILCMD_DRAW_DRAWING) */);
            }                           



        }