Silverlight 4.0 Silverlight图像平移

Silverlight 4.0 Silverlight图像平移,silverlight-4.0,Silverlight 4.0,我正在工作的silverlight应用程序,我想平移像顶部,底部,左,右的图像,我可以如何平移图像 我曾经使用过像素着色,但我没有成功 谢谢..看一看 您还可以查看拖动的混合行为。这对我很有效。用户可以使用此窗口预览放大的图像,并将图像平移到更相关的部分,例如图像的底部 要使用该窗口,请执行以下操作: BitmapImage BMP = /* resolve the bitmap */; PreviewImageWindow.Execute(BMP); 窗口的代码(隐藏)如下所示 using

我正在工作的silverlight应用程序,我想平移像顶部,底部,左,右的图像,我可以如何平移图像

我曾经使用过像素着色,但我没有成功

谢谢..

看一看


您还可以查看拖动的混合行为。

这对我很有效。用户可以使用此窗口预览放大的图像,并将图像平移到更相关的部分,例如图像的底部

要使用该窗口,请执行以下操作:

BitmapImage BMP = /* resolve the bitmap */;

PreviewImageWindow.Execute(BMP);
窗口的代码(隐藏)如下所示

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace ITIS.Controls.LinearViewer.Windows {

public partial class PreviewImageWindow : ChildWindow {

    /// <summary>See Execute</summary>
    PreviewImageWindow() {
        InitializeComponent();
    }

    private void OKButton_Click(object sender, RoutedEventArgs e) {
        this.DialogResult = true;
    }

    private void CancelButton_Click(object sender, RoutedEventArgs e) {
        this.DialogResult = false;
    }

    static public void Execute(BitmapImage imageSource) {

        PreviewImageWindow Window = new PreviewImageWindow();

        Window.Image.Source = imageSource;

        /* don't allow the window to grow larger than the image */
        Window.MaxWidth = imageSource.PixelWidth;
        Window.MaxHeight = imageSource.PixelHeight;

        Window.Show();
    }

    private void ChildWindow_KeyDown(object sender, KeyEventArgs e) {

        if (e.Key == Key.Escape) {

            DialogResult = false;
        }
    }

    Point? _lastPoint;

    bool _isMouseDown;

    private void image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {

        ((Image)sender).CaptureMouse();

        _isMouseDown = true;

        ShowCursor(e.GetPosition(Canvas));

        _lastPoint = e.GetPosition((Image)sender);
    }

    private void image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {

        ((Image)sender).ReleaseMouseCapture();

        _isMouseDown = false;

        ShowCursor(e.GetPosition(Canvas));

        _lastPoint = null;
    }

    private void image_MouseMove(object sender, MouseEventArgs e) {

        if (_lastPoint != null) {

            Image Image = (Image)sender;

            Point CurrentPoint = e.GetPosition(Image);

            double 
                XDelta = CurrentPoint.X - _lastPoint.Value.X,
                YDelta = CurrentPoint.Y - _lastPoint.Value.Y;

            _lastPoint = null;

            if (XDelta != 0) {
                double NewLeft = Canvas.GetLeft(Image) + XDelta;
                if (NewLeft <= 0 && NewLeft + Image.ActualWidth >= Canvas.ActualWidth) {
                    Canvas.SetLeft(Image, NewLeft);
                }
            }

            if (YDelta != 0) {
                double NewTop = Canvas.GetTop(Image) + YDelta;
                if (NewTop <= 0 && NewTop + Image.ActualHeight >= Canvas.ActualHeight) {
                    Canvas.SetTop(Image, NewTop);
                }
            }

            _lastPoint = e.GetPosition(Image);
        }

        Point CanvasPoint = e.GetPosition(Canvas);
        ShowCursor(CanvasPoint);
    }

    private void Canvas_Loaded(object sender, RoutedEventArgs e) {

        TryDefaultImageToTop();
    }

    void TryDefaultImageToTop() {

        if (Image == null || Canvas == null) { return; }

        /* move the image up so we can focus on the road? user-friendly since we are most-likely going to look at the road, not the horizon or top - half */
        if (!_initialized) {

            _initialized = true;

            Canvas.SetTop(Image, Canvas.ActualHeight - Image.ActualHeight);
            Canvas.SetLeft(Image, (Canvas.ActualWidth - Image.ActualWidth) / 2);
        }
    }

    bool _initialized;

    private void image_Loaded(object sender, RoutedEventArgs e) {

        TryDefaultImageToTop();
    }

    private void image_MouseEnter(object sender, MouseEventArgs e) {

        imgOpenHand.Visibility = Visibility.Visible;
        imgClosedHand.Visibility = Visibility.Collapsed;

        ShowCursor(e.GetPosition(Canvas));
    }

    void ShowCursor(Point point) {

        if (_isMouseDown) {
            imgClosedHand.Visibility = Visibility.Visible;
            imgOpenHand.Visibility = Visibility.Collapsed;

            Canvas.SetLeft(imgClosedHand, point.X);
            Canvas.SetTop(imgClosedHand, point.Y);
        }
        else {
            imgClosedHand.Visibility = Visibility.Collapsed;
            imgOpenHand.Visibility = Visibility.Visible;

            Canvas.SetLeft(imgOpenHand, point.X);
            Canvas.SetTop(imgOpenHand, point.Y);
        }
    }

    private void image_MouseLeave(object sender, MouseEventArgs e) {
        imgOpenHand.Visibility = Visibility.Collapsed;
        imgClosedHand.Visibility = Visibility.Collapsed;
    }
}
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Input;
使用System.Windows.Media.Imaging;
命名空间ITIS.Controls.LinearViewer.Windows{
公共部分类PreviewImageWindow:ChildWindow{
///见执行
预览图像窗口(){
初始化组件();
}
私有无效确认按钮\单击(对象发送者,路由目标e){
this.DialogResult=true;
}
私有作废取消按钮\u单击(对象发送方,路由目标){
this.DialogResult=false;
}
静态公共void执行(BitmapImage imageSource){
PreviewImageWindow=新建PreviewImageWindow();
Window.Image.Source=图像源;
/*不允许窗口的大小超过图像的大小*/
Window.MaxWidth=imageSource.PixelWidth;
Window.MaxHeight=imageSource.PixelHeight;
Window.Show();
}
私有void ChildWindow_KeyDown(对象发送方,KeyEventArgs e){
if(e.Key==Key.Escape){
DialogResult=false;
}
}
点?\最后一点;
bool_is mousedown;
私有无效图像\u MouseLeftButtonDown(对象发送器,MouseButtonEventArgs e){
((图像)sender.CaptureMouse();
_isMouseDown=真;
ShowCursor(例如GetPosition(Canvas));
_lastPoint=e.GetPosition((图像)发送器);
}
私有void image_MouseLeftButtonUp(对象发送器,MouseButtonEventArgs e){
((图像)发送者)。释放mouseCapture();
_isMouseDown=错误;
ShowCursor(例如GetPosition(Canvas));
_lastPoint=null;
}
私有无效图像\u MouseMove(对象发送器,MouseEventArgs e){
如果(_lastPoint!=null){
图像=(图像)发送方;
点CurrentPoint=e.GetPosition(图像);
双重的
XDelta=CurrentPoint.X-_lastPoint.Value.X,
YDelta=CurrentPoint.Y-\u lastPoint.Value.Y;
_lastPoint=null;
如果(XDelta!=0){
double NewLeft=Canvas.GetLeft(Image)+XDelta;
if(NewLeft=Canvas.ActualWidth){
Canvas.SetLeft(图像,NewLeft);
}
}
如果(YDelta!=0){
double NewTop=Canvas.GetTop(Image)+YDelta;
如果(NewTop=Canvas.ActualHeight){
Canvas.SetTop(图像,NewTop);
}
}
_lastPoint=e.GetPosition(图像);
}
点画布点=e.GetPosition(画布);
ShowCursor(CanvasPoint);
}
已加载私有无效画布(对象发送方、路由目标){
TryDefaultImageToTop();
}
void TryDefaultImageToTop(){
如果(Image==null | | Canvas==null){return;}
/*将图像向上移动,以便我们可以将注意力集中在道路上?用户友好,因为我们最有可能看到的是道路,而不是地平线或上半部*/
如果(!\u已初始化){
_初始化=真;
SetTop(Image,Canvas.ActualHeight-Image.ActualHeight);
SetLeft(Image,(Canvas.ActualWidth-Image.ActualWidth)/2);
}
}
bool_初始化;
已加载私有无效图像(对象发送器,路由目标e){
TryDefaultImageToTop();
}
专用void image_MouseEnter(对象发送器,MouseEventArgs e){
imgOpenHand.Visibility=Visibility.Visible;
imgClosedHand.Visibility=可见性.已折叠;
ShowCursor(例如GetPosition(Canvas));
}
无效显示光标(点){
如果(_ismouse down){
imgClosedHand.Visibility=可见性.Visibility;
imgOpenHand.Visibility=Visibility.Collapsed;
Canvas.SetLeft(imgClosedHand,point.X);
Canvas.SetTop(imgClosedHand,point.Y);
}
否则{
imgClosedHand.Visibility=可见性.已折叠;
imgOpenHand.Visibility=Visibility.Visible;
Canvas.SetLeft(imgOpenHand,point.X);
Canvas.SetTop(imgOpenHand,point.Y);
}
}
专用void image_MouseLeave(对象发送器,MouseEventArgs e){
imgOpenHand.Visibility=Visibility.Collapsed;
imgClosedHand.Visibility=可见性.已折叠;
}
}
}

窗口的XAML如下所示:

<controls:ChildWindow 
x:Class="ITIS.Controls.LinearViewer.Windows.PreviewImageWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
Background="#383838" Foreground="WhiteSmoke"    
Title="Preview" Margin="50"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
KeyDown="ChildWindow_KeyDown"    
>
<Grid x:Name="LayoutRoot" Margin="0" Cursor="None" IsHitTestVisible="True">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Canvas Cursor="None" IsHitTestVisible="True" x:Name="Canvas" Loaded="Canvas_Loaded">
        <Image Source="{Binding ImageSource}" x:Name="Image"
               Cursor="None" Loaded="image_Loaded"
               MouseLeftButtonDown="image_MouseLeftButtonDown"
               MouseLeftButtonUp="image_MouseLeftButtonUp"
               MouseMove="image_MouseMove"
               MouseEnter="image_MouseEnter"
               MouseLeave="image_MouseLeave"
               IsHitTestVisible="True"
               />
        <Image Style="{StaticResource HandOpenImage}" x:Name="imgOpenHand" 
               Visibility="Collapsed" IsHitTestVisible="False" 
               />
        <Image Style="{StaticResource HandClosedImage}" x:Name="imgClosedHand"
               Visibility="Collapsed" IsHitTestVisible="False" 
               />
    </Canvas>        
</Grid>    

关于此代码的一些捕获/注释:

  • 此窗口的名称空间为“ITIS.Controls.LinearViewer.Windows”,请将名称空间更改为系统中更相关的名称空间
  • 正常的光标图像为,当鼠标按钮按下时,图像变为
  • 我在全球应用程序范围的资源字典中提供的图像样式:
  • (开放式图像样式)

    
    
    (闭合图像样式)

    
    
  • 此预览尝试从图像的下半部分开始,而不是上半部分
  • 希望这有帮助。我花了一段时间才消除了一些恼怒

    <Style TargetType="Image" x:Key="HandOpenImage">
        <Setter Property="Source" Value="/ITIS.Controls.LinearViewer.Silverlight;component/Images/HandOpen.png" />
        <Setter Property="Width" Value="16" />
        <Setter Property="Height" Value="16" />
    </Style>
    
    <Style TargetType="Image" x:Key="HandClosedImage">
        <Setter Property="Source" Value="/ITIS.Controls.LinearViewer.Silverlight;component/Images/HandClosed.png" />
        <Setter Property="Width" Value="13" />
        <Setter Property="Height" Value="11" />
    </Style>