ScrollViewer中的画布中的图像

时间:2014-06-23 21:46:39

标签: c# wpf xaml

所以我在ScrollViewer中有这个图像(图像是平移和可缩放的):

<ScrollViewer x:Name="imageScrollViewer" SizeChanged="image_SizeChanged" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible">
    <Image x:Name="image" Stretch="Uniform" RenderOptions.BitmapScalingMode="Fant" RenderOptions.EdgeMode="Aliased"
    MouseMove="image_MouseMove" MouseLeftButtonDown="image_MouseLeftButtonDown" SizeChanged="image_SizeChanged">
        <Image.LayoutTransform>
            <ScaleTransform x:Name="scaleTransform" ScaleX="1" ScaleY="1" CenterX="0" CenterY="0" />
        </Image.LayoutTransform>
    </Image>
</ScrollViewer>

现在,我想要绘制我的图像(只是一些矩形用于某些标记)。我想直接绘制到图像上(没有System.Drawing ...)是不可能的?

我尝试使用Canvas(所以我可以添加Rectangle对象),如下所示:

<ScrollViewer x:Name="imageScrollViewer" SizeChanged="image_SizeChanged" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible">
    <ScrollViewer.InputBindings>
        <KeyBinding Key="PageDown" Command="local:MainWindow.NextPage" />
        <KeyBinding Key="PageUp" Command="local:MainWindow.PreviousPage" />
    </ScrollViewer.InputBindings>
    <Canvas x:Name="canvas">
        <Image x:Name="image" Stretch="Uniform" RenderOptions.BitmapScalingMode="Fant" RenderOptions.EdgeMode="Aliased"
    MouseMove="image_MouseMove" MouseLeftButtonDown="image_MouseLeftButtonDown" SizeChanged="image_SizeChanged">
            <Image.LayoutTransform>
                <ScaleTransform x:Name="scaleTransform" ScaleX="1" ScaleY="1" CenterX="0" CenterY="0" />
            </Image.LayoutTransform>
        </Image>
    </Canvas>
</ScrollViewer>

现在我的影像不再泛也不可缩放,因为画布&#34;破坏&#34;我的整个布局,从而功能。在我的代码隐藏中,我真的依赖于Image(以及它的ActualWidth / ActualHeight等),因为我捕获了鼠标位置,然后决定它是否在图像的标记上盘旋。

对此有何想法?

1 个答案:

答案 0 :(得分:1)

您可以a)将画布宽度/高度绑定到图像宽度/高度以使坐标相同,b)使用WritableBitmap并将绘图绘制到实际位图上。

相关问题