本文整理汇总了C#中System.Windows.Controls.Control.Measure方法的典型用法代码示例。如果您正苦于以下问题:C# Control.Measure方法的具体用法?C# Control.Measure怎么用?C# Control.Measure使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Controls.Control
的用法示例。
在下文中一共展示了Control.Measure方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ScreenCapture
public static void ScreenCapture(Control control, string filename)
{
try
{
// The BitmapSource that is rendered with a Visual.
control.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
Size size = control.DesiredSize;
int width = (int) size.Width;
int height = (int) size.Height;
control.Arrange(new Rect(0, 0, width, height));
var rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
rtb.Render(control);
// Encoding the RenderBitmapTarget as a PNG file.
var png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
using (Stream stm = File.Create(filename))
{
png.Save(stm);
}
}
finally
{
}
}
示例2: getImageFromControl
/// <summary>
/// Convert any control to a PngBitmapEncoder
/// </summary>
/// <param name="controlToConvert">The control to convert to an ImageSource</param>
/// <returns>The returned ImageSource of the controlToConvert</returns>
private static PngBitmapEncoder getImageFromControl(Control controlToConvert)
{
// save current canvas transform
Transform transform = controlToConvert.LayoutTransform;
// get size of control
Size sizeOfControl = new Size(controlToConvert.ActualWidth, controlToConvert.ActualHeight);
// measure and arrange the control
controlToConvert.Measure(sizeOfControl);
// arrange the surface
controlToConvert.Arrange(new Rect(sizeOfControl));
// craete and render surface and push bitmap to it
RenderTargetBitmap renderBitmap = new RenderTargetBitmap((Int32)sizeOfControl.Width, (Int32)sizeOfControl.Height, 96d, 96d, PixelFormats.Pbgra32);
// now render surface to bitmap
renderBitmap.Render(controlToConvert);
// encode png data
PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
// puch rendered bitmap into it
pngEncoder.Frames.Add(BitmapFrame.Create(renderBitmap));
// return encoder
return pngEncoder;
}