本文整理汇总了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;
}
示例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);
}
}
}
示例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;
}
示例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;
}
示例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;
}
示例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! ;}";
}
示例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");
}
示例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);
}
示例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;
}
示例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;
}
示例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);
}
示例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);
}
}
示例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);
}
}
示例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);
}
示例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");
}