How should I do the Scale operation in the process of saving and loading the InkCanvas Stroke?

HoneyBee 186 Reputation points
2023-08-24T06:58:35.12+00:00
<InkCanvas x:Name="CurrentPage" PreviewMouseDown="CurrentPage_PreviewMouseDown" PreviewMouseUp="CurrentPage_PreviewMouseUp">
<InkCanvas.Background>
    <ImageBrush x:Name="CurrentPageImage" Stretch="Uniform" ImageSource="{Binding CurrentTextBook}" />
</InkCanvas.Background>
<InkCanvas.RenderTransform>
    <TransformGroup>
        <ScaleTransform x:Name="scaleTransform" ScaleX="1" ScaleY="1" />
        <TranslateTransform x:Name="CurrentPageTranslateTransform" X="0" Y="0"/>
    </TransformGroup>
</InkCanvas.RenderTransform>

I am working with InkCanvas in C# WPF.

I am using a specific image for the Background Image property of my InkCanvas, and the Stretch of this image is Uniform.

As a result, I am working on drawing a Stroke on the image.

But now a problem arose.

After working the program in window mode, save the stroke If you call Stroke in full screen mode The location and size do not match.

Even with a simple scale calculation method, the position and size of the stroke do not match.

How should I solve it?

I have the total size at the time of saving and the size at the time of loading.

I am also working on the same image. I am working with InkCanvas in C# WPF.

I am using a specific image for the Background Image property of my InkCanvas, and the Stretch of this image is Uniform.

As a result, I am working on drawing a Stroke on the image.

But now a problem arose.

After working the program in window mode, save the stroke If you call Stroke in full screen mode The location and size do not match.

Even with a simple scale calculation method, the position and size of the stroke do not match.

How should I solve it?

I have the total size at the time of saving and the size at the time of loading.

I am also working on the same image.

Developer technologies | Windows Presentation Foundation
{count} votes

1 answer

Sort by: Most helpful
  1. HoneyBee 186 Reputation points
    2023-08-24T11:03:25.85+00:00

    Hi, @Hui Liu-MSFT .

    I've solved the problem.

    We modified the way Scale is calculated, so it works fine in any case.

    I share the code I used.

    public void SetFullPageStrokes(StrokeCollection[] strokes, double originalWidth, double originalHeight, double currentWidth, double currentHeight)
            {
                if (strokes == null)
                    return;
    
                double scaleX = currentWidth / originalWidth;
                double scaleY = currentHeight / originalHeight;
    
                double scale = originalWidth > currentWidth ? Math.Max(scaleX, scaleY) : Math.Min(scaleX, scaleY);
    
                double displayedImageWidth = originalWidth * scale;
                double displayedImageHeight = originalHeight * scale;
                double offsetX = (currentWidth - displayedImageWidth) / 2;
                double offsetY = (currentHeight - displayedImageHeight) / 2;
    
                var matrix = Matrix.Identity;
                matrix.Scale(scale, scale);
                matrix.Translate(offsetX, offsetY);
    
                foreach (var loadedStrokes in strokes)
                {
                    if (loadedStrokes != null)
                    {
                        foreach (Stroke stroke in loadedStrokes)
                        {
                            stroke.Transform(matrix, false);
                        }
                    }
                }
    
                PageStrokes = strokes;
            }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.