本文整理汇总了C#中System.Windows.Media.Imaging.RenderTargetBitmap类的典型用法代码示例。如果您正苦于以下问题:C# RenderTargetBitmap类的具体用法?C# RenderTargetBitmap怎么用?C# RenderTargetBitmap使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RenderTargetBitmap类属于System.Windows.Media.Imaging命名空间,在下文中一共展示了RenderTargetBitmap类的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: 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);
}
}
示例3: 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();
}
示例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: CreateCursor
public static Cursor CreateCursor(UIElement element, int xHotSpot,
int yHotSpot)
{
element.Measure(new Size(double.PositiveInfinity,
double.PositiveInfinity));
element.Arrange(new Rect(0, 0, element.DesiredSize.Width,
element.DesiredSize.Height));
RenderTargetBitmap rtb =
new RenderTargetBitmap((int)element.DesiredSize.Width,
(int)element.DesiredSize.Height, 96, 96, PixelFormats.Pbgra32);
rtb.Render(element);
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
MemoryStream ms = new MemoryStream();
encoder.Save(ms);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);
ms.Close();
ms.Dispose();
Cursor cur = InternalCreateCursor(bmp, xHotSpot, yHotSpot);
bmp.Dispose();
return cur;
}
示例6: 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);
}
}
示例7: Convert
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// Converts a Canvas to an image source
var canvas = value as Canvas;
if (canvas == null)
{
return null;
}
var param = parameter as CanvasToImageSourceParameters;
double width = canvas.Width;
double height = canvas.Height;
if (param != null)
{
width = param.Width;
height = param.Height;
}
if (!canvas.IsMeasureValid)
{
canvas.Measure(new Size(width, height));
canvas.Arrange(new Rect(0, 0, width, height));
canvas.UpdateLayout();
}
var rtb = new RenderTargetBitmap((int)width, (int)height, 96d, 96d, PixelFormats.Pbgra32);
rtb.Render(canvas);
var bmp = GetBitmap(rtb);
return rtb;
}
示例8: 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;
}
示例9: 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);
}
示例10: CreatePdfDocument
public Document CreatePdfDocument(Chart chart, string path)
{
RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
(int) chart.ActualWidth,
(int) chart.ActualHeight,
96d,
96d,
PixelFormats.Pbgra32);
renderBitmap.Render(chart);
MemoryStream stream = new MemoryStream();
BitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
encoder.Save(stream);
Bitmap bitmap = new Bitmap(stream);
System.Drawing.Image image = bitmap;
System.Drawing.Image resizedImage = ResizeImage(image, image.Width*2, image.Height);
Document doc = new Document(PageSize.A4);
PdfWriter.GetInstance(doc, new FileStream(path, FileMode.OpenOrCreate));
doc.Open();
Image pdfImage = Image.GetInstance(resizedImage, ImageFormat.Jpeg);
doc.Add(pdfImage);
doc.Close();
return doc;
}
示例11: OnUpdate
protected override void OnUpdate(double[] values)
{
const double space = 3;
const double barwidth = 8;
int pts = values.Length / 2;
int width = 1000;//pts * (space + barwidth) - space;
int height = width / 4;
if (_bmp == null || width != _bmp.PixelWidth)
{
_bmp = new RenderTargetBitmap(width, height, 120, 96, PixelFormats.Pbgra32);
}
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
Brush brush = DrawingBrush.Clone();
brush.Freeze();
double totalWidth = width / pts;
double totalBarWidth = totalWidth * ((barwidth) / (barwidth + space));
double totalSpace = totalWidth * ((space) / (barwidth + space));
for (int i = 0; i < pts; i++)
{
double x = i * totalWidth;
double y1 = height;
double y2 = y1 - values[i] * height;
drawingContext.DrawRectangle(brush, null, new Rect(x, y2, totalBarWidth, y1));
}
drawingContext.Close();
_bmp.Clear();
_bmp.Render(drawingVisual);
PART_visualationDisplay.Source = _bmp;
}
示例12: 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;
}
示例13: ButtonBase_OnClick
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var bitmap = new RenderTargetBitmap((int)Math.Ceiling(Grid.RenderSize.Width), (int)Math.Ceiling(Grid.RenderSize.Height), 96, 96, PixelFormats.Default);
bitmap.Render(Grid);
Grid.Effect = new CircleRevealTransitionEffect
{
OldImage = new ImageBrush(bitmap),
Reverse = !ToggleButton.IsChecked.Value
};
TaskbarItemInfo = ToggleButton.IsChecked == true
? new TaskbarItemInfo {ProgressState = TaskbarItemProgressState.Indeterminate}
: new TaskbarItemInfo {ProgressState = TaskbarItemProgressState.None};
Application.Current.Resources["Dunno"] = ToggleButton.IsChecked == true ? Application.Current.Resources["Dunno2"] : Application.Current.Resources["Dunno1"];
Grid.Background = ToggleButton.IsChecked == true ? Brushes.Transparent : _background;
OnAirImage.Visibility = ToggleButton.IsChecked == true ? Visibility.Visible : Visibility.Collapsed;
_compositeNodeViewModel.ResetNodes();
_sw = Stopwatch.StartNew();
_timer?.Stop();
_timer = new DispatcherTimer(TimeSpan.FromMilliseconds(10), DispatcherPriority.Normal, Callback, Dispatcher);
}
示例14: ExportToPng
public static byte[] ExportToPng(this FrameworkElement surface)
{
var transform = surface.LayoutTransform;
surface.LayoutTransform = null;
var size = new Size(surface.ActualWidth, surface.ActualHeight);
surface.Measure(size);
surface.Arrange(new Rect(size));
var renderBitmap = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96d, 96d, PixelFormats.Pbgra32);
renderBitmap.Render(surface);
try
{
using (var outStream = new MemoryStream())
{
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
encoder.Save(outStream);
return outStream.ToArray();
}
}
finally
{
surface.LayoutTransform = transform;
}
}
示例15: GetJpgImage
///
/// Gets a JPG "screenshot" of the current UIElement
///
/// UIElement to screenshot
/// Scale to render the screenshot
/// JPG Quality
/// Byte array of JPG data
public byte[] GetJpgImage(double scale, int quality, int dpi)
{
double actualHeight = this.RootVisual.RenderSize.Height;
double actualWidth = this.RootVisual.RenderSize.Width;
double renderHeight = actualHeight * scale;
double renderWidth = actualWidth * scale;
RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)renderWidth, (int)renderHeight, dpi, dpi, PixelFormats.Pbgra32);
VisualBrush sourceBrush = new VisualBrush(this.RootVisual);
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(actualWidth, actualHeight)));
}
renderTarget.Render(drawingVisual);
JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
jpgEncoder.QualityLevel = quality;
jpgEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
Byte[] _imageArray;
using (MemoryStream outputStream = new MemoryStream())
{
jpgEncoder.Save(outputStream);
_imageArray = outputStream.ToArray();
}
return _imageArray;
}