本文整理汇总了C#中CanvasRenderTarget.CreateDrawingSession方法的典型用法代码示例。如果您正苦于以下问题:C# CanvasRenderTarget.CreateDrawingSession方法的具体用法?C# CanvasRenderTarget.CreateDrawingSession怎么用?C# CanvasRenderTarget.CreateDrawingSession使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CanvasRenderTarget
的用法示例。
在下文中一共展示了CanvasRenderTarget.CreateDrawingSession方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Button_Click
private async void Button_Click(object sender, RoutedEventArgs e)
{
using (var stream = await Root.RenderToRandomAccessStream())
{
var device = new CanvasDevice();
var bitmap = await CanvasBitmap.LoadAsync(device, stream);
var renderer = new CanvasRenderTarget(device, bitmap.SizeInPixels.Width, bitmap.SizeInPixels.Height, bitmap.Dpi);
using (var ds = renderer.CreateDrawingSession())
{
var blur = new GaussianBlurEffect();
blur.BlurAmount = 5.0f;
blur.Source = bitmap;
ds.DrawImage(blur);
}
stream.Seek(0);
await renderer.SaveAsync(stream, CanvasBitmapFileFormat.Png);
BitmapImage image = new BitmapImage();
image.SetSource(stream);
Blured.Source = image;
}
}
示例2: ToQrDataUri
public async static Task<Uri> ToQrDataUri(this ISdp sdp, int width, int height)
{
var qrCodeWriter = new QRCodeWriter();
var bitMatrix = qrCodeWriter.encode(sdp.ToString(), ZXing.BarcodeFormat.QR_CODE, width, height);
using (var canvasRenderTarget = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), 500, 500, 96))
{
using (var drawingSession = canvasRenderTarget.CreateDrawingSession())
{
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
drawingSession.DrawRectangle(x, y, 1, 1, bitMatrix.get(x, y) ? Color.FromArgb(0, 0, 0, 0) : Color.FromArgb(255, 255, 255, 255));
}
}
}
using (var inMemoryRandomAccessStream = new InMemoryRandomAccessStream())
{
await canvasRenderTarget.SaveAsync(inMemoryRandomAccessStream, CanvasBitmapFileFormat.Png);
inMemoryRandomAccessStream.Seek(0);
var buffer = new byte[inMemoryRandomAccessStream.Size];
await inMemoryRandomAccessStream.ReadAsync(buffer.AsBuffer(), (uint)inMemoryRandomAccessStream.Size, InputStreamOptions.None);
return new Uri($"data:image/png;base64,{Convert.ToBase64String(buffer)}");
}
}
}
示例3: DrawStrokeOnImageBackground
public byte[] DrawStrokeOnImageBackground(IReadOnlyList<InkStroke> strokes, byte[] backgroundImageBuffer)
{
var stmbuffer = new InMemoryRandomAccessStream();
stmbuffer.AsStreamForWrite().AsOutputStream().WriteAsync(backgroundImageBuffer.AsBuffer()).AsTask().Wait();
CanvasDevice device = CanvasDevice.GetSharedDevice();
var canbit = CanvasBitmap.LoadAsync(device, stmbuffer, 96).AsTask().Result;
CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, canbit.SizeInPixels.Width, canbit.SizeInPixels.Height, 96);
using (var ds = renderTarget.CreateDrawingSession())
{
ds.Clear(Colors.Transparent);
if (backgroundImageBuffer != null)
{
ds.DrawImage(canbit);
}
ds.DrawInk(strokes);
}
var stm = new InMemoryRandomAccessStream();
renderTarget.SaveAsync(stm, CanvasBitmapFileFormat.Png).AsTask().Wait();
var readfrom = stm.GetInputStreamAt(0).AsStreamForRead();
var ms = new MemoryStream();
readfrom.CopyTo(ms);
var outputBuffer = ms.ToArray();
return outputBuffer;
}
示例4: Save
public async Task Save(StorageFile file)
{
var image = GetImage();
// Measure the extent of the image (which may be cropped).
Rect imageBounds;
using (var commandList = new CanvasCommandList(sourceBitmap.Device))
using (var drawingSession = commandList.CreateDrawingSession())
{
imageBounds = image.GetBounds(drawingSession);
}
// Rasterize the image into a rendertarget.
using (var renderTarget = new CanvasRenderTarget(sourceBitmap.Device, (float)imageBounds.Width, (float)imageBounds.Height, 96))
{
using (var drawingSession = renderTarget.CreateDrawingSession())
{
drawingSession.Blend = CanvasBlend.Copy;
drawingSession.DrawImage(image, -(float)imageBounds.X, -(float)imageBounds.Y);
}
// Save it out.
var format = file.FileType.Equals(".png", StringComparison.OrdinalIgnoreCase) ? CanvasBitmapFileFormat.Png : CanvasBitmapFileFormat.Jpeg;
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
stream.Size = 0;
await renderTarget.SaveAsync(stream, format);
}
}
}
示例5: Process
public void Process(CanvasBitmap input, CanvasRenderTarget output, TimeSpan time)
{
using (CanvasDrawingSession session = output.CreateDrawingSession())
{
session.DrawImage(input);
session.DrawText("Canvas Effect test", 0f, 0f, Colors.Red);
}
}
示例6: drawText
public void drawText(CanvasRenderTarget crt, Color color)
{
using (CanvasDrawingSession ds = crt.CreateDrawingSession())
{
ds.DrawTextLayout(textLayout, (float)location.X, (float)location.Y, color);
}
}
示例7: BlurElementAsync
/// <summary>
/// Applys a blur to a UI element
/// </summary>
/// <param name="sourceElement">UIElement to blur, generally an Image control, but can be anything</param>
/// <param name="blurAmount">Level of blur to apply</param>
/// <returns>Blurred UIElement as BitmapImage</returns>
public static async Task<BitmapImage> BlurElementAsync(this UIElement sourceElement, float blurAmount = 2.0f)
{
if (sourceElement == null)
return null;
var rtb = new RenderTargetBitmap();
await rtb.RenderAsync(sourceElement);
var buffer = await rtb.GetPixelsAsync();
var array = buffer.ToArray();
var displayInformation = DisplayInformation.GetForCurrentView();
using (var stream = new InMemoryRandomAccessStream())
{
var pngEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
pngEncoder.SetPixelData(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Premultiplied,
(uint) rtb.PixelWidth,
(uint) rtb.PixelHeight,
displayInformation.RawDpiX,
displayInformation.RawDpiY,
array);
await pngEncoder.FlushAsync();
stream.Seek(0);
var canvasDevice = new CanvasDevice();
var bitmap = await CanvasBitmap.LoadAsync(canvasDevice, stream);
var renderer = new CanvasRenderTarget(canvasDevice,
bitmap.SizeInPixels.Width,
bitmap.SizeInPixels.Height,
bitmap.Dpi);
using (var ds = renderer.CreateDrawingSession())
{
var blur = new GaussianBlurEffect
{
BlurAmount = blurAmount,
Source = bitmap
};
ds.DrawImage(blur);
}
stream.Seek(0);
await renderer.SaveAsync(stream, CanvasBitmapFileFormat.Png);
var image = new BitmapImage();
await image.SetSourceAsync(stream);
return image;
}
}
示例8: ImageInfo
public ImageInfo(CanvasRenderTarget image)
{
_crt = image;
BitmapSize bsize = image.SizeInPixels;
Size size = image.Size;
_offset = new Point(size.Width / 2.0f, size.Height / 2.0f);
using (CanvasDrawingSession ds = image.CreateDrawingSession())
{
_symbolBounds = image.GetBounds(ds);
_imageBounds = ShapeUtilities.clone(_symbolBounds);
}
}
示例9: Process
public void Process(CanvasBitmap input, CanvasRenderTarget output, TimeSpan time)
{
using (CanvasDrawingSession session = output.CreateDrawingSession())
{
session.DrawImage(input);
session.FillCircle(
(float)input.Bounds.Width / 2,
(float)input.Bounds.Height / 2,
(float)(Math.Min(input.Bounds.Width, input.Bounds.Height) / 2 * Math.Cos(2 * Math.PI * time.TotalSeconds)),
Colors.Aqua
);
}
}
示例10: CreateParticleBitmaps
//Creates textures of different sizes
public void CreateParticleBitmaps(bool blur = false)
{
SetColors(baseColor);
particleBitmaps = new CanvasRenderTarget[sizes];
int i = -1;
var nextRadius = 0;
var nextSize =0;
var transparent = Color.FromArgb(0, 0, 0, 0);
float viewportsize = 100; //Here is the trick, if this value is too small appears the displacement and the original image
for (int r = 1; r < sizes + 1; r += 1)
{
nextRadius = (r * minRadius);
nextSize = nextRadius * 2;
CanvasRenderTarget canvas = new CanvasRenderTarget(device, viewportsize, viewportsize, parent.Dpi);
var center = new Vector2((viewportsize - nextRadius) / 2);
//The following is like a 'drawing graph', the output of the first is the input of the second one;
using (CanvasDrawingSession targetSession = canvas.CreateDrawingSession())
{
targetSession.Clear(transparent);
targetSession.FillCircle(center, nextRadius, outerColor);
targetSession.FillCircle(center, nextRadius - 6, innerColor);
}
if (!blur)
{
particleBitmaps[++i] = canvas;
}
else //Add blur just one time
{
var blurEffect = new GaussianBlurEffect() { BlurAmount = 2f };
CanvasRenderTarget blurredcanvas = new CanvasRenderTarget(device, viewportsize, viewportsize, parent.Dpi);
blurEffect.Source = canvas;
using (CanvasDrawingSession targetSession = blurredcanvas.CreateDrawingSession())
{
targetSession.Clear(transparent);
targetSession.DrawImage(blurEffect);
}
particleBitmaps[++i] = blurredcanvas;
}
}
}
示例11: RenderTargetDpiTest
public void RenderTargetDpiTest()
{
const float defaultDpi = 96;
const float highDpi = defaultDpi * 2;
const float size = 100;
const float fractionalSize = 100.8f;
var device = new CanvasDevice();
var renderTargetDefault = new CanvasRenderTarget(device, size, size, defaultDpi);
var renderTargetHigh = new CanvasRenderTarget(device, size, size, highDpi);
var renderTargetFractionalSize = new CanvasRenderTarget(device, fractionalSize, fractionalSize, highDpi);
// Check each rendertarget reports the expected DPI.
Assert.AreEqual(defaultDpi, renderTargetDefault.Dpi);
Assert.AreEqual(highDpi, renderTargetHigh.Dpi);
Assert.AreEqual(highDpi, renderTargetFractionalSize.Dpi);
// Check each rendertarget is of the expected size.
Assert.AreEqual(size, renderTargetDefault.Size.Width);
Assert.AreEqual(size, renderTargetDefault.Size.Height);
Assert.AreEqual(size, renderTargetHigh.Size.Width);
Assert.AreEqual(size, renderTargetHigh.Size.Height);
Assert.AreEqual(Math.Round(fractionalSize), renderTargetFractionalSize.Size.Width);
Assert.AreEqual(Math.Round(fractionalSize), renderTargetFractionalSize.Size.Height);
// Check sizes in pixels.
Assert.AreEqual(size, renderTargetDefault.SizeInPixels.Width);
Assert.AreEqual(size, renderTargetDefault.SizeInPixels.Height);
Assert.AreEqual(size * highDpi / defaultDpi, renderTargetHigh.SizeInPixels.Width);
Assert.AreEqual(size * highDpi / defaultDpi, renderTargetHigh.SizeInPixels.Height);
Assert.AreEqual(Math.Round(fractionalSize * highDpi / defaultDpi), renderTargetFractionalSize.SizeInPixels.Width);
Assert.AreEqual(Math.Round(fractionalSize * highDpi / defaultDpi), renderTargetFractionalSize.SizeInPixels.Height);
// Check that drawing sessions inherit the DPI of the rendertarget being drawn onto.
var drawingSessionDefault = renderTargetDefault.CreateDrawingSession();
var drawingSessionHigh = renderTargetHigh.CreateDrawingSession();
Assert.AreEqual(defaultDpi, drawingSessionDefault.Dpi);
Assert.AreEqual(highDpi, drawingSessionHigh.Dpi);
}
示例12: DrawStrokeOnSolidColorBackground
public byte[] DrawStrokeOnSolidColorBackground(IReadOnlyList<InkStroke> strokes, int width, int height, Color color )
{
CanvasDevice device = CanvasDevice.GetSharedDevice();
CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, width, height, 96);
using (var ds = renderTarget.CreateDrawingSession())
{
ds.Clear(color);
ds.DrawInk(strokes);
}
var stm = new InMemoryRandomAccessStream();
renderTarget.SaveAsync(stm, CanvasBitmapFileFormat.Png).AsTask().Wait();
var readfrom = stm.GetInputStreamAt(0).AsStreamForRead();
var ms = new MemoryStream();
readfrom.CopyTo(ms);
var outputBuffer = ms.ToArray();
return outputBuffer;
}
示例13: BtnSave_Click
private async void BtnSave_Click(object sender, RoutedEventArgs e)
{
StorageFolder storageFolder = KnownFolders.SavedPictures;
var file = await storageFolder.CreateFileAsync("sample.jpg", CreationCollisionOption.ReplaceExisting);
CanvasDevice device = CanvasDevice.GetSharedDevice();
CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, (int)MyInkCanvas.ActualWidth, (int)MyInkCanvas.ActualHeight, 96);
using (var ds = renderTarget.CreateDrawingSession())
{
ds.Clear(Colors.White);
ds.DrawInk(MyInkCanvas.InkPresenter.StrokeContainer.GetStrokes());
}
using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
await renderTarget.SaveAsync(fileStream, CanvasBitmapFileFormat.Jpeg, 1f);
}
}
示例14: DownloadPng
private static async Task<CanvasBitmap> DownloadPng(CanvasDevice device, Uri uri)
{
try
{
return await CanvasBitmap.LoadAsync(device, await CachedData.GetRandomAccessStreamAsync(uri));
}
catch (FileNotFoundException)
{
var rt = new CanvasRenderTarget(device, 480, 360, 96);
using (var ds = rt.CreateDrawingSession())
{
ds.Clear(Colors.Transparent);
ds.DrawLine(0, 0, (float)rt.Size.Width, (float)rt.Size.Height, Colors.Black, 1);
ds.DrawLine(0, (float)rt.Size.Height, (float)rt.Size.Width, 0, Colors.Black, 1);
}
return rt;
}
}
示例15: RendererImage
private static void RendererImage(Stream stream, SvgImageRendererSettings settings)
{
var svg = settings.Document;
var viewPort = svg.RootElement.ViewPort;
if (!viewPort.HasValue) throw new ArgumentException(nameof(settings));
var width = viewPort.Value.Width;
var height = viewPort.Value.Height;
var device = CanvasDevice.GetSharedDevice();
using (var offScreen = new CanvasRenderTarget(device, width, height, settings.Scaling * 96.0F))
{
using (var renderer = new Win2dRenderer(offScreen, svg))
using (var session = offScreen.CreateDrawingSession())
{
session.Clear(Colors.Transparent);
renderer.Render(width, height, session);
}
offScreen.SaveAsync(stream.AsRandomAccessStream(), (CanvasBitmapFileFormat)settings.Format, settings.Quality).AsTask().GetAwaiter().GetResult();
}
}