I am taking a photo with the camera, using it as the background for an InkCanvas, and letting the user draw overtop.
When the user saves their drawing, I am scaling the drawing to match the size of the image in the background. I can not resize the original image.
However, while the length and position of the strokes are being scaled, the widths are not.
Scrolling through StackOverflow I have found people saying that you also need to set the scale on the InkStroke.DrawingAttributes.PenTipTransform
as well as the InkStroke.PointTransform
, but when I try to set the value to the scale, the value doesn't change.
Any information on how I can set this value, or another way to o the same thing would be greatly appreciated.
Thanks
UI Code
<!-- Canvas -->
<Image Grid.Row="1" x:Name="imgImage" Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center" SizeChanged="imgImage_SizeChanged"/>
<InkCanvas Grid.Row="1" x:Name="inkCanvas" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" PointerExited="inkCanvas_PointerExited" Visibility="Collapsed">
<InkCanvas.RenderTransform>
<ScaleTransform x:Name="inkCanvasScaleTransform" />
</InkCanvas.RenderTransform>
</InkCanvas>
EDIT - More information
The image taken by the camera has the resolution 3264x244, the user can then draw on the image with the canvas, but I can't show the entire image on the screen, so it gets scaled down to fit the size of the canvas. The canvas (in this particular case), has a resolution of 1012x759.
When the image is saved, the location of the draw gets scaled up to match the full size of the image. but the width of the stroke does not.
P.S. the scale transform in the XAML is currently not used.
SAVE CODE
StorageFile inputFile = null;
inputFile = await StorageFile.GetFileFromPathAsync(_image.fullPath);
height = (int)inkCanvas.ActualHeight;
width = (int)inkCanvas.ActualWidth;
double scalex = 0;
double scaley = 0;
double offsety = 0;
double offsetx = 0;
double widthscale = 1;
if (inputFile != null)
{
var prop = await inputFile.Properties.GetImagePropertiesAsync();
offsetx = (double)prop.Width - width;
offsety = (double)prop.Height - height;
scalex = ((double)prop.Width - width) / width;
scaley = ((double)prop.Height - height) / height;
width = (int)prop.Width;
height = (int)prop.Height;
widthscale = 1 + ((scalex * scaley) / 2);
}
var strokes = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();
List<InkStroke> ExpandedStrokes = new List<InkStroke>();
foreach (var stroke in strokes)
{
InkStroke strk = stroke.Clone();
Matrix3x2 scaleMatrix = Matrix3x2.CreateScale(1 + (float)scalex, 1 + (float)scaley);
strk.PointTransform = scaleMatrix;
strk.DrawingAttributes.PenTipTransform = scaleMatrix;
ExpandedStrokes.Add(strk);
}
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
CanvasDevice device = CanvasDevice.GetSharedDevice();
CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, width, height, 96);
using (var ds = renderTarget.CreateDrawingSession())
{
ds.Clear(Colors.White);
if (inputFile != null)
{
using (CanvasBitmap image = await CanvasBitmap.LoadAsync(device, inputFile.Path, 96))
{
ds.DrawImage(image);
}
}
ds.DrawInk(ExpandedStrokes);
}
using (var fileStream = await inputFile.OpenAsync(FileAccessMode.ReadWrite))
{
await renderTarget.SaveAsync(fileStream, CanvasBitmapFileFormat.Jpeg, 1f);
}
The scale for the PenTip may not be correct, but was just trying to get it making a change.
EXAMPLE IMAGES