本文整理汇总了C#中System.Windows.Media.Imaging.RenderTargetBitmap.Render方法的典型用法代码示例。如果您正苦于以下问题:C# RenderTargetBitmap.Render方法的具体用法?C# RenderTargetBitmap.Render怎么用?C# RenderTargetBitmap.Render使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Media.Imaging.RenderTargetBitmap
的用法示例。
在下文中一共展示了RenderTargetBitmap.Render方法的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: Create3DViewPort
private void Create3DViewPort()
{
var hvp3d = new HelixViewport3D();
Viewport3D vp3d = new Viewport3D();
var lights = new DefaultLights();
var tp = new Teapot();
hvp3d.Children.Add(lights);
hvp3d.Children.Add(tp);
vp3d = hvp3d.Viewport;
tata.Children.Add(vp3d); // comenter ca pour test
/* MEGA TEST DE L'ESPACE SUBSAHARIEN */
RenderTargetBitmap bmp = new RenderTargetBitmap(800, 800, 96, 96, PixelFormats.Pbgra32);
var rect = new Rect(0, 0, 800, 800);
vp3d.Measure(new Size(800, 800));
vp3d.Arrange(rect);
vp3d.InvalidateVisual();
bmp.Render(vp3d);
PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(bmp));
String filepath = "C:\\Users\\Remi\\Desktop\\canardmasque.png";
using (Stream stm = File.Create(filepath))
{
png.Save(stm);
}
}
示例4: getFeatures
private double[] getFeatures()
{
rtb = new RenderTargetBitmap((int)InkCanvas1.ActualWidth, (int)InkCanvas1.ActualHeight,
96d, 96d, PixelFormats.Default);
rtb.Render(InkCanvas1);
//save the ink to a memory stream
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
byte[] bitmapBytes;
Bitmap bmp;
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
ms.Position = 0;
bitmapBytes = ms.ToArray();
ms.Seek(0, SeekOrigin.Begin);
bmp = new Bitmap(ms);
}
bmp = resize.Apply(bmp);
double[] features = ToFeatures(bmp);
return features;
}
示例5: CreateScreenshot
/// <summary>Creates the screenshot of entire plotter element</summary>
/// <returns></returns>
internal static BitmapSource CreateScreenshot(UIElement uiElement, Int32Rect screenshotSource)
{
Window window = Window.GetWindow(uiElement);
if (window == null)
{
return CreateElementScreenshot(uiElement);
}
Size size = window.RenderSize;
//double dpiCoeff = 32 / SystemParameters.CursorWidth;
//int dpi = (int)(dpiCoeff * 96);
double dpiCoeff = 1;
int dpi = 96;
RenderTargetBitmap bmp = new RenderTargetBitmap(
(int)(size.Width * dpiCoeff), (int)(size.Height * dpiCoeff),
dpi, dpi, PixelFormats.Default);
// white background
Rectangle whiteRect = new Rectangle { Width = size.Width, Height = size.Height, Fill = Brushes.White };
whiteRect.Measure(size);
whiteRect.Arrange(new Rect(size));
bmp.Render(whiteRect);
// the very element
bmp.Render(uiElement);
CroppedBitmap croppedBmp = new CroppedBitmap(bmp, screenshotSource);
return croppedBmp;
}
示例6: RenderPages
/// <summary>
/// Generates an image of each page in the year book
/// and saves it to the src folder
/// </summary>
/// <param name="bv"></param>
/// <param name="folderloc"></param>
private static void RenderPages(BookViewer bv, string folderloc)
{
int currentpage = bv.ViewIndex;
//loops though each page
foreach (Page p in bv.CurrentBook.Pages)
{
bv.ViewIndex = p.PageNumber;
//forces the canvas to re-render
BookViewer.DesignerCanvas.UpdateLayout();
//takes a picture of the canvas
RenderTargetBitmap rtb = new RenderTargetBitmap(PaperSize.Pixel.PaperWidth, PaperSize.Pixel.PaperHeight, 96, 96, PixelFormats.Default);
rtb.Render(BookViewer.DesignerCanvas);
//getting the bleed margin
Int32Rect bleedmargin = new Int32Rect((PaperSize.Pixel.PaperWidth - PaperSize.Pixel.BleedWidth) / 2, (PaperSize.Pixel.PaperHeight - PaperSize.Pixel.BleedHeight) / 2, PaperSize.Pixel.BleedWidth, PaperSize.Pixel.BleedHeight);
//cropping the image
CroppedBitmap cb = new CroppedBitmap(rtb, bleedmargin);
//encodes the image in png format
PngBitmapEncoder pbe = new PngBitmapEncoder();
pbe.Frames.Add(BitmapFrame.Create(cb));
//saves the resulting image
FileStream fs = File.Open(folderloc + "\\src\\" + (p.PageNumber+1) + ".png", FileMode.Create);
pbe.Save(fs);
fs.Flush();
fs.Close();
}
bv.ViewIndex = currentpage;
}
示例7: GetImage
public static RenderTargetBitmap GetImage(UIElement fe, Brush background = null, Size sz = default(Size), int dpi = 144)
{
if (sz.Width < alib.Math.math.ε || sz.Height < alib.Math.math.ε)
{
fe.Measure(util.infinite_size);
sz = fe.DesiredSize; //VisualTreeHelper.GetContentBounds(fe).Size; //
}
DrawingVisual dv = new DrawingVisual();
RenderOptions.SetEdgeMode(dv, EdgeMode.Aliased);
using (DrawingContext ctx = dv.RenderOpen())
{
Rect r = new Rect(0, 0, sz.Width, sz.Height);
if (background != null)
ctx.DrawRectangle(background, null, r);
VisualBrush br = new VisualBrush(fe);
br.AutoLayoutContent = true;
ctx.DrawRectangle(br, null, r);
}
Double f = dpi / 96.0;
RenderTargetBitmap bitmap = new RenderTargetBitmap(
(int)(sz.Width * f) + 1,
(int)(sz.Height * f) + 1,
dpi,
dpi,
PixelFormats.Pbgra32);
bitmap.Render(dv);
return bitmap;
}
示例8: CaptureImage
public static Image CaptureImage(this FrameworkElement me, bool ensureSize = false)
{
//var width = ContentPanel.ActualWidth == 0 ? 1 : (int)ContentPanel.ActualWidth;
//var height = ContentPanel.ActualHeight == 0 ? 1 : (int)ContentPanel.ActualHeight;
var width = Convert.ToInt32(me.ActualWidth);
width = width == 0 ? 1 : width;
var height = Convert.ToInt32(me.ActualHeight);
height = height == 0 ? 1 : height;
// TODO: Multiplikation der DPI mit der aktuellen PresentationSource
var bmp = new RenderTargetBitmap(
width, height,
96, 96,
PixelFormats.Default);
bmp.Render(me);
var img = new Image
{
Source = bmp,
Stretch = Stretch.None,
Width = width - (ensureSize ? 1 : 0),
Height = height - (ensureSize ? 1 : 0)
};
//img.Measure(new Size(width, height));
//var imageSize = img.DesiredSize;
//img.Arrange(
// new Rect(new Point(0, 0), imageSize));
return img;
}
示例9: 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;
}
示例10: DrawGraphicsOnBitmap
public DrawGraphicsOnBitmap()
{
Title = "Draw Graphics on Bitmap";
// ��Ʈ���� ������� �����ϱ� ���� ����� ������.
Background = Brushes.Khaki;
// RenderTargetBitmap ������.
RenderTargetBitmap renderbitmap = new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Default);
// DrawingVisual ��ü�� ������.
DrawingVisual drawvis = new DrawingVisual();
DrawingContext dc = drawvis.RenderOpen(); // Render �ҵ� ȣ��
dc.DrawRoundedRectangle(Brushes.Blue, new Pen(Brushes.Red, 10),
new Rect(25, 25, 50, 50), 10, 10);
dc.Close();
// RenderTargetBitmap ���� DrawingVisual ��ü�� ��.
renderbitmap.Render(drawvis);
// �̹��� ��ä ���� -> ������Ƽ ����
Image img = new Image();
img.Source = renderbitmap;
// ���
Content = img;
}
示例11: Convert
public void Convert(IEnumerable<IAssetImageSource> sources, string target, VisualAssetInfo visualAsset)
{
if (sources == null)
{
throw new ArgumentNullException("sources");
}
if (string.IsNullOrEmpty(target))
{
throw new ArgumentNullException("target");
}
if (visualAsset == null)
{
throw new ArgumentNullException("visualAsset");
}
var rectangle = this.GetRectangleBounds(visualAsset.Width, visualAsset.Height, visualAsset.Margin);
var drawingVisual = this.CreateDrawingVisual(sources, rectangle);
var targetBitmap = new RenderTargetBitmap(visualAsset.Width, visualAsset.Height, 96, 96, PixelFormats.Pbgra32);
targetBitmap.Render(drawingVisual);
var png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(targetBitmap));
using (var stream = new FileStream(target, FileMode.Create, FileAccess.Write))
{
png.Save(stream);
}
}
示例12: 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;
}
示例13: 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);
}
示例14: GetNewMessagesNotificationOverlay
public static ImageSource GetNewMessagesNotificationOverlay(Window window, DataTemplate template, int count = 0)
{
if (window == null)
return null;
var presentation = PresentationSource.FromVisual(window);
if (presentation == null)
return null;
Matrix m = presentation.CompositionTarget.TransformToDevice;
double dx = m.M11;
double dy = m.M22;
double iconWidth = 16.0 * dx;
double iconHeight = 16.0 * dy;
string countText = count.ToString();
RenderTargetBitmap bmp = new RenderTargetBitmap((int) iconWidth, (int) iconHeight, 96, 96, PixelFormats.Default);
ContentControl root = new ContentControl
{
ContentTemplate = template,
Content = count > 99 ? "…" : countText
};
root.Arrange(new Rect(0, 0, iconWidth, iconHeight));
bmp.Render(root);
return bmp;
}
示例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;
}