本文整理汇总了C#中System.Windows.Controls.Canvas.Arrange方法的典型用法代码示例。如果您正苦于以下问题:C# Canvas.Arrange方法的具体用法?C# Canvas.Arrange怎么用?C# Canvas.Arrange使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Controls.Canvas
的用法示例。
在下文中一共展示了Canvas.Arrange方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
示例2: 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;
}
示例3: 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);
}
}
}
示例4: 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;
}
示例5: 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! ;}";
}
示例6: 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;
}
示例7: 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);
}
示例8: 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;
}
示例9: 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);
}
}
示例10: 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);
}
}
示例11: 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);
}
示例12: canvastoBitmap
public BitmapImage canvastoBitmap(Canvas canvas)
{
Size size = new Size(canvas.ActualWidth, canvas.ActualHeight);
canvas.Measure(size);
canvas.Arrange(new Rect(size));
RenderTargetBitmap bitmap = new RenderTargetBitmap((int)canvas.ActualWidth, (int)canvas.ActualHeight, 96d, 96d, PixelFormats.Pbgra32);
bitmap.Render(canvas);
BitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
BitmapImage bmp = new BitmapImage() { CacheOption = BitmapCacheOption.OnLoad };
MemoryStream outStream = new MemoryStream();
encoder.Save(outStream);
outStream.Seek(0, SeekOrigin.Begin);
bmp.BeginInit();
bmp.StreamSource = outStream;
bmp.EndInit();
return bmp;
}
示例13: ExportToPng
public void ExportToPng(Uri path, Canvas surface)
{
if (path == null)
return;
// Save current canvas transform
Transform transform = surface.LayoutTransform;
// reset current transform (in case it is scaled or rotated)
surface.LayoutTransform = null;
// Get the size of canvas
System.Windows.Size size = new System.Windows.Size(surface.Width, surface.Height);
// Measure and arrange the surface
// VERY IMPORTANT
surface.Measure(size);
surface.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(surface);
// Create a file stream for saving image
using (FileStream outStream = new FileStream(path.LocalPath, FileMode.Create))
{
// Use png encoder for our data
PngBitmapEncoder encoder = new PngBitmapEncoder();
// push the rendered bitmap to it
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
// save the data to the stream
encoder.Save(outStream);
}
// Restore previously saved layout
surface.LayoutTransform = transform;
}
示例14: CanvasLayoutTest
public void CanvasLayoutTest()
{
Canvas panel = new Canvas();
FrameworkElement child1 = new FrameworkElement { Width = 200, Height = 100 };
FrameworkElement child2 = new FrameworkElement { Width = 200, Height = 100 };
FrameworkElement child3 = new FrameworkElement { Width = 200, Height = 100 };
FrameworkElement child4 = new FrameworkElement { Width = 200, Height = 100 };
Canvas.SetLeft(child1, 20);
Canvas.SetTop(child1, 10);
Canvas.SetRight(child2, 20);
Canvas.SetTop(child2, 10);
Canvas.SetRight(child3, 20);
Canvas.SetBottom(child3, 10);
Canvas.SetLeft(child4, 20);
Canvas.SetBottom(child4, 10);
panel.Children.Add(child1);
panel.Children.Add(child2);
panel.Children.Add(child3);
panel.Children.Add(child4);
panel.Measure(new Size(1000, 1000));
Assert.AreEqual(Size.Zero, panel.DesiredSize);
panel.Arrange(new Rect(1000, 1000));
Assert.AreEqual(new Size(1000, 1000), panel.VisualSize);
Assert.AreEqual(new Size(200, 100), child1.VisualSize);
Assert.AreEqual(new Size(200, 100), child2.VisualSize);
Assert.AreEqual(new Size(200, 100), child3.VisualSize);
Assert.AreEqual(new Size(200, 100), child4.VisualSize);
Assert.AreEqual(new Point(20, 10), child1.VisualOffset);
Assert.AreEqual(new Point(780, 10), child2.VisualOffset);
Assert.AreEqual(new Point(780, 890), child3.VisualOffset);
Assert.AreEqual(new Point(20, 890), child4.VisualOffset);
}
示例15: XamlCanvasToPngBitmap
/// <summary>
/// Преобразует Xaml-canvas в png изображение
/// </summary>
/// <param name="surface">XAML Canvas</param>
/// <returns>Bitmap, rendered from XAML</returns>
public static Bitmap XamlCanvasToPngBitmap(Canvas surface) {
if (surface == null) {
throw new ArgumentNullException("surface");
}
// Save current canvas transform
var transform = surface.LayoutTransform;
// reset current transform (in case it is scaled or rotated)
surface.LayoutTransform = null;
// Get the size of canvas
var size = new System.Windows.Size(surface.Width, surface.Height);
// Measure and arrange the surface
// VERY IMPORTANT
surface.Measure(size);
surface.Arrange(new Rect(size));
// Create a render bitmap and push the surface to it
var renderBitmap =
new RenderTargetBitmap(
(int)size.Width,
(int)size.Height,
96d,
96d,
PixelFormats.Pbgra32);
renderBitmap.Render(surface);
Bitmap bmp;
// Create a file stream for saving image
using (var stream = new MemoryStream()) {
// Use png encoder for our data
var encoder = new PngBitmapEncoder();
// push the rendered bitmap to it
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
// save the data to the stream
encoder.Save(stream);
bmp = new Bitmap(stream);
}
return bmp;
}