.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > control Image and PictureBox.location, WPF, C#
 

control Image and PictureBox.location, WPF, C#

hi,
I have to transform a line of code from winform to WPF: pic.Location = new System.Drawing.Point(x, y);
in winforms "pic" is a picturebox, in WPF i used a control "Image" but when I get "pic.Location" it signals an error.
I precise too that System.Drawing don't operate with WPF but I think that that is not a problem.
any one have a solution to propose?
lindsay05
The Image class does not contain a property called Image.Location.

Please refer to this page for all of the available properties, functions and events which can be used with Image:


The reason that Location does not exist is because of the way that WPF is used to create layout. The image can sit within any number of different containers (Window, Grid, StackPanel, etc..)

If you're looking to duplicate the previous functionality than you might want to look into putting your Image within a Canvas and then specify the Image's position within the Canvas like so:

        // Create a canvas sized to fill the window
        Canvas myCanvas = new Canvas();

        // Add image to the Canvas
        Image img1 = new Image();
        Canvas.SetTop(img1, 100);
        Canvas.SetLeft(img1, 10);
        myCanvas.Children.Add(img1);

This code creates a new Image object and set's it's x (Canvas.SetLeft) and y (Canvas.SetTop) prior to adding it to the Canvas.
  • Proposed As Answer byDeviantSeev Thursday, September 24, 2009 5:47 PM
  •  
DeviantSeev
ok, but my objective is to deplace this image, so I want to delate my image in the last position and then display it in the new position...etc. and with the solution you gave me the image is displayed on the first position an on the second (and third,...) position.
lindsay05
Hello Lindsay05,

After we add the Image to a Canvas Control, we can change the Canvas.Top Attach property.

For example:

Canvas myCanvas = new Canvas();

// Add image to the Canvas
Image img1 = new Image();
Canvas.SetTop(img1, 100);
Canvas.SetLeft(img1, 10);

myCanvas.Children.Add(img1);

//Contiune to change the location on a Canvas.

Canvas.SetTop(img1, 260);
Canvas.SetLeft(img1, 100);

Hope we talk the same thing.

Thanks.

Please mark the replies as answers if they help and unmark them if they provide no help
  • Proposed As Answer byDeviantSeev Thursday, September 24, 2009 5:47 PM
  •  
Hua Chen

You can use google to search for other answers

Custom Search

More Threads

• How to sort items in a ListBox independent of the order of the ItemsSource?
• Display Canvas as Thumbnail
• Adding resources to visual tree of a page for use in the designer
• Textblock - Wrap And Trim
• TextBox, ScrollToEnd only if not scrolled up?
• Creating Animated GIF files in WPF
• ListBoxItem text with ellipsis?
• Setting Content property in a Style?
• changing style of CheckBoxes in XAML
• Using TextBox KeyDown Event