HoneyBee
HoneyBee

Reputation: 11

How to calculate Scale and Offset when saving and loading InkCanvas Stroke of C# WPF?

<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>
// When the size at the time of saving is original and the current is current,
// If you simply apply the logic below An error occurs.
double scaleX = currentWidth / originalWidth;
double scaleY = currentHeight / originalHeight;

double newX = point.X * scaleX;
double newY = point.Y * scaleY;

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.

Upvotes: -2

Views: 90

Answers (1)

HoneyBee
HoneyBee

Reputation: 11

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;
    }

Upvotes: -1

Related Questions