代码之家  ›  专栏  ›  技术社区  ›  JoanComasFdz

wpf如何将图像居中。source

  •  6
  • JoanComasFdz  · 技术社区  · 14 年前

    我正在WPF.NET 3.5和Visual Studio 2010中开发自定义图像控件。

    在WiFras中 皮卡盒 控件具有 西佐莫德 财产包括“ 中心图像 “。

    我希望我的图像控制有这种能力。

    还有吗?

    谢谢

    我的XAML代码:

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="768" Width="1024" xmlns:my="http://schemas.sharpsoft.net/xaml" xmlns:my1="clr-namespace:WpfApplication1">
        <Grid>
            <my1:CustomControl1
                        x:Name="customControl11"
                        Width="206"
                        Height="197"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Top"
                        Margin="18,58,0,0"
                        Stretch="Uniform"/>
        </Grid>
    </Window>
    

    我的自定义控件代码:

    public class CustomControl1 : Image
    {
        public CustomControl1()
        {
            // Bitmap to Stream
            Stream ms = new MemoryStream();
            Properties.Resources.webcam_background.Save(ms, ImageFormat.Png);
    
            // Stream to BitmapImage
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.StreamSource = ms;
            bitmap.EndInit();
    
            // Set it
            Source = bitmap;
        }
    }
    

    其中“Webcam背景”是默认的Visual Studio资源编辑器添加的PNG图像。

    4 回复  |  直到 7 年前
        1
  •  2
  •   Quartermeister    14 年前

    集合 Stretch 一点也没有。

    <Image Source="..." Stretch="None" />
    
        2
  •  5
  •   decyclone    14 年前

    你应该试着集中整个 Image 使用对齐的元素本身:

        <Grid>
            <Image Stretch="None"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center" />
        </Grid>
    
        3
  •  0
  •   Cobalt Scales    7 年前

    这是我的工作解决方案:

    <Image Name="PreviewImage" 
           HorizontalAlignment="Stretch" 
           Stretch="Uniform" 
           VerticalAlignment="Top" 
           Width="456"  
           Height="256"/>
    
        4
  •  -2
  •   MDaldoss    12 年前

    只需在窗口上给出一个x:name,并检索有关其维度的信息。

    <Window x:Name="mainWindowContainer"
        x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="768" Width="1024" xmlns:my="http://schemas.sharpsoft.net
        [...]
    </Window>
    

    然后从代码中回忆窗口实际大小的信息。然后,因为您有宿主大小和对象大小,所以这只是一个数学问题。在“framewidth”的背景周围说明帧大小,即图像的尺寸(destwidth、destheight),您可以设置:

            int wOffset = (((int)mainWindowContainer.ActualWidth - frameWidth * 2) - destWidth) / 2 + frameWidth;
            int hOffset = (((int)mainWindowContainer.ActualHeight - frameWidth * 2) - destHeight) / 2 + frameWidth;
    
            Canvas.SetLeft(image, wOffset);
            Canvas.SetTop(image, hOffset);