简体   繁体   中英

WPF Canvas Add img to Canvas give unnecessary magin

Hi when i try to put image to canvas using Label as container, I got TOP + 4 Left + 4 out of nowhere. I did send the image to = 0 left = 0 and got it on top=4 left = 4); Please Help. thx

XAML:

<Grid   >

    <Canvas Name="TWCanvas"  Margin="0,0,0,0" Height="1000" Width="1200"/>
</Grid>

Code:

Public void PutImg(string path)
    {
        Image img = new Image();
        img.Width = 100;
        img.Height = 100;
        img.Source = new BitmapImage(new Uri(path, UriKind.Relative));
        img.Stretch = Stretch.Fill;
        Label lbl = new Label();
        lbl.Content = img;
        lbl.Margin = new Thickness(0, 0, 0, 0);

        Canvas.SetLeft(lbl, 0);
        Canvas.SetTop(lbl, 0);

        TWCanvas.Children.Add(lbl);

    }

What you see is the Label's Padding (an internal margin). Either set it to zero

lbl.Padding = new Thickness();

or drop the (apparently redundant) Label control and add the Image directly to your Canvas:

Canvas.SetLeft(img, 0);
Canvas.SetTop(img, 0);
TWCanvas.Children.Add(img);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM