本文整理汇总了C#中System.Windows.FrameworkElement.Measure方法的典型用法代码示例。如果您正苦于以下问题:C# FrameworkElement.Measure方法的具体用法?C# FrameworkElement.Measure怎么用?C# FrameworkElement.Measure使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.FrameworkElement
的用法示例。
在下文中一共展示了FrameworkElement.Measure方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetPngImage
private static byte[] GetPngImage(FrameworkElement element)
{
var size = new Size(double.PositiveInfinity, double.PositiveInfinity);
element.Measure(size);
element.Arrange(new Rect(element.DesiredSize));
var renderTarget =
new RenderTargetBitmap((int)element.RenderSize.Width,
(int)element.RenderSize.Height,
96, 96,
PixelFormats.Pbgra32);
var sourceBrush = new VisualBrush(element);
var drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
drawingContext.DrawRectangle(
sourceBrush, null, new Rect(
new Point(0, 0),
new Point(element.RenderSize.Width,
element.RenderSize.Height)));
}
renderTarget.Render(drawingVisual);
var pngEncoder = new PngBitmapEncoder();
pngEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
using (var outputStream = new MemoryStream())
{
pngEncoder.Save(outputStream);
return outputStream.ToArray();
}
}
示例2: RealizeFrameworkElement
public static void RealizeFrameworkElement(FrameworkElement fe) {
var size = new Size(double.MaxValue, double.MaxValue);
if (fe.Width > 0 && fe.Height > 0) size = new Size(fe.Width, fe.Height);
fe.Measure(size);
fe.Arrange(new Rect(new Point(), fe.DesiredSize));
fe.UpdateLayout();
}
示例3: UpdateAndMeasure
public Size UpdateAndMeasure(FrameworkElement element)
{
element.LayoutTransform = ChildRenderTransform;
element.UpdateLayout();
element.Measure(new Size(BodySize.Width, double.PositiveInfinity));
return element.DesiredSize;
}
示例4: DoWithElementAtSize
private void DoWithElementAtSize(FrameworkElement element, Size size, Action action)
{
Transform transform = element.LayoutTransform;
element.LayoutTransform = null;
element.Measure(size);
element.Arrange(new Rect(size));
action();
element.LayoutTransform = transform;
}
示例5: InvalidatePosition
protected virtual void InvalidatePosition(FrameworkElement child)
{
if (viewport == null) return;
var transform = GetTransform(availableSize);
Size elementSize = GetElementSize(child, AvailableSize, transform);
child.Measure(elementSize);
Rect bounds = GetElementScreenBounds(transform, child);
if (!bounds.IsNaN())
{
child.Arrange(bounds);
}
}
示例6: ToImageSource
public static ImageSource ToImageSource(FrameworkElement obj)
{
Transform transform = obj.LayoutTransform;
Thickness margin = obj.Margin;
obj.Margin = new Thickness(0, 0, margin.Right - margin.Left, margin.Bottom - margin.Top);
Size size = new Size(obj.ActualWidth, obj.ActualHeight);
obj.Measure(size);
obj.Arrange(new Rect(size));
RenderTargetBitmap bmp = new RenderTargetBitmap((int)obj.ActualWidth, (int)obj.ActualHeight, 96, 96, PixelFormats.Pbgra32);
bmp.Render(obj);
obj.LayoutTransform = transform;
obj.Margin = margin;
return bmp;
}
示例7: TryAdd
public bool TryAdd(FrameworkElement control)
{
control.Measure(new Size(Width, Height));
Children.Add(control);
var sizeAfterControlHasBeenAdded = MeasureOverride(new Size(Width, Height));
// If the new size is larger than the WrapPanel remove the control and return false
if (sizeAfterControlHasBeenAdded.Width > Width || sizeAfterControlHasBeenAdded.Height > Height)
{
Children.Remove(control);
return false;
}
return true;
}
示例8: WpfVisual
public WpfVisual(FrameworkElement element, int defaultWidth = BadgeCaps.Width, int defaultHeight = BadgeCaps.Height, bool dither = false, bool enableBlend = false)
{
Element = element;
Element.Measure(new Size(defaultWidth, defaultHeight));
Element.Arrange(new Rect(0, 0, defaultWidth, defaultHeight));
ClipWidth = (int)Math.Ceiling(Element.ActualWidth);
ClipHeight = (int)Math.Ceiling(Element.ActualHeight);
Dither = dither;
EnableBlend = enableBlend;
m_cachedIntermediate = new BadgeRenderTarget(ClipWidth, ClipHeight);
m_renderTarget = new RenderTargetBitmap(ClipWidth, ClipHeight, 96, 96, PixelFormats.Pbgra32);
Update(0); // To avoid remeasuring on a background thread
UpdateCachedImage();
}
示例9: XAMLToBitmap
public static void XAMLToBitmap(FrameworkElement element, string fileName)
{
try
{
element.RenderTransform = new ScaleTransform(0.1, 0.1);
element.Measure(new Size((int)element.Width, (int)element.Height));
element.Arrange(new Rect(new Size((int)element.Width, (int)element.Height)));
var renderTargetBitmap = new RenderTargetBitmap((int)element.ActualWidth / 10, (int)element.ActualHeight / 10, 96d, 96d, PixelFormats.Pbgra32);
renderTargetBitmap.Render(element);
using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
{
var bmpBitmapEncoder = new BmpBitmapEncoder();
bmpBitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
bmpBitmapEncoder.Save(fileStream);
}
}
catch { }
}
示例10: CalculateVisualSize
/// <summary>
/// Calculates visual size
/// </summary>
/// <param name="visual">Visual as FrameworkElement</param>
/// <returns>Visual size</returns>
public static Size CalculateVisualSize(FrameworkElement visual)
{
Size retVal = new Size(0,0);
if (visual != null)
{
visual.Measure(new Size(Double.MaxValue, Double.MaxValue));
retVal = visual.DesiredSize;
}
return retVal;
}
示例11: ModifyPosition
/// <summary>
/// The modify position.
/// </summary>
/// <param name="fe">
/// The fe.
/// </param>
private static void ModifyPosition(FrameworkElement fe)
{
var fs = new Size(
fe.ActualWidth + fe.Margin.Left + fe.Margin.Right, fe.ActualHeight + fe.Margin.Top + fe.Margin.Bottom);
fe.Measure(fs);
fe.Arrange(new Rect(-fe.Margin.Left, -fe.Margin.Top, fs.Width, fs.Height));
}
示例12: SetClip
public static void SetClip(FrameworkElement element)
{
element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
element.Clip = new RectangleGeometry { Rect = new Rect(0, 0, element.ActualWidth, element.ActualHeight) };
}
示例13: ModifyPositionBack
/// <summary>
/// The modify position back.
/// </summary>
/// <param name="fe">
/// The fe.
/// </param>
private static void ModifyPositionBack(FrameworkElement fe)
{
fe.Measure(new Size());
}
示例14: RenderControl
/// <summary>
/// This tells a visual to render itself to a wpf bitmap. From there, you can get the bytes (colors), or run it through a converter
/// to save as jpg, bmp files.
/// </summary>
/// <remarks>
/// This fixes an issue where the rendered image is blank:
/// http://blogs.msdn.com/b/jaimer/archive/2009/07/03/rendertargetbitmap-tips.aspx
/// </remarks>
public static BitmapSource RenderControl(FrameworkElement visual, int width, int height, bool isInVisualTree)
{
if (!isInVisualTree)
{
// If the visual isn't part of the visual tree, then it needs to be forced to finish its layout
visual.Width = width;
visual.Height = height;
visual.Measure(new Size(width, height)); // I thought these two statements would be expensive, but profiling shows it's mostly all on Render
visual.Arrange(new Rect(0, 0, width, height));
}
RenderTargetBitmap retVal = new RenderTargetBitmap(width, height, DPI, DPI, PixelFormats.Pbgra32);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext ctx = dv.RenderOpen())
{
VisualBrush vb = new VisualBrush(visual);
ctx.DrawRectangle(vb, null, new Rect(new Point(0, 0), new Point(width, height)));
}
retVal.Render(dv); // profiling shows this is the biggest hit
return retVal;
}
示例15: InitializeBusyIndicator
partial void InitializeBusyIndicator()
{
if (BusyIndicator != null)
{
return;
}
BusyIndicator = CreateBusyIndicator();
BusyIndicator.DataContext = _busyIndicatorDataContext;
_containerPopup = new Popup();
_containerPopup.VerticalAlignment = VerticalAlignment.Center;
_containerPopup.HorizontalAlignment = HorizontalAlignment.Center;
_containerPopup.Child = BusyIndicator;
_containerPopup.LayoutUpdated += (sender, e) =>
{
if (Application.Current == null)
{
return;
}
// 1 is the default old behavior but since we set both horizontal and vertical offset, we might
// be causing a "Layout cycle"
_skipLayoutUpdateCount++;
if (_skipLayoutUpdateCount < 2)
{
return;
}
_skipLayoutUpdateCount = 0;
var parent = _parent.Target as FrameworkElement;
if (parent == null)
{
return;
}
try
{
if (parent.ActualHeight == 0.0d && parent.ActualWidth == 0.0d)
{
Hide();
return;
}
var transform = parent.TransformToVisual(Application.Current.RootVisual);
var offset = transform.Transform(new Point(0, 0));
double parentTop = offset.Y;
double parentLeft = offset.X;
// Allow fallback to root (Host.Content)
double parentWidth = Application.Current.Host.Content.ActualWidth;
double parentHeight = Application.Current.Host.Content.ActualHeight;
if (_parent != null)
{
parentWidth = (parent.ActualWidth > 0) ? parent.ActualWidth : parentWidth;
parentHeight = (parent.ActualHeight > 0) ? parent.ActualHeight : parentHeight;
}
BusyIndicator.Measure(new System.Windows.Size(parentWidth, parentHeight));
var busyIndicatorSize = BusyIndicator.DesiredSize;
double indicatorWidth = (busyIndicatorSize.Width > 0) ? busyIndicatorSize.Width : DefaultBusyIndicatorWidth;
double indicatorHeight = (busyIndicatorSize.Height > 0) ? busyIndicatorSize.Height : DefaultBusyIndicatorHeight;
_containerPopup.HorizontalOffset = parentLeft + ((parentWidth / 2) - (indicatorWidth / 2));
_containerPopup.VerticalOffset = parentTop + ((parentHeight / 2) - (indicatorHeight / 2));
}
catch (Exception)
{
// Ignore
}
};
return;
}