将大图像加载到 WPF 应用程序 - 需要滚动和绘图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15868621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:30:49  来源:igfitidea点击:

Loading a large image to a WPF app - Scrolling and Drawing both needed

wpfimagecanvasscrollviewer

提问by user1524713

I have an idea of how to do only one of these req's, not both of them.

我知道如何只执行这些请求中的一个,而不是两个。

For loading a large image and be able to scroll to see all its parts, I added the image inside a <ScrollViewer/>and got what I needed.

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

For drawing on the image, I was able to load that image as the background of an <Canvas/>.

为了在图像上绘图,我能够将该图像加载为<Canvas/>.

But how to accomplish both?

但是如何实现两者呢?

Basically, I have a very big image that I don't want to shrink, and that image has a bunch of rooms drawn on it (like a floor plan of a building). I have the coordinates of the rooms, and when a user clicks inside a room, I want to fill that room with color (currently i just have a listbox that says "Room 1 has been clicked").

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

Editted XAML (for shrinking image):

编辑的 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>

回答by sa_ddam213

You could just set the Canvassize to the Size of the Imagethat you have set as the Background, This will allow you to use the ScrollViewerand keep the correct Imagesize when set as the CanvasBackground

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

Example:

例子:

<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>

Result:

结果:

enter image description here

在此处输入图片说明