Image控件的简单使用示例1

Image控件加载图片包括加载动态图片,加载静态图片两种方式。
一。加载动态图片通过生成一个BitmapImage,创建该对象后,赋给Image的Source即可。加载的形式:

示例1

1 BitmapImage myBitmapImage =new BitmapImage();
2 myBitmapImage.BeginInit();
3  //取得数据库存储的图片字段,MS-SQL的Image类型
4  Byte[] mybyte = ReadImage();
5 myBitmapImage.StreamSource =new MemoryStream(mybyte);
6 myBitmapImage.EndInit();
7 myImage.Width = myBitmapImage.Width/2;
8 myImage.Height = myBitmapImage.Height/2;
9 myImage.Source = myBitmapImage;

示例2

            //加载动态图片
            BitmapImage img = new BitmapImage();
            img.BeginInit();
            Uri imgUri = new Uri(@"K:Picturejpg1.jpg",UriKind.Absolute);
            img.UriSource = imgUri;
            img.EndInit();

            imageOne.Width = img.Width / 2;
            imageOne.Height = img.Height / 2;
            imageOne.Source = img;

二.加载系统目录中已经存在的图片

可以采用WPF中的pack://application:,,,格式来加载,比 如在系统的Resource目录下的图片,我们可以这样定义
ImgDev.Source = new BitmapImage(new Uri(pack://application:,,,/Resources/aa.jpg));

示例1:

        void ImageThree_Loaded(object sender, RoutedEventArgs e)
        {
            imgOne.Source = new BitmapImage(new Uri(@"K:Picturejpg1.jpg",UriKind.Absolute));
        }

前台设置:

        <Image Name="imgOne" Opacity="0.5"
            HorizontalAlignment="Left"
               Height="208" Margin="24,22,0,0"
               VerticalAlignment="Top" Width="404"
               ScrollViewer.CanContentScroll="True"
               />

显示结果:

 三。可拖动图片显示

后台代码:

        public ImageFourth()
        {
            InitializeComponent();
            canvasOne.MouseMove += ImageFourth_MouseMove;
        }
        //获取文件夹下的图片
        int count = 0;
        string path = @"K:Picturejpg";
        bool IsMouseDown = false;
        Size spanDown = Size.Empty;
        object mouseDownControl = null;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (Directory.Exists(path))
            {
                string[] files = Directory.GetFiles(path);
                foreach (string filename in files)
                {
                    count++;
                    Image img = new Image();
                    img.Source = new BitmapImage(new Uri(filename, UriKind.Absolute));
                    img.Width = 100;
                    img.Height = 60;
                    img.Margin = new Thickness(10);
                    img.Stretch = Stretch.Fill;
                    Canvas.SetLeft(img, count * 10);
                    Canvas.SetTop(img, count * 10);

                    img.MouseDown += img_MouseDown;
                    img.MouseUp += img_MouseUp;
                    canvasOne.Children.Add(img);
                }
            }
        }
        //鼠标移动
        void ImageFourth_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseDown)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    Point movePoint = e.GetPosition(canvasOne);

                    Image downControl = (Image)mouseDownControl;
                    Canvas.SetLeft(downControl, movePoint.X - spanDown.Width);
                    Canvas.SetTop(downControl, movePoint.Y - spanDown.Height);
                }
            }
        }
        //窗体鼠标松开
        void img_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (IsMouseDown)
            {
                IsMouseDown = false;
            }
        }
        //窗体鼠标按下事件
        void img_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Image downControl = (Image)sender;
            if (downControl != null)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    Point mouseDownPoint = e.GetPosition(canvasOne);

                    spanDown = new Size();
                    spanDown.Width = mouseDownPoint.X - Canvas.GetLeft(downControl);
                    spanDown.Height = mouseDownPoint.Y - Canvas.GetTop(downControl);

                    IsMouseDown = true;
                    mouseDownControl = sender;
                }
            }
        }

前台设置:

        <ScrollViewer Name="scrllOne" Grid.Row="1" 
                      >
            <Canvas Name="canvasOne"  Margin="10"  
                    Background="LightPink"
                    >
                <!--使用Cavas滚动条没有出现作用-->
                <!--<Ellipse Width="600" Height="500" Stroke="DeepSkyBlue" StrokeThickness="2"></Ellipse>-->
            </Canvas>
            <!--使用环绕面板可以出现滚动条-->
            <!-- <WrapPanel Name="wrapOne">
               <Ellipse Width="600" Height="500" Stroke="DeepSkyBlue" StrokeThickness="2"></Ellipse>
            </WrapPanel>-->
        </ScrollViewer>

显示结果:

原文地址:https://www.cnblogs.com/tianma3798/p/3722384.html