【问题标题】:Loading a large image to a WPF app - Scrolling and Drawing both needed将大图像加载到 WPF 应用程序 - 滚动和绘图都需要
【发布时间】:2013-03-29 21:59:35
【问题描述】:

我知道如何只做其中一个请求,而不是两个。

为了加载大图像并能够滚动查看其所有部分,我将图像添加到 <ScrollViewer/> 中并得到了我需要的东西。

为了在图像上绘图,我能够将该图像加载为<Canvas/> 的背景。

但是如何做到这两点呢?

基本上,我有一个我不想缩小的非常大的图像,并且该图像上绘制了一堆房间(例如建筑物的平面图)。我有房间的坐标,当用户在房间内点击时,我想用颜色填充那个房间(目前我只有一个显示“房间 1 已被点击”的列表框)。

已编辑的 XAML(用于缩小图像):

    <Grid Margin="10">
    <Grid.RowDefinitions>
        <RowDefinition Height="30" />
        <RowDefinition Height="*" />
        <RowDefinition Height="200" />
    </Grid.RowDefinitions>

    <ComboBox x:Name="buildingCombo" Grid.Row="0" ItemContainerStyle="{DynamicResource ComboItemStyle}" Tag="{Binding}"
              Template="{StaticResource ComboBoxTemplate}"  Width="1000" SelectionChanged="buildingCombo_SelectionChanged"/>

    <Grid Grid.Row="1">

        <ScrollViewer  Grid.Column="0"  HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <Canvas Width="{Binding ActualWidth, ElementName=image}" Height="{Binding ActualHeight, ElementName=floorPlanImage}" >
                <Canvas.Background>
                    <VisualBrush  >
                        <VisualBrush.Visual>
                            <Image Grid.Column="0" Stretch="None" x:Name="floorPlanImage" MouseLeftButtonDown="Image_MouseLeftButtonDown"/>
                        </VisualBrush.Visual>
                    </VisualBrush>
                </Canvas.Background>
            </Canvas>
        </ScrollViewer>
    </Grid>

    <DataGrid Grid.Row="2" ColumnHeaderStyle="{DynamicResource GridViewColumnHeaderStyle}" Background="LightGray" RowBackground="LightYellow" AlternatingRowBackground="LightBlue" 
              x:Name="dataGridViewRoomQuery"  BorderBrush="Gray" BorderThickness="5"/>

</Grid>

【问题讨论】:

  • 你需要手绘吗?
  • 不,只是以编程方式用纯色填充一个矩形
  • 那你想要InkCanvas 做什么?我不明白。
  • 也许 InkCanvas 不是我需要的 .. Canvas 可以吗?
  • 我不确定,我不完全了解您需要什么。可以发截图吗?如果 StackOverflow 中没有,请尝试 imgur.com。另外,您目前如何确定“room1 已被点击”?

标签: wpf image canvas scrollviewer


【解决方案1】:

您可以将Canvas 的大小设置为您设置为Background 的Image 的大小,这将允许您使用ScrollViewer 并在设置时保持正确的Image 大小作为CanvasBackground

例子:

<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
    <Canvas Width="{Binding ActualWidth, ElementName=image}" Height="{Binding ActualHeight, ElementName=image}" >
        <Canvas.Background>
            <VisualBrush  >
                <VisualBrush.Visual>
                    <Image x:Name="image" Source="/WpfApplication10;component/Capture.PNG"  />
                </VisualBrush.Visual>
            </VisualBrush>
        </Canvas.Background>
    </Canvas>
</ScrollViewer>

结果:

【讨论】:

  • 谢谢 - 这非常接近我的需要。调整窗口大小时如何避免图像缩小?该图像是底部带有 DataGrid 的网格的一部分。我认为 Stretch="None" 但这不起作用。
  • 图像完全不会闪退,我认为您的 Grid 设置不正确,您能否为 Grid/DataGrid 显示您的 Xaml
  • 我认为它只是这个Width="{Binding ActualWidth, ElementName=image}" 它应该是Width="{Binding ActualWidth, ElementName=floorPlanImage}" 否则你的代码对我来说很好。
  • 哎呀..是的,错字。谢谢。
  • 没问题 :),快乐编码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-21
  • 2017-02-06
  • 1970-01-01
相关资源
最近更新 更多