How to render InkCanvas to an image in UWP Windows 10 application? - c#

The RenderTargetBitmap class worked with simple Canvas + InkManager (in Windows 8.1) to render ink strokes to an image.
UWP introduced InkCanvas and a new Inking API. However, it seems like the RenderTargetBitmap does not work with that. When I try to capture ink strokes with RenderAsync method, no ink strokes get rendered only other objects like Rectangle and so on.
Is it a bug or this new API is not meant to be used this way? If not, then how can I render an image out of InkCanvas?
Thanks!

Here is how I solved this issue with Win2D.
First of all, add Win2D.uwp nuget package to your project.
Then use this code:
CanvasDevice device = CanvasDevice.GetSharedDevice();
CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, (int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight, 96);
using (var ds = renderTarget.CreateDrawingSession())
{
ds.Clear(Colors.White);
ds.DrawInk(inkCanvas.InkPresenter.StrokeContainer.GetStrokes());
}
using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
await renderTarget.SaveAsync(fileStream, CanvasBitmapFileFormat.Jpeg, 1f);

Please try InkPresenter.StrokeContainer property
http://blogs.windows.com/buildingapps/2015/09/08/going-beyond-keyboard-mouse-and-touch-with-natural-input-10-by-10/

Related

Image smoothing in UWP app

I have a small UWP application that load same image in two ways:
var folder = Package.Current.InstalledLocation;
var file = await folder.GetFileAsync("TestImage.png");
var bitmapImage1 = new BitmapImage();
using (var stream = await file.OpenAsync(FileAccessMode.Read))
{
await bitmapImage1.SetSourceAsync(stream);
}
var bitmapImage2 = new BitmapImage(new Uri(BaseUri, "TestImage.png"));
_image.Source = bitmapImage1;
//_image.Source = bitmapImage2;
The problem is that Image control shows the same image in different ways. For bitmapImage1 image is not smoothed, but for bitmapImage2 it's ok. How it looks like. I need to do some manipulation with image before show (change some pixels) but I also need to have smoothed image after that. Could you please help me with it?
I also used WriteableBitmap to change some pixels and have same result (not smoothed). Looks like I need to tell to Image control how to draw this.
Here is link to project for more information
I tested your code on my side. And you will found if you don't set the Height and Width properties for the image control, the pictures will looks all the same. So the reason for one looks smooth but another looks not is the image control scaling the picture. Without scaling the picture will not looks different. I guess this may be leaded but difference between vector and bitmap image.
You should be able to resolve it by create a smooth picture with the size you just want to show in the app, and set the image control to the same size with the picture or without setting the Height and Width properties, this may resolved.
My result:
I've got solution on another thread.
If change order of code lines it helps:
var folder = Package.Current.InstalledLocation;
var file = await folder.GetFileAsync("TestImage.png");
var bitmapImage1 = new BitmapImage();
_image.Source = bitmapImage1;
using (var stream = await file.OpenAsync(FileAccessMode.Read))
{
await bitmapImage1.SetSourceAsync(stream);
}
So we should assign Image source and than set source stream to Bitmap.
It seems, Image control decodes the picture quite flexibly so that it can suit for acutal UI size. If you set the BitmapImage to Image.Source first, the picture is to be smoothed properly like the second one.

Lumia imaging crop filter

Hi so I'm making an app for Windows 10 that requires a user to choose an image and it's going to crop the image to 310*128. I got the file picker code already. But I want to know how to actually crop and save the image and display in image box. I already have the xaml page done
With Lumia Imaging SDK you can both crop the selected image and resize the image. In this answer I assume you actually want to crop, but from the text I could just as well guess you really just want to resize.
For crop, use the CropEffect from Lumia.Imaging.Transforms. Set the CropArea property on it to the object, and then render it. If you are rendering straight to the XAML page I recommend using a SwapChainPanel object in XAML and a SwapChainPanelRenderer to render on it.
Given that you are loading a StorageFile and rendering to a SwapChainPanel your code might look like something like this:
StorageFile file = ...
using (var source = new StorageFileImageSource(file))
using (var crop = new CropEffect(source, new Rect(0, 0, 310, 128))
using (var renderer = new SwapChainPanelRenderer(crop, YourSwapChainPanel))
{
await renderer.RenderAsync();
}

Capturing images with blended images in nokia imaging sdk

I am making windows phone app using nokia imaging sdk and example of app is this real time blend demo
I am trying to capture image with Image overlayed image i.e image with other image in top of it as in above example in live camera stream below is code i am trying to capture image with effect
CameraCaptureSequence cameraCaptureSequence = App.Camera.CreateCaptureSequence(1);
MemoryStream stream = new MemoryStream();
cameraCaptureSequence.Frames[0].CaptureStream = stream.AsOutputStream();
await App.Camera.PrepareCaptureSequenceAsync(cameraCaptureSequence);
await cameraCaptureSequence.StartCaptureAsync();
stream.Seek(0, SeekOrigin.Begin);
MediaLibrary library = new MediaLibrary();
library.SavePictureToCameraRoll("picture1.jpg", stream);
but the above code only saves image without effect, so how to capture images with live blended effects from camera.
Basically what you have to do is attach the same effects/filters that you had in the preview to a new image source taking the captured photo stream instead. And probably use a different renderer too.
Either that or set up a duplicate set of filters for the capture. There are reasons to, you could e.g. configure lower quality effects in the preview to help performance.

C#, Emgu webcam - choose capture size

I'm using the Emgu library for integrating the open CV webcam features in C#.
I use this code for choosing the capture device and setting its size:
camera = new Capture(0);
camera.SetCaptureProperty(CAP_PROP.CV_CAP_PROP_FRAME_WIDTH, videoSettings.width);
camera.SetCaptureProperty(CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT, videoSettings.height);
Then I display it in an imageBox like this: imageBox1.Image = camera.QueryFrame();
Then to capture a snapshot of the current frame I use this code:
Image<Bgr, byte> snapshot = camera.QueryFrame();
snapshot.Save("snapshot.jpg");
Though I would want to be able to save the snapshot at a higher resolution than the preview window.
But the problem is that as far as I know I can't create a new "Capture" object using the same webcamera. So I'm wondering if it is maybe possible to set the camera.setCaptureProperty height and width to let's say 1028x720 but then in some way crop it for displaying it in the imageBox with the resolution of 514x360?
Or is there any other way to do this?
I solved this by using
imageBox1.SizeMode = PictureBoxSizeMode.StretchImage;
I solved this by using the Resize() method in QueryFrame()
currentFrame = grabber.QueryFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);

How to export Image of my form c#

I need a solution how could i make a Print Screen of my WinForm on C# and export it as PNG.
Bests
I think this blog post will help.
using (Bitmap bitmap = new Bitmap(ParentForm.Size.Width, ParentForm.Size.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(new Point(ParentForm.DesktopLocation.X, ParentForm.DesktopLocation.Y), new Point(0, 0), ParentForm.Size);
}
bitmap.Save(#"C:\test.jpg", ImageFormat.Jpeg);
}
Never tried it, but i'd think you should be able to call OnPaint(args) with a PaintEventArgs you create, that includes a Graphics for the image you want to draw on, and the ClipRectangle encompassing the whole area of the form.
This would only work if your form properly processes paint messages (ie: if it stores enough info to be able to repaint the window fully at will), and it may only get the client area (ie: it might not get the title bar or menus).

Categories