none
请教silverlight相对图片定位的问题 RRS feed

  • 问题

  • 大家好:
          我的要求是这样的,现在有一张图片,我在图片上又增加了一个Rectangle对象,我想实现鼠标移动Rectangle对象时,Rectangle对象自动选定图像的这个区域,然后裁剪这个区域,现在我想知道,我如何获取Rectangle对象在图片的什么位置,或者应该怎么样实现Rectangle对象来选中图片区域以及裁剪这个区域?
    • 已编辑 钱迷迷 2010年3月6日 13:36 更改错误
    2010年3月6日 13:21

答案


  • 代码如下:


    <UserControl x:Class="SilverlightApplication4.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
      <Canvas x:Name="LayoutRoot" MouseMove="LayoutRoot_MouseMove">
            <TextBlock x:Name="txtPost" Text=""  Canvas.Left="0" Canvas.Top="0"/>
            <Image x:Name="imgResult" Canvas.Left="20" Canvas.Top="20"  Width="100" Height="80"  Canvas.ZIndex="30"/>
            <Image x:Name="imgTest" Canvas.Left="0" Canvas.Top="0"  Source="/Dock.jpg"/>
            <Rectangle x:Name="rectHander" Canvas.Left="300" Canvas.Top="300"  Fill="Blue" Width="100" Height="80"
                       MouseLeftButtonUp="rectHander_MouseLeftButtonUp" MouseLeave="rectHander_MouseLeave"
                       MouseLeftButtonDown="rectHander_MouseLeftButtonDown" MouseMove="rectHander_MouseMove"/>
        </Canvas>
    </UserControl>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Windows.Media.Imaging;
    
    namespace SilverlightApplication4
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
            }
    
    
            private bool IsPress = false;
            private Point StartPoint = new Point();
            private void rectHander_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                IsPress = false;
                Point positionMouseOnRect = e.GetPosition(imgTest);//得到相对于图片的位置
    
                double x = positionMouseOnRect.X;
                double y = positionMouseOnRect.Y;
                this.txtPost.Text = "松开位置:(" + x + "," + y + ")";
                BitmapSource img = CutImage(x, y, 100, 80);
                this.imgResult.Source = img;
            }
    
            private void rectHander_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                IsPress = true;
                StartPoint = e.GetPosition(LayoutRoot);
            }
    
            private void rectHander_MouseMove(object sender, MouseEventArgs e)
            {
                if (IsPress)
                {
                    Point position = e.GetPosition(LayoutRoot);
                    double x = position.X;
                    double y = position.Y;
                    double x1 = StartPoint.X;
                    double y1 = StartPoint.Y;
                    double rX = Canvas.GetLeft(rectHander);
                    double rY = Canvas.GetTop(rectHander);
                    Canvas.SetLeft(rectHander, rX + x - x1);
                    Canvas.SetTop(rectHander, rY + y - y1);
                    StartPoint = position;
                }
            }
    
            private void rectHander_MouseLeave(object sender, MouseEventArgs e)
            {
                IsPress = false;
                
            }
    
            private BitmapSource CutImage(double x, double y, int width, int height)
            {
                WriteableBitmap bit = new WriteableBitmap(width, height);
                TranslateTransform tran = new TranslateTransform();
                tran.X = -x;
                tran.Y = -y;
                bit.Render(imgTest, tran);
                bit.Invalidate();
    
                return bit;
            }
    
            private void LayoutRoot_MouseMove(object sender, MouseEventArgs e)
            {
                Point positionMouseOnRect = e.GetPosition(LayoutRoot);
    
                double x = positionMouseOnRect.X;
                double y = positionMouseOnRect.Y;
                this.txtPost.Text = "(" + x + "," + y + ")";
            }
    
        }
    }



    努力!完成了js解析器,还差一个svg插件,一个绘图程序,做好自己,呵呵~!
    2010年3月9日 1:35
    版主

全部回复

  • 如果imgTest是图片
    Point positionMouseOnRect = e.GetPosition(imgTest);//得到相对于图片的位置
    努力!完成了js解析器,还差一个svg插件,一个绘图程序,做好自己,呵呵~!
    2010年3月9日 1:33
    版主

  • 代码如下:


    <UserControl x:Class="SilverlightApplication4.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
      <Canvas x:Name="LayoutRoot" MouseMove="LayoutRoot_MouseMove">
            <TextBlock x:Name="txtPost" Text=""  Canvas.Left="0" Canvas.Top="0"/>
            <Image x:Name="imgResult" Canvas.Left="20" Canvas.Top="20"  Width="100" Height="80"  Canvas.ZIndex="30"/>
            <Image x:Name="imgTest" Canvas.Left="0" Canvas.Top="0"  Source="/Dock.jpg"/>
            <Rectangle x:Name="rectHander" Canvas.Left="300" Canvas.Top="300"  Fill="Blue" Width="100" Height="80"
                       MouseLeftButtonUp="rectHander_MouseLeftButtonUp" MouseLeave="rectHander_MouseLeave"
                       MouseLeftButtonDown="rectHander_MouseLeftButtonDown" MouseMove="rectHander_MouseMove"/>
        </Canvas>
    </UserControl>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Windows.Media.Imaging;
    
    namespace SilverlightApplication4
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
            }
    
    
            private bool IsPress = false;
            private Point StartPoint = new Point();
            private void rectHander_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                IsPress = false;
                Point positionMouseOnRect = e.GetPosition(imgTest);//得到相对于图片的位置
    
                double x = positionMouseOnRect.X;
                double y = positionMouseOnRect.Y;
                this.txtPost.Text = "松开位置:(" + x + "," + y + ")";
                BitmapSource img = CutImage(x, y, 100, 80);
                this.imgResult.Source = img;
            }
    
            private void rectHander_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                IsPress = true;
                StartPoint = e.GetPosition(LayoutRoot);
            }
    
            private void rectHander_MouseMove(object sender, MouseEventArgs e)
            {
                if (IsPress)
                {
                    Point position = e.GetPosition(LayoutRoot);
                    double x = position.X;
                    double y = position.Y;
                    double x1 = StartPoint.X;
                    double y1 = StartPoint.Y;
                    double rX = Canvas.GetLeft(rectHander);
                    double rY = Canvas.GetTop(rectHander);
                    Canvas.SetLeft(rectHander, rX + x - x1);
                    Canvas.SetTop(rectHander, rY + y - y1);
                    StartPoint = position;
                }
            }
    
            private void rectHander_MouseLeave(object sender, MouseEventArgs e)
            {
                IsPress = false;
                
            }
    
            private BitmapSource CutImage(double x, double y, int width, int height)
            {
                WriteableBitmap bit = new WriteableBitmap(width, height);
                TranslateTransform tran = new TranslateTransform();
                tran.X = -x;
                tran.Y = -y;
                bit.Render(imgTest, tran);
                bit.Invalidate();
    
                return bit;
            }
    
            private void LayoutRoot_MouseMove(object sender, MouseEventArgs e)
            {
                Point positionMouseOnRect = e.GetPosition(LayoutRoot);
    
                double x = positionMouseOnRect.X;
                double y = positionMouseOnRect.Y;
                this.txtPost.Text = "(" + x + "," + y + ")";
            }
    
        }
    }



    努力!完成了js解析器,还差一个svg插件,一个绘图程序,做好自己,呵呵~!
    2010年3月9日 1:35
    版主