WPF之IMage控件
Image控件加载图片包括加载动态图片,加载静态图片两种方式。
加载动态图片通过生成一个BitmapImage,创建该对象后,赋给Image的Source即可。加载的形式:
代码
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 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;
加载系统目录中已经存在的图片,可以采用WPF中的pack://application:,,,格式来加载,比 如在系统的Resource目录下的图片,我们可以这样定义
ImgDev.Source = new BitmapImage(new Uri(pack://application:,,,/Resources/aa.jpg));
如果我们需要挪动页面上的图片,该怎么办呢?下面以我自己做的一个小DEMO来演示下:
挪动图片实际上挪动Image控件的上一层框架容器,这样我们把Image控件放入canvas,来对
canvas进行挪动。例如:
<Canvas Name="canvas1" Height="238" HorizontalAlignment="Left" VerticalAlignment="Top" Width="378">
<Image Name="myImage"></Image>
</Canvas>
<Image Name="myImage"></Image>
</Canvas>
然后我们需要在WPF窗体里面监测鼠标的左键按下,移动的动作,因此在xaml文件的window标签中
我们加入MouseMove="win_mainX_MouseMove" MouseUp="win_mainX_MouseUp"
其中win_mainX是窗体的名称,同样,在Image控件,需要判断鼠标是否按下,如果按下,则记录相应的
Position,并且移动图片。代码如下:
代码
1 bool IsMouseDown =false;
2 Point mousePoint;
3 object mouseCtrl =null;
4 privatevoid win_mainX_MouseMove(object sender, MouseEventArgs e){
5 if (IsMouseDown){
6 if (e.LeftButton == MouseButtonState.Pressed)
2 Point mousePoint;
3 object mouseCtrl =null;
4 privatevoid win_mainX_MouseMove(object sender, MouseEventArgs e){
5 if (IsMouseDown){
6 if (e.LeftButton == MouseButtonState.Pressed)
{Point theMousePoint = e.GetPosition(this.canvas1);
7 Canvas.SetLeft((UIElement)mouseCtrl, theMousePoint.X - (mousePoint.X - Canvas.GetLeft(((UIElement)mouseCtrl))));
8 Canvas.SetTop((UIElement)mouseCtrl, theMousePoint.Y - (mousePoint.Y - Canvas.GetTop(((UIElement)mouseCtrl))));
9 mousePoint = theMousePoint;
10 }
11 }
12 }
13 privatevoid win_mainX_MouseUp(object sender, MouseButtonEventArgs e){
14 if (IsMouseDown){
15 IsMouseDown =false;
16 }
17 }
18 privatevoid myImage_MouseDown(object sender, MouseButtonEventArgs e){
19 if (e.LeftButton == MouseButtonState.Pressed) {
20 IsMouseDown =true;
21 mousePoint = e.GetPosition(this.canvas1);
7 Canvas.SetLeft((UIElement)mouseCtrl, theMousePoint.X - (mousePoint.X - Canvas.GetLeft(((UIElement)mouseCtrl))));
8 Canvas.SetTop((UIElement)mouseCtrl, theMousePoint.Y - (mousePoint.Y - Canvas.GetTop(((UIElement)mouseCtrl))));
9 mousePoint = theMousePoint;
10 }
11 }
12 }
13 privatevoid win_mainX_MouseUp(object sender, MouseButtonEventArgs e){
14 if (IsMouseDown){
15 IsMouseDown =false;
16 }
17 }
18 privatevoid myImage_MouseDown(object sender, MouseButtonEventArgs e){
19 if (e.LeftButton == MouseButtonState.Pressed) {
20 IsMouseDown =true;
21 mousePoint = e.GetPosition(this.canvas1);
mouseCtrl = sender;
22 }
23 }
24
22 }
23 }
24