本文整理汇总了C#中CanvasRenderTarget.SaveAsync方法的典型用法代码示例。如果您正苦于以下问题:C# CanvasRenderTarget.SaveAsync方法的具体用法?C# CanvasRenderTarget.SaveAsync怎么用?C# CanvasRenderTarget.SaveAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CanvasRenderTarget
的用法示例。
在下文中一共展示了CanvasRenderTarget.SaveAsync方法的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: MultithreadedSaveToFile
public void MultithreadedSaveToFile()
{
//
// This test can create a deadlock if the SaveAsync code holds on to the
// ID2D1Multithread resource lock longer than necessary.
//
// A previous implementation waited until destruction of the IAsyncAction before calling Leave().
// In managed code this can happen on an arbitrary thread and so could result in deadlock situations
// where other worker threads were waiting for a Leave on the original thread that never arrived.
//
using (new DisableDebugLayer()) // 6184116 causes the debug layer to fail when CanvasBitmap::SaveAsync is called
{
var task = Task.Run(async () =>
{
var device = new CanvasDevice();
var rt = new CanvasRenderTarget(device, 16, 16, 96);
var filename = Path.Combine(ApplicationData.Current.TemporaryFolder.Path, "testfile.jpg");
await rt.SaveAsync(filename);
rt.Dispose();
});
task.Wait();
}
}
示例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: 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)}");
}
}
}
示例5: 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;
}
}
示例6: MultithreadedSaveToStream
public void MultithreadedSaveToStream()
{
// This is the stream version of the above test
var task = Task.Run(async () =>
{
var device = new CanvasDevice();
var rt = new CanvasRenderTarget(device, 16, 16, 96);
var stream = new MemoryStream();
await rt.SaveAsync(stream.AsRandomAccessStream(), CanvasBitmapFileFormat.Bmp);
rt.Dispose();
});
task.Wait();
}
示例7: 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;
}
示例8: 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);
}
}
示例9: MultithreadedSaveToStream
public void MultithreadedSaveToStream()
{
// This is the stream version of the above test
using (new DisableDebugLayer()) // 6184116 causes the debug layer to fail when CanvasBitmap::SaveAsync is called
{
var task = Task.Run(async () =>
{
var device = new CanvasDevice();
var rt = new CanvasRenderTarget(device, 16, 16, 96);
var stream = new MemoryStream();
await rt.SaveAsync(stream.AsRandomAccessStream(), CanvasBitmapFileFormat.Bmp);
rt.Dispose();
});
task.Wait();
}
}
示例10: 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();
}
}
示例11: GenerateFromString
public static IRandomAccessStream GenerateFromString(string str, string fontFamily, Color color)
{
float size = 512;
using (var device = new CanvasDevice())
using (var renderTarget = new CanvasRenderTarget(device, size, size, 96))
{
using (var ds = renderTarget.CreateDrawingSession())
{
ds.DrawText(str, size / 2, size / 2, color,
new CanvasTextFormat()
{
FontFamily = fontFamily,
FontSize = size / 2,
HorizontalAlignment = CanvasHorizontalAlignment.Center,
VerticalAlignment = CanvasVerticalAlignment.Center
});
}
InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
renderTarget.SaveAsync(stream, CanvasBitmapFileFormat.Png).AsTask().Wait();
return stream;
}
}
示例12: GenerateAndSaveTileImage
static async Task<string> GenerateAndSaveTileImage(CanvasDevice device, float width, float height)
{
using (var renderTarget = new CanvasRenderTarget(device, width, height, 96))
{
using (var ds = renderTarget.CreateDrawingSession())
{
DrawTile(ds, width, height);
}
var filename = string.Format("tile-{0}x{1}-{2}.png",
(int)width,
(int)height,
Guid.NewGuid().ToString());
await renderTarget.SaveAsync(Path.Combine(ApplicationData.Current.LocalFolder.Path, filename));
return filename;
}
}
示例13: PlayBackImage_ImageOpened
private async void PlayBackImage_ImageOpened(IRandomAccessStream stream)
{
if (CurrentSong != null)
{
{
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
{
Source = bitmap
};
blur.BlurAmount = 16.0f;
blur.BorderMode = EffectBorderMode.Hard;
ds.DrawImage(blur);
}
stream.Seek(0);
await renderer.SaveAsync(stream, CanvasBitmapFileFormat.Png);
stream.Seek(0);
BitmapImage image = new BitmapImage();
image.SetSource(stream);
BackgroundBlur.Source = image;
renderer = null;
bitmap = null;
device = null;
GC.Collect();
}
}
}
示例14: 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);
}
}
}
示例15: GenerateIcon
async Task GenerateIcon(AppInfo appInfo, IconInfo iconInfo, StorageFolder folder)
{
// Draw the icon image into a command list.
var commandList = new CanvasCommandList(device);
using (var ds = commandList.CreateDrawingSession())
{
appInfo.DrawIconImage(ds, iconInfo);
}
ICanvasImage iconImage = commandList;
// Rasterize into a rendertarget.
var renderTarget = new CanvasRenderTarget(device, iconInfo.Width, iconInfo.Height, 96);
using (var ds = renderTarget.CreateDrawingSession())
{
// Initialize with the appropriate background color.
ds.Clear(iconInfo.TransparentBackground ? Colors.Transparent : appInfo.BackgroundColor);
// Work out where to position the icon image.
var imageBounds = iconImage.GetBounds(ds);
imageBounds.Height *= 1 + iconInfo.BottomPadding;
float scaleUpTheSmallerIcons = Math.Max(1, 1 + (60f - iconInfo.Width) / 50f);
float imageScale = appInfo.ImageScale * scaleUpTheSmallerIcons;
var transform = Matrix3x2.CreateTranslation((float)-imageBounds.X, (float)-imageBounds.Y) *
Utils.GetDisplayTransform(renderTarget.Size.ToVector2(), new Vector2((float)imageBounds.Width, (float)imageBounds.Height)) *
Matrix3x2.CreateScale(imageScale, renderTarget.Size.ToVector2() / 2);
if (iconInfo.Monochrome)
{
// Optionally convert to monochrome.
iconImage = new DiscreteTransferEffect
{
Source = new Transform2DEffect
{
Source = new LuminanceToAlphaEffect { Source = iconImage },
TransformMatrix = transform
},
RedTable = new float[] { 1 },
GreenTable = new float[] { 1 },
BlueTable = new float[] { 1 },
AlphaTable = new float[] { 0, 1 }
};
}
else
{
ds.Transform = transform;
// Optional shadow effect.
if (appInfo.AddShadow)
{
var shadow = new ShadowEffect
{
Source = iconImage,
BlurAmount = 12,
};
ds.DrawImage(shadow);
}
}
// draw the main icon image.
ds.DrawImage(iconImage);
}
// Save to a file.
using (var stream = await folder.OpenStreamForWriteAsync(iconInfo.Filename, CreationCollisionOption.ReplaceExisting))
{
await renderTarget.SaveAsync(stream.AsRandomAccessStream(), CanvasBitmapFileFormat.Png);
}
}