In WPF, how do you get the dimensions of an element on an InkCanvas?

First, thank you for your time.

I have created a wpf desktop applciation.  On the view there is an InkCanvas control.  After a png file is loaded into the canvas the user has the ability to modify this image in numerous ways.  After the user colors, resizes and moves the image around, the user then clicks the Save button.  The save button should not save the entire contents of the canvas but just the modified image that was placed on the canvas.  In order for the code to just save a small specific rectangle of the canvas, I need to know where the image is and its dimensions.  I can successfully get the width and height of the image, but I can't seem to get the top and left coordinates of the image on the canvas.

Here is the Save method:
private void Save_Click(object sender, RoutedEventArgs e)
{
    var filename = General.PromptForSaveFilename();
    if (filename != null)
    {
         foreach (Image child in inkCanvas.Children)
         {
               if (child.Source != null && child.Source.ToString().Contains(".png"))
               {
                     var element = (UIElement) child;
                     element.Focus();
                     double top = 0.0D, right = 0.0D, bottom = 0.0D, left = 0.0D;

                     GetElementDimensions(element, out top, out left, out bottom, out right);
                     // The value for left and top are 0.0!  This is the problem.
                }
         }
    }
}

// Return the dimensions of this element.  This routine is wrong!  The top and left values are 0!?
private static void GetElementDimensions(UIElement element, out double top, out double left, out double bottom, out double right)
{
    top = InkCanvas.GetTop(element);
    left = InkCanvas.GetLeft(element);
    bottom = InkCanvas.GetBottom(element);
    right = InkCanvas.GetRight(element);

    // It's possible that some of the values are NaN or infinity.  Reset those values to 0.
    EnsureReasonableValue(ref top);
    EnsureReasonableValue(ref left);
    EnsureReasonableValue(ref bottom);
    EnsureReasonableValue(ref right);
}

// If the incoming value is not reasonable, i.e. NaN or Infinity, then set it to 0.0; otherwise leave it alone.
private static void EnsureReasonableValue(ref double value)
{
    if (!IsFinite(value))
        value = 0.0D;
}

// Return true if the incoming value is reasonable (not infinity); otherwise return false. 
public static bool IsFinite(double value)
{
    return !double.IsNaN(value) && !double.IsInfinity(value);
}

Open in new window

 

Again, thank you for your time.
MichaelDavidCarrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sytech SolutionsCommented:
Hi Michael

Are you using a RenderTransform to move the image on the InkCanvas? If so this is applied after the layout which would mean InkCanvas.Top and Left would be NaN if not explicitly set when the image is loaded. Assuming this is the case the following code worked for me:

<InkCanvas x:Name="inkCanvas">
    <Image Source="Image.png" Width="100" Height="100">
        <Image.RenderTransform>
            <TranslateTransform X="40" Y="50" />
        </Image.RenderTransform>
    </Image>
</InkCanvas>

Open in new window


FrameworkElement element = (FrameworkElement)inkCanvas.Children[0];
TranslateTransform translateTransform = (TranslateTransform)element.RenderTransform;
double translateX = translateTransform.X;
double translateY = translateTransform.Y;
double width = element.ActualWidth;
double height = element.ActualHeight;

Open in new window



Let me know if that helps?

Thanks
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.