locked
Get center of an image

    Question

  • Hello,

    I am currently working on a project and I am stuck.

    I have a bing maps displayed and onto it I have an image. This image is not in the map. My problem is I would like to get the center of the image (the point) to be able to use the function TryPixelToLocation. I didn't find a way to get the center as a point...

    Do you have any idea?

    Monday, June 30, 2014 11:12 PM

Answers

  • Hello LandM_2,

    Try this out. I haven't tested the code, so it might need a couple of modifications:

    // Declare and initialize an Image here
    Image myImage;
    
    // Transform coordinates from the image to the grid
    // myGrid = Grid containing the map
    GeneralTransform transform = myImage.TransformToVisual(myGrid);
    
    // Get the position of the image on the Grid. Divide the Width and Height by 2 to get the center of the image
    Point point = transform.TransformPoint(new Point((myImage.RenderSize.Width / 2), (myImage.RenderSize.Height / 2)));

    If you can replace a Grid with a Canvas, that might be easier, as you will be able to set the Left and Top properties of your image to position it on the Canvas. Getting the center of the image can then be done as follows:

    // Get the Left and Top of the image on the canvas
    double Left = (double)myImage.GetValue(Canvas.LeftProperty);
    double Top = (double)myImage.GetValue(Canvas.TopProperty);
    
    // Get the center X and Y points of the image on the canvas
    double centerX = (Left + (myImage.Width / 2));
    double centerY = (Top + (myImage.Height / 2));
    
    Point centerPos = new Point(centerX, centerY);

    I hope that helps!

    • Edited by Alexander Deeb Wednesday, July 2, 2014 6:06 AM
    • Marked as answer by LandM_2 Wednesday, July 2, 2014 6:30 PM
    Wednesday, July 2, 2014 6:00 AM

All replies

  • This isn't clear. Where is the image? How do you want to relate a location to a pixel on an image that isn't on a map and thus doesn't have a location related to it?

    http://rbrundritt.wordpress.com

    Tuesday, July 1, 2014 7:16 AM
  • Hi, sorry if I was not clear,

    the map is displayed on the background and we add an image on a grid wich is ontop of the map. Is it more clear?

    What I want to get, is the center (X,Y) of the image in regards with the grid.

    The map is not important for now.

    thank you for your time.

    Tuesday, July 1, 2014 4:11 PM
  • Hello LandM_2,

    Try this out. I haven't tested the code, so it might need a couple of modifications:

    // Declare and initialize an Image here
    Image myImage;
    
    // Transform coordinates from the image to the grid
    // myGrid = Grid containing the map
    GeneralTransform transform = myImage.TransformToVisual(myGrid);
    
    // Get the position of the image on the Grid. Divide the Width and Height by 2 to get the center of the image
    Point point = transform.TransformPoint(new Point((myImage.RenderSize.Width / 2), (myImage.RenderSize.Height / 2)));

    If you can replace a Grid with a Canvas, that might be easier, as you will be able to set the Left and Top properties of your image to position it on the Canvas. Getting the center of the image can then be done as follows:

    // Get the Left and Top of the image on the canvas
    double Left = (double)myImage.GetValue(Canvas.LeftProperty);
    double Top = (double)myImage.GetValue(Canvas.TopProperty);
    
    // Get the center X and Y points of the image on the canvas
    double centerX = (Left + (myImage.Width / 2));
    double centerY = (Top + (myImage.Height / 2));
    
    Point centerPos = new Point(centerX, centerY);

    I hope that helps!

    • Edited by Alexander Deeb Wednesday, July 2, 2014 6:06 AM
    • Marked as answer by LandM_2 Wednesday, July 2, 2014 6:30 PM
    Wednesday, July 2, 2014 6:00 AM
  • Hi,

    it worked! I used the first solution without modification.

    Thank you very much for your help!

    Wednesday, July 2, 2014 6:30 PM