当前位置: 首页>>代码示例>>C#>>正文


C# Canvas.Measure方法代码示例

本文整理汇总了C#中System.Windows.Controls.Canvas.Measure方法的典型用法代码示例。如果您正苦于以下问题:C# Canvas.Measure方法的具体用法?C# Canvas.Measure怎么用?C# Canvas.Measure使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Windows.Controls.Canvas的用法示例。


在下文中一共展示了Canvas.Measure方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RenderSample

 protected override void RenderSample()
 {
     var canvas = new Canvas { Width = 10, Height = 10, SnapsToDevicePixels = true, Background = Brushes.Transparent };
     canvas.Children.Add(new Path
     {
         Stroke = Stroke,
         StrokeThickness = StrokeThickness,
         Fill = Fill,
         Data = new RectangleGeometry(new Rect(0, 0, 10, 10)),
     });
     canvas.Measure(new Size(canvas.Width, canvas.Height));
     canvas.Arrange(new Rect(0, 0, canvas.Width, canvas.Height));
     var dpiX = 96.0;
     var dpiY = 96.0;
     var source = PresentationSource.FromVisual(Application.Current.MainWindow);
     if (source != null && source.CompositionTarget != null)
     {
         var matrix = source.CompositionTarget.TransformToDevice;
         dpiX *= matrix.M11;
         dpiY *= matrix.M22;
     }
     var rtb = new RenderTargetBitmap((int)Math.Round(canvas.Width), (int)Math.Round(canvas.Height), dpiX, dpiY, PixelFormats.Pbgra32);
     rtb.Render(canvas);
     SampleImageSource = rtb;
 }
开发者ID:AuditoryBiophysicsLab,项目名称:ESME-Workbench,代码行数:25,代码来源:BarSeriesViewModel.cs

示例2: Export

        /// <summary>
        /// Exports the specified plot model to an xps file.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="fileName">The file name.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="background">The background color.</param>
        public static void Export(IPlotModel model, string fileName, double width, double height, OxyColor background)
        {
            using (var xpsPackage = Package.Open(fileName, FileMode.Create, FileAccess.ReadWrite))
            {
                using (var doc = new XpsDocument(xpsPackage))
                {
                    var canvas = new Canvas { Width = width, Height = height, Background = background.ToBrush() };
                    canvas.Measure(new Size(width, height));
                    canvas.Arrange(new Rect(0, 0, width, height));

                    var rc = new ShapesRenderContext(canvas);
#if !NET35
                    rc.TextFormattingMode = TextFormattingMode.Ideal;
#endif

                    model.Update(true);
                    model.Render(rc, width, height);

                    canvas.UpdateLayout();

                    var xpsdw = XpsDocument.CreateXpsDocumentWriter(doc);
                    xpsdw.Write(canvas);
                }
            }
        }
开发者ID:benjaminrupp,项目名称:oxyplot,代码行数:33,代码来源:XpsExporter.cs

示例3: SaveToPng

        public void SaveToPng( Canvas surface, string file )
        {
            // Save current canvas transform
            var transform = surface.LayoutTransform;
            // reset current transform (in case it is scaled or rotated)
            surface.LayoutTransform = null;

            var size = new Size( 1600, 900 );

            // Attentation: Measure and arrange the surface !
            surface.Measure( size );
            surface.Arrange( new Rect( size ) );

            var renderBitmap = new RenderTargetBitmap( (int)size.Width, (int)size.Height, 96d, 96d, PixelFormats.Pbgra32 );

            var bounds = VisualTreeHelper.GetDescendantBounds( surface );
            var dv = new DrawingVisual();
            using ( var ctx = dv.RenderOpen() )
            {
                var vb = new VisualBrush( surface );
                ctx.DrawRectangle( vb, null, new Rect( new Point(), bounds.Size ) );
            }

            renderBitmap.Render( dv );
            using ( var outStream = new FileStream( file, FileMode.OpenOrCreate, FileAccess.Write ) )
            {
                var encoder = new PngBitmapEncoder();
                encoder.Frames.Add( BitmapFrame.Create( renderBitmap ) );
                encoder.Save( outStream );
            }

            // Restore previously saved layout
            surface.LayoutTransform = transform;
        }
开发者ID:JackWangCUMT,项目名称:Plainion.Whiteboard,代码行数:34,代码来源:PersistenceService.cs

示例4: Rasterize

        public static bool[,] Rasterize(Point[] points, int width, int height)
        {
            Contract.Requires(points != null);
            Contract.Requires(width > 0);
            Contract.Requires(height > 0);
            Contract.Requires(width % 8 == 0);
            Contract.Ensures(Contract.Result<bool[,]>() != null);
            Contract.Ensures(Contract.Result<bool[,]>().GetLength(0) == width);
            Contract.Ensures(Contract.Result<bool[,]>().GetLength(1) == height);

            var canvas = new Canvas { Background = Brushes.White, Width = width, Height = height };
            var polygon = new Polygon { Stroke = Brushes.Black, Fill = Brushes.Black, StrokeThickness = 1, Points = new PointCollection(points) };
            canvas.Children.Add(polygon);
            RenderOptions.SetEdgeMode(canvas, EdgeMode.Aliased);

            canvas.Measure(new Size(width, height));
            canvas.Arrange(new Rect(0, 0, canvas.DesiredSize.Width, canvas.DesiredSize.Height));

            var rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
            rtb.Render(canvas);

            var fmb = new FormatConvertedBitmap(rtb, PixelFormats.BlackWhite, null, 0);
            var pixels = new byte[width * height / 8];
            fmb.CopyPixels(pixels, width / 8, 0);

            System.Collections.BitArray ba = new System.Collections.BitArray(pixels);

            var result = new bool[width, height];
            for (int i = 0, y = 0; y < height; ++y)
                for (int x = 0; x < width; ++x, ++i)
                    result[x, y] = !ba[i];

            return result;
        }
开发者ID:alexshtf,项目名称:Various-utility-projects,代码行数:34,代码来源:PolygonRasterizer.cs

示例5: SaveCanvasToBitmap

        public static RenderTargetBitmap SaveCanvasToBitmap(Canvas canvas)
        {
            // Save current canvas transform
            Transform transform = canvas.LayoutTransform;
            // reset current transform (in case it is scaled or rotated)
            canvas.LayoutTransform = null;

            // Get the size of canvas
            Size size = new Size(canvas.Width, canvas.Height);
            // Measure and arrange the surface
            // VERY IMPORTANT
            canvas.Measure(size);
            canvas.Arrange(new Rect(size));

            // Create a render bitmap and push the surface to it
            RenderTargetBitmap renderBitmap =
              new RenderTargetBitmap(
                (int)size.Width,
                (int)size.Height,
                96d,
                96d,
                PixelFormats.Pbgra32);
            renderBitmap.Render(canvas);

            return renderBitmap;
        }
开发者ID:steamprodz,项目名称:DantistApp,代码行数:26,代码来源:CanvasHelper.cs

示例6: loader_RunWorkerCompleted

      void loader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
         buff.UpdateLayout();

         Canvas canvas = new Canvas();
         canvas.Children.Add(buff);
         canvas.Width = 512 * 13;
         canvas.Height = 512 * 7;

         canvas.UpdateLayout();

         canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
         canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));
         int Height = ((int)(canvas.ActualHeight));
         int Width = ((int)(canvas.ActualWidth));

         RenderTargetBitmap _RenderTargetBitmap = new RenderTargetBitmap(Width, Height, 96, 96, PixelFormats.Pbgra32);
         _RenderTargetBitmap.Render(buff);

         Image img = new Image();
         img.Source = _RenderTargetBitmap;

         Viewer.PanoramaImage = _RenderTargetBitmap;

         Title = "Demo.StreetView, enjoy! ;}";
      }
开发者ID:chharam,项目名称:Capstone_IPM_RV,代码行数:26,代码来源:Window1.xaml.cs

示例7: ChildlessMeasureTest

		public void ChildlessMeasureTest ()
		{
			Canvas c = new Canvas ();
			Size s = new Size (10,10);

			c.Measure (s);

			Assert.AreEqual (new Size (0,0), c.DesiredSize, "DesiredSize");
		}
开发者ID:kangaroo,项目名称:moon,代码行数:9,代码来源:CanvasTest.cs

示例8: Store

 public static void Store(IEnumerable<Point> points, int width, int height, string fileName)
 {
     var canvas = new Canvas { Background = Brushes.White };
     canvas.Width = width;
     canvas.Height = height;
     canvas.Children.Add(new Polyline { Stroke = Brushes.Black, StrokeThickness = 1, Points = new PointCollection(points) });
     canvas.Measure(new Size(width, height));
     canvas.Arrange(new Rect(0, 0, canvas.DesiredSize.Width, canvas.DesiredSize.Height));
     RenderOptions.SetEdgeMode(canvas, EdgeMode.Aliased);
     Store(canvas, fileName);
 }
开发者ID:alexshtf,项目名称:Various-utility-projects,代码行数:11,代码来源:ImageWriter.cs

示例9: RenderSample

 protected override void RenderSample()
 {
     StreamGeometry geometry;
     var canvas = new Canvas { Width = Math.Max(MarkerSize + 10, 10), Height = Math.Max(MarkerSize + 2, 10), SnapsToDevicePixels = true, Background = Brushes.Transparent };
     if (DrawLine)
     {
         geometry = new StreamGeometry();
         using (var ctx = geometry.Open())
         {
             ctx.BeginFigure(new Point(0, canvas.Height / 2), false, false);
             ctx.LineTo(new Point(canvas.Width, canvas.Height / 2), true, false);
         }
         canvas.Children.Add(new Path
         {
             Stroke = LineStroke,
             StrokeThickness = LineStrokeThickness,
             StrokeDashArray = LineStrokeDashArray,
             Data = geometry,
         });
     }
     if (DrawMarker)
     {
         geometry = new StreamGeometry();
         using (var ctx = geometry.Open())
         {
             MarkerType(ctx, new Point(canvas.Width / 2, canvas.Height / 2), MarkerSize);
         }
         canvas.Children.Add(new Path
         {
             Stroke = MarkerStroke,
             StrokeThickness = MarkerStrokeThickness,
             Data = geometry,
         });
     }
     canvas.Measure(new Size(canvas.Width, canvas.Height));
     canvas.Arrange(new Rect(0, 0, canvas.Width, canvas.Height));
     var dpiX = 96.0;
     var dpiY = 96.0;
     var source = PresentationSource.FromVisual(Application.Current.MainWindow);
     if (source != null && source.CompositionTarget != null)
     {
         var matrix = source.CompositionTarget.TransformToDevice;
         dpiX *= matrix.M11;
         dpiY *= matrix.M22;
     } 
     var rtb = new RenderTargetBitmap((int)Math.Round(canvas.Width), (int)Math.Round(canvas.Height), dpiX, dpiY, PixelFormats.Pbgra32);
     rtb.Render(canvas);
     SampleImageSource = rtb;
 }
开发者ID:AuditoryBiophysicsLab,项目名称:ESME-Workbench,代码行数:49,代码来源:LineSeriesViewModel.cs

示例10: CreateIcon

        protected void CreateIcon(Floor floor)
        {
            ItemsControl control = new ItemsControl();
            Canvas canvas = new Canvas();
            control.ItemTemplate = Application.Current.FindResource("SegmentRowTemplate") as DataTemplate;
            canvas.Children.Add(control);

            control.ItemsSource = floor.Segments;
            control.UpdateLayout();
            canvas.Measure(new Size(1000, 1000));
            canvas.Arrange(new Rect(new Size(1000, 1000)));

            RenderTargetBitmap bmp = new RenderTargetBitmap((int)control.DesiredSize.Width, (int)control.DesiredSize.Height, 96, 96, PixelFormats.Pbgra32);
            bmp.Render(control);

            floor.Icon = bmp;
        }
开发者ID:OlekNg,项目名称:AHMED,代码行数:17,代码来源:FloorIconUpdater.cs

示例11: ChildMeasureTest2

		public void ChildMeasureTest2 ()
		{
			Canvas c = new Canvas ();
			Rectangle r = new Rectangle();

			c.Children.Add (r);

			Canvas.SetLeft (r, 10);
			Canvas.SetTop (r, 10);

			r.Width = 50;
			r.Height = 50;

			c.Measure (new Size (100, 100));

			Assert.AreEqual (new Size (0,0), c.DesiredSize);
		}
开发者ID:kangaroo,项目名称:moon,代码行数:17,代码来源:CanvasTest.cs

示例12: SaveFile

        public void SaveFile(Canvas canvas, string filename)
        {
            RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)canvas.Width, (int)canvas.Height, 96d, 96d, PixelFormats.Pbgra32);
            // needed otherwise the image output is black
            canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
            canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));

            renderBitmap.Render(canvas);

            //JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(renderBitmap));

            using (FileStream file = File.Create(filename))
            {
                encoder.Save(file);
            }
        }
开发者ID:pawelsudo,项目名称:BasicPaint,代码行数:18,代码来源:SaveHelper.cs

示例13: CreateImage

        /// <summary>
        /// Renders a UI control into an image.
        /// </summary>
        /// <param name="control"></param>
        /// <param name="isWideTile"></param>
        /// <returns></returns>
        public static void CreateImage(UIElement control, string imagePath, int width, int height, SolidColorBrush tileBackgroundColor)
        {
            // 1. Setup dimensions for wide tile.
            var bmp = new WriteableBitmap(width, height);

            // 2. Get the name of the background image based on theme            
            var canvas = new System.Windows.Controls.Canvas();
            canvas.Width = width;
            canvas.Height = height;
            canvas.Background = tileBackgroundColor;

            canvas.Children.Add(control);
            canvas.Measure(new Size(width, height));
            canvas.Arrange(new Rect(0, 0, width, height));
            canvas.UpdateLayout();

            // 4. Now output the control as text.
            bmp.Render(canvas, null);
            bmp.Invalidate();

            // 8. Now save the image to local folder.
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                // FileMode.Open, FileAccess.Read, FileShare.Read,
                using (var st = new IsolatedStorageFileStream(imagePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, store))
                {
                    bmp.SaveJpeg(st, width, height, 0, 100);
                    st.Close();
                }
            }

            try
            {

                bmp = null;
                canvas.Children.Clear();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            catch (Exception ex)
            {
                Slate.Core.Logging.Logger.Error("Create image", "Warning, attempt to clear up memory for tile image failed", ex);
            }
        }
开发者ID:kishorereddy,项目名称:slate-lib,代码行数:50,代码来源:ImageHelper.cs

示例14: Export

        /// <summary>
        /// Exports the specified plot model to a stream.
        /// </summary>
        /// <param name="model">The plot model.</param>
        /// <param name="stream">The stream to write to.</param>
        /// <param name="width">The width of the export image.</param>
        /// <param name="height">The height of the exported image.</param>
        /// <param name="background">The background.</param>
        public static void Export(IPlotModel model, Stream stream, double width, double height, OxyColor background)
        {
            var canvas = new Canvas { Width = width, Height = height };
            if (background.IsVisible())
            {
                canvas.Background = background.ToBrush();
            }

            canvas.Measure(new Size(width, height));
            canvas.Arrange(new Rect(0, 0, width, height));

            var rc = new SilverlightRenderContext(canvas);
            model.Update(true);
            model.Render(rc, width, height);

            canvas.UpdateLayout();
            var image = canvas.ToImage();
            image.WriteToStream(stream);
        }
开发者ID:benjaminrupp,项目名称:oxyplot,代码行数:27,代码来源:PngExporter.cs

示例15: MeasureTest2

		public void MeasureTest2 ()
		{
			Canvas c = new Canvas ();
			Rectangle r = new Rectangle ();
			r.Width = 10;
			r.Height = 20;

			r.Measure (new Size (50, 50));
			Assert.AreEqual (new Size (0, 0), r.DesiredSize);
			
			c.Children.Add (r);

			r.Measure (new Size (50, 50));
			Assert.AreEqual (new Size (0, 0), r.DesiredSize);

			c.Measure (new Size (50, 50));

			Assert.AreEqual (new Size (0, 0), c.DesiredSize, "Canvas desired");
			Assert.AreEqual (new Size (0, 0), r.DesiredSize, "Rectangle desired");
		}
开发者ID:dfr0,项目名称:moon,代码行数:20,代码来源:RectangleTest.cs


注:本文中的System.Windows.Controls.Canvas.Measure方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。