本文整理汇总了C#中System.Windows.Media.DrawingVisual.RenderOpen方法的典型用法代码示例。如果您正苦于以下问题:C# DrawingVisual.RenderOpen方法的具体用法?C# DrawingVisual.RenderOpen怎么用?C# DrawingVisual.RenderOpen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Media.DrawingVisual
的用法示例。
在下文中一共展示了DrawingVisual.RenderOpen方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateBitmap
public void CreateBitmap()
{
int width = 100;
int height = 100;
int dpi = 96;
Tracing.Log(">> CreateBitmap");
var thread = new Thread(new ThreadStart(() =>
{
Tracing.Log(">> CreateBitmap - Thread start; creating drawing visual");
//Dispatcher.Invoke(new Action(() => {
_drawingVisual = new DrawingVisual();
_drawingContext = _drawingVisual.RenderOpen();
//}));
Tracing.Log(">> CreateBitmap - Drawing to context");
_drawingContext.DrawRectangle(new SolidColorBrush(Colors.HotPink), new Pen(), new Rect(0, 0, 50, 50));
_drawingContext.DrawRectangle(new SolidColorBrush(Colors.Blue), new Pen(), new Rect(50, 0, 50, 50));
_drawingContext.DrawRectangle(new SolidColorBrush(Colors.Orange), new Pen(), new Rect(0, 50, 50, 50));
_drawingContext.DrawRectangle(new SolidColorBrush(Colors.DarkRed), new Pen(), new Rect(50, 50, 50, 50));
_drawingContext.Close();
Tracing.Log(">> CreateBitmap - Finished drawing; creating render target bitmap");
_bitmap = new RenderTargetBitmap(width, height, dpi, dpi, PixelFormats.Default);
_bitmap.Render(_drawingVisual);
Tracing.Log(">> CreateBitmap - Finished work");
_bitmap.Freeze();
}));
//thread.IsBackground = true;
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
示例2: ButtonSave_Click
private void ButtonSave_Click(object sender, RoutedEventArgs e)
{
var rect = new Rect { Width = 512, Height = 384 };
var dv = new DrawingVisual();
var dc = dv.RenderOpen();
dc.PushTransform(new TranslateTransform(-rect.X, -rect.Y));
dc.DrawRectangle(InkCanvasMain.Background, null, rect);
InkCanvasMain.Strokes.Draw(dc);
dc.Close();
var rtb = new RenderTargetBitmap((int)rect.Width, (int)rect.Height, 96, 96, PixelFormats.Default);
rtb.Render(dv);
var enc = new PngBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(rtb));
var fn = TextBoxFileName.Text;
if (!fn.EndsWith(".png", StringComparison.OrdinalIgnoreCase)) fn += ".png";
using (Stream s = File.Create(TegakiImageFolder + "/" + fn))
{
enc.Save(s);
}
((TegakiWindowViewModel)DataContext).AddToMediaList(System.IO.Path.GetFullPath(TegakiImageFolder + "/" + fn));
Close();
}
示例3: FeedbackColorFrame
/// <summary>
/// Draw feedback ColorFrame in a correct pixel format (Pbgra32)
/// </summary>
/// <param name="colorFrame">Byte array corresponding to the pixel data</param>
/// <param name="nWidthImage">Image Width</param>
/// <param name="nHeightImage">Image Height</param>
/// <returns></returns>
public static byte[] FeedbackColorFrame(byte[] colorFrame, int nWidthImage, int nHeightImage)
{
try
{
// Create BitmapSource with the data pixel (pixel format : Bgr32)
BitmapSource sourceColor = BitmapSource.Create(nWidthImage, nHeightImage, 96, 96,
PixelFormats.Bgr32, null, colorFrame, nWidthImage * 4);
DrawingVisual refDrawingVisual = new DrawingVisual();
// Draw the BitmapSource in DrawingVisual
using (DrawingContext dc = refDrawingVisual.RenderOpen())
{
dc.DrawImage(sourceColor, new Rect(0.0, 0.0, nWidthImage, nHeightImage));
DrawKinectModeOnStream(dc);
}
// Convert the pixel format to the Pbgra32
var rtb = new RenderTargetBitmap(nWidthImage, nHeightImage, 96d, 96d, PixelFormats.Pbgra32);
rtb.Render(refDrawingVisual);
// return a byte array with a correct pixel format
return ConvertBitmapToByteArray(rtb, nWidthImage, nHeightImage);
}
catch (Exception ex)
{
DebugLog.DebugTraceLog("Error reading color frame " + ex.ToString(), true);
return null;
}
}
示例4: XamlDrawingToPngBase64String
/// <summary>
/// Преобразует XAML Drawing/DrawingGroup в png base64 string
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="drawing"></param>
/// <returns>Base64 string containing png bitmap</returns>
public static string XamlDrawingToPngBase64String(int width, int height, Drawing drawing) {
var bitmapEncoder = new PngBitmapEncoder();
// The image parameters...
double dpiX = 96;
double dpiY = 96;
// The Visual to use as the source of the RenderTargetBitmap.
var drawingVisual = new DrawingVisual();
using (var drawingContext = drawingVisual.RenderOpen()) {
drawingContext.DrawDrawing(drawing);
}
var bounds = drawingVisual.ContentBounds;
var targetBitmap = new RenderTargetBitmap(
width * 10, height * 10, dpiX, dpiY,
PixelFormats.Pbgra32);
drawingVisual.Transform = new ScaleTransform(width * 10 / bounds.Width, height * 10 / bounds.Height);
targetBitmap.Render(drawingVisual);
// Encoding the RenderBitmapTarget as an image.
bitmapEncoder.Frames.Add(BitmapFrame.Create(targetBitmap));
byte[] values;
using (var str = new MemoryStream()) {
bitmapEncoder.Save(str);
values = str.ToArray();
}
return Convert.ToBase64String(values);
}
示例5: GetScreenShot
public static byte[] GetScreenShot(this UIElement source, double scale, int quality)
{
double renderHeight = source.RenderSize.Height * scale;
double renderWidth = source.RenderSize.Width * scale;
RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96, PixelFormats.Pbgra32);
VisualBrush sourceBrush = new VisualBrush(source);
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
using (drawingContext)
{
drawingContext.PushTransform(new ScaleTransform(scale, scale));
drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(source.RenderSize.Width, source.RenderSize.Height)));
}
renderTarget.Render(drawingVisual);
JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
jpgEncoder.QualityLevel = quality;
jpgEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
Byte[] image;
using (MemoryStream outputStream = new MemoryStream())
{
jpgEncoder.Save(outputStream);
image = outputStream.ToArray();
}
return image;
}
示例6: CreateImage
protected override sealed void CreateImage(ImageGenerationContext context)
{
base.CreateImage(context);
Rect bounds = new Rect(StrokeWidth / 2, StrokeWidth / 2,
CalculatedWidth - StrokeWidth,
CalculatedHeight - StrokeWidth);
Brush brush = Fill.GetBrush();
Pen pen = GetPen();
DrawingVisual dv = new DrawingVisual();
DrawingContext dc = dv.RenderOpen();
if (Roundness == 0)
dc.DrawRectangle(brush, pen, bounds);
else
dc.DrawRoundedRectangle(brush, pen, bounds, Roundness, Roundness);
dc.Close();
RenderTargetBitmap rtb = RenderTargetBitmapUtility.CreateRenderTargetBitmap(CalculatedWidth, CalculatedHeight);
rtb.Render(dv);
Bitmap = new FastBitmap(rtb);
}
示例7: Init_DrawLT
public void Init_DrawLT(int running_train)
{
int i;
int temp;
int late_time_range;
late_time_range = 1000;
temp = 0;
x1 = 30;
y1 = 10;
x2 = 700;
y2 = 580;
narrow_x = (double)running_train / (x2 - x1);
narrow_y = (double)late_time_range / (y2 - y1);
drawingVisual = new DrawingVisual();
dc = drawingVisual.RenderOpen();
dc.DrawLine(new Pen(Brushes.Black, 1.5), new Point(x1, x1), new Point(x1, y2));
dc.DrawLine(new Pen(Brushes.Black, 1.5), new Point(x1, y2), new Point(x2, y2));
dc.DrawText(new FormattedText("N",
CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface("Verdana"),
12, System.Windows.Media.Brushes.Black),
new System.Windows.Point(x2 - 30, y2 + 6));
dc.DrawText(new FormattedText("T",
CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface("Verdana"),
12, System.Windows.Media.Brushes.Black),
new System.Windows.Point(x1 - 10, y1 - 8));
for (i = 0; i < (x2 - x1); i++)
{
if (i % ((x2 - x1) / 5) == 0)
{
dc.DrawLine(new Pen(Brushes.Black, 1.5), new Point(x1 + i, y2), new Point(x1 + i, y2 - 3));
dc.DrawText(new FormattedText((temp * running_train / 5).ToString(),
CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface("Verdana"),
12, System.Windows.Media.Brushes.Black),
new System.Windows.Point(i + x1 - 5, y2 + 7));
temp++;
}
}
temp = 0;
for (i = 0; i < (y2 - y1); i++)
{
if (i % ((y2 - y1) / 5) == 0)
{
dc.DrawLine(new Pen(Brushes.Black, 1.5), new Point(x1, y2 - i), new Point(x1 + 3, y2 - i));
dc.DrawText(new FormattedText((temp * late_time_range / 5).ToString(),
CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface("Verdana"),
12, System.Windows.Media.Brushes.Black),
new System.Windows.Point(x1 - 28, y2 - i - 9));
temp++;
}
}
}
示例8: SetPreviewElement
public void SetPreviewElement(FrameworkElement element)
{
double x = 0;
double y = 0;
GetCurrentDPI(out x, out y);
// Does the DrawingBrush thing seem a little hacky? Yeah, it's a necessary hack. See:
// http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=364041
Rect bounds = VisualTreeHelper.GetDescendantBounds(element);
RenderTargetBitmap r = new RenderTargetBitmap((int)(element.ActualWidth * x / 96), (int)(element.ActualHeight * y / 96), (int)x, (int)y, PixelFormats.Pbgra32);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext ctx = dv.RenderOpen()) {
VisualBrush vb = new VisualBrush(element);
ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
}
r.Render(dv);
DragImage = r;
InvalidateMeasure();
}
示例9: DrawContextualMenu
internal static void DrawContextualMenu(DrawingVisual drawingVisual, Dictionary<int, string> itemsList)
{
//draw the stuff
//3 cases
//menu items == 7
//menu items < 7
//menu items > 7
DrawingContext context = drawingVisual.RenderOpen();
int itemsCount = itemsList.Count;
if (itemsCount == 7)
{
}
if (itemsCount < 7)
{
}
if (itemsCount > 7)
{
}
// position the menu correctly
}
示例10: WaveGraph
public WaveGraph()
{
InitializeComponent();
DrawingVisual ghostVisual;
Width = 300;
Height = 350;
ghostVisual = new DrawingVisual();
using (DrawingContext dc = ghostVisual.RenderOpen())
{
dc.DrawEllipse(Brushes.Black, new Pen(Brushes.Red, 10),
new Point(95, 95), 15, 15);
dc.DrawEllipse(Brushes.Black, new Pen(Brushes.Red, 10),
new Point(170, 105), 15, 15);
Pen p = new Pen(Brushes.Black, 10);
p.StartLineCap = PenLineCap.Round;
p.EndLineCap = PenLineCap.Round;
dc.DrawLine(p, new Point(75, 160), new Point(175, 150));
}
this.AddVisualChild(ghostVisual);
this.AddLogicalChild(ghostVisual);
}
示例11: RenderMyVisual
private void RenderMyVisual(DrawingVisual v)
{
using (var dc = v.RenderOpen())
{
RenderRectangles(dc);
}
}
示例12: CreateNodeVisual
private DrawingVisual CreateNodeVisual(VisibilityType visibility, bool IsHighlighted,
bool IsPathHighlighted, bool IsSelected, bool UsedInConstruction)
{
var visual = new DrawingVisual();
using (var context = visual.RenderOpen())
{
// Draw the actual circle
var size = this.GetSize(IsHighlighted, IsPathHighlighted, IsSelected, UsedInConstruction);
switch (visibility)
{
case VisibilityType.Important:
context.DrawEllipse(
this.GetFillBrush(IsSelected, UsedInConstruction),
this.GetStrokePen(IsHighlighted, IsPathHighlighted, IsSelected, UsedInConstruction),
new Point(0, 0), size.Width / 2, size.Height / 2);
break;
case VisibilityType.ShowWhenNecessary:
if ((IsPathHighlighted) || (IsSelected)
|| (IsHighlighted) || (UsedInConstruction))
{
context.DrawEllipse(
this.GetFillBrush(IsSelected, UsedInConstruction),
this.GetStrokePen(IsHighlighted, IsPathHighlighted, IsSelected, UsedInConstruction),
new Point(0, 0), size.Width / 2, size.Height / 2);
}
break;
}
}
return visual;
}
示例13: RenderToBitmap
/// <summary>
///
/// </summary>
/// <param name="geometry"></param>
/// <param name="bitmapSize"></param>
/// <param name="geometryPositionOnBitmap"></param>
/// <param name="brush">The Brush with which to fill the Geometry. This is optional, and can be null. If the brush is null, no fill is drawn.</param>
/// <param name="pen">The Pen with which to stroke the Geometry. This is optional, and can be null. If the pen is null, no stroke is drawn</param>
/// <param name="pixelFormat"></param>
/// <param name="dpiX"></param>
/// <param name="dpiY"></param>
/// <returns></returns>
public static BitmapSource RenderToBitmap(
this Geometry geometry,
Size bitmapSize,
Point geometryPositionOnBitmap,
Size geometrySize,
Brush brush,
Pen pen,
PixelFormat pixelFormat,
double dpiX = 96.0,
double dpiY = 96.0)
{
var rtb = new RenderTargetBitmap((int)(bitmapSize.Width * dpiX / 96.0),
(int)(bitmapSize.Height * dpiY / 96.0),
dpiX,
dpiY,
pixelFormat);
var dv = new DrawingVisual();
using (var cx = dv.RenderOpen())
{
cx.Render(geometry, geometryPositionOnBitmap, brush, pen);
}
rtb.Render(dv);
return rtb;
}
示例14: OnSelectTileImageSourceCommand
private void OnSelectTileImageSourceCommand()
{
var openFileService = this.GetDependencyResolver().Resolve<IOpenFileService>();
openFileService.Filter = Constants.SupportedImageSourceFilter;
openFileService.CheckFileExists = true;
if (openFileService.DetermineFile())
{
var imageSource = new BitmapImage(new Uri(openFileService.FileName)) { CacheOption = BitmapCacheOption.OnLoad };
if (imageSource.PixelWidth != Model.TileWidth || imageSource.PixelHeight != Model.TileHeight)
{
var messageService = this.GetDependencyResolver().Resolve<IMessageService>();
messageService.ShowWarningAsync($"图片分辨率必须为 {Model.TileWidth}x{Model.TileHeight}。");
return;
}
if (imageSource.DpiX != 96 || imageSource.DpiY != 96)
{
var drawing = new DrawingVisual();
using (var context = drawing.RenderOpen())
{
context.DrawImage(imageSource, new Rect(0, 0, 60, 30));
}
var bitmap = new RenderTargetBitmap(60, 30, 96, 96, PixelFormats.Pbgra32);
bitmap.Render(drawing);
TileImageSource = imageSource;
return;
}
TileImageSource = imageSource;
}
}
示例15: GetJpgImage
/// <summary>
/// </summary>
/// <param name="source"> </param>
/// <param name="scale"> </param>
/// <param name="quality"> </param>
/// <returns> </returns>
public static byte[] GetJpgImage(UIElement source, double scale, int quality)
{
var actualHeight = source.RenderSize.Height;
var actualWidth = source.RenderSize.Width;
var renderHeight = actualHeight * scale;
var renderWidth = actualWidth * scale;
var renderTarget = new RenderTargetBitmap((int) renderWidth, (int) renderHeight, 96, 96, PixelFormats.Pbgra32);
var sourceBrush = new VisualBrush(source);
var drawingVisual = new DrawingVisual();
var drawingContext = drawingVisual.RenderOpen();
using (drawingContext)
{
drawingContext.PushTransform(new ScaleTransform(scale, scale));
drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(actualWidth, actualHeight)));
}
renderTarget.Render(drawingVisual);
var jpgEncoder = new JpegBitmapEncoder
{
QualityLevel = quality
};
jpgEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
Byte[] imageArray;
using (var outputStream = new MemoryStream())
{
jpgEncoder.Save(outputStream);
imageArray = outputStream.ToArray();
}
return imageArray;
}