Image控件的简单使用示例1

时间:2023-08-04 20:53:26

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

示例1

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

示例2

            //加载动态图片
BitmapImage img = new BitmapImage();
img.BeginInit();
Uri imgUri = new Uri(@"K:\Picture\jpg\1.jpg",UriKind.Absolute);
img.UriSource = imgUri;
img.EndInit(); imageOne.Width = img.Width / ;
imageOne.Height = img.Height / ;
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:\Picture\jpg\1.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"
/>

显示结果:

Image控件的简单使用示例1

 三。可拖动图片显示

后台代码:

        public ImageFourth()
{
InitializeComponent();
canvasOne.MouseMove += ImageFourth_MouseMove;
}
//获取文件夹下的图片
int count = ;
string path = @"K:\Picture\jpg";
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 = ;
img.Height = ;
img.Margin = new Thickness();
img.Stretch = Stretch.Fill;
Canvas.SetLeft(img, count * );
Canvas.SetTop(img, count * ); 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>

显示结果:

Image控件的简单使用示例1

相关文章