none
WPF动态加载图片问题 在线等! RRS feed

  • 问题

  • 点击button
    窗口中的Image控件就显示出一张图片来
    这个东西怎么搞 ?
    都弄了2天了还没搞出来..
    求助啊!
    请给出代码吧!


    解决了!
    自己把代码贴出来..
    初学者可以看看..
    Windows1.xaml


    <Window x:Class="MSDNimage.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MSDNimage" WindowState="Maximized"
        >
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.973254086181278*" />
                <ColumnDefinition Width="0.00742942050520059*" />
                <ColumnDefinition Width="0.011887072808321*" />
                <ColumnDefinition Width="0.00742942050520059*" />
            </Grid.ColumnDefinitions>
            <Button Height="23" HorizontalAlignment="Left" Margin="15,64,0,0" Name="btn1" VerticalAlignment="Top" Width="75" Click="btn1_Click">Hello</Button>
            <Image Margin="228,26,21,77" Name="img1" />
            
        </Grid>
    </Window>


    Windows1.xaml.cs

     
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;

    namespace MSDNimage
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>

        public partial class Window1 : System.Windows.Window
        {
            

            public Window1()
            {
                InitializeComponent();

                btn1.Click +=new RoutedEventHandler(btn1_Click);
            }
            public void btn1_Click(object sender, RoutedEventArgs e)
            {
              

                    // 创建一个源
                    BitmapImage myBitmapImage = new BitmapImage();

                    // BitmapImage.UriSource必须使用BeginInit/EndInit块
                    myBitmapImage.BeginInit();
                    myBitmapImage.UriSource = new Uri(@"sampleImages/building.jpg",UriKind.Relative);

                    myBitmapImage.EndInit();
                    //把源赋给Image控件
                    img1.Source = myBitmapImage;
             

            }
        }
    }



    2009年1月16日 6:58

答案

全部回复

  • 图片的动态加载可以实现了
    拖拽不能实现..
    Windows1.xaml
    <Window x:Class="MSDNimage.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MSDNimage" WindowState="Maximized"
        >
        <Grid>
            <Canvas Name="ImageCanvas" DragOver="ImageCanvas_DragOver" AllowDrop="True"
                    PreviewMouseLeftButtonUp="ImageCanvas_PreviewMouseLeftButtonUp"
                    MouseMove="ImageCanvas_MouseMove" Margin="-8,0,8,0"  >
                <Button Name="btn1" Click="btn1_Click" Canvas.Left="100" Canvas.Top="100" Width="50" Height="50" ></Button>
                <Image Name="img1"  Height="100" Width="100"  Canvas.Top="100" Canvas.Left="300" Cursor="Hand" ></Image>
            </Canvas>
        </Grid>

    </Window>



    Windows1.xaml.cs

     
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;

    namespace MSDNimage
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>

        public partial class Window1 : System.Windows.Window
        {
            

            public Window1()
            {
                InitializeComponent();

                btn1.Click +=new RoutedEventHandler(btn1_Click);
            }
            public void btn1_Click(object sender, RoutedEventArgs e)
            {
              

                    // 创建一个源
                    BitmapImage myBitmapImage = new BitmapImage();

                    // BitmapImage.UriSource必须使用BeginInit/EndInit块
                    myBitmapImage.BeginInit();
                    myBitmapImage.UriSource = new Uri(@"sampleImages/building.jpg",UriKind.Relative);

                    myBitmapImage.EndInit();
                    //把源赋给Image控件
                    img1.Source = myBitmapImage;
             

            }
            private bool _isDragging = false;
            private Point _startPoint;
            private Point _relativePoint;
            private Image _selectImage;

            private void Image_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                _selectImage = sender as Image;
                _startPoint = e.GetPosition(ImageCanvas);
                _relativePoint = e.GetPosition(_selectImage);
                _isDragging = true;

                foreach (Image image in ImageCanvas.Children)
                {
                    image.SetValue(Canvas.ZIndexProperty, 0);
                }
                _selectImage.SetValue(Canvas.ZIndexProperty, 1);
            }

            private void ImageCanvas_DragOver(object sender, DragEventArgs e)
            {
                if (e.Data != null && e.Data.GetDataPresent(typeof(Image)))
                {
                    Image image = e.Data.GetData(typeof(Image)) as Image;
                    Point p = e.GetPosition(ImageCanvas);

                    image.SetValue(Canvas.LeftProperty, p.X - _relativePoint.X);
                    image.SetValue(Canvas.TopProperty, p.Y - _relativePoint.Y);
                }
            }

            private void ImageCanvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                _isDragging = false;
            }

            private void ImageCanvas_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.LeftButton == MouseButtonState.Pressed && _isDragging)
                {
                    Point newPos = e.GetPosition(ImageCanvas);
                    if ((Math.Abs(newPos.X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance) ||
                        (Math.Abs(newPos.Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance))
                    {
                        ImageCanvas.GiveFeedback += new GiveFeedbackEventHandler(ImageCanvas_GiveFeedback);
                      
                        DragDrop.DoDragDrop(ImageCanvas, _selectImage, DragDropEffects.Move);                   
                       
                        _isDragging = false;   
                        ImageCanvas.GiveFeedback -= new GiveFeedbackEventHandler(ImageCanvas_GiveFeedback);
                    }
                }
            }
            void ImageCanvas_GiveFeedback(object sender, GiveFeedbackEventArgs e)
            {
                Mouse.SetCursor(Cursors.Hand);  //Set Cursor
                e.Handled = true;
            }

        }
    }
      


    2009年1月16日 8:27
  • 在你的MouseLeftButtonDown事件处理程序里面,你应该通知Windows你开始一个拖拽操作,用下面的函数开始托拽操作:

    System.Windows.DragDrop.DoDragDrop

     

    这里有几个托拽操作的例子,你可以参考一下:

    http://msdn.microsoft.com/zh-cn/library/ms771293.aspx

     

    你还需要处理Canvas.Drop事件,以便终止拖拉操作。

    2009年1月16日 10:01