图片查看器

一个简单的图片查看器,主要用到的东西

(1)动画DoubleAnimationUsingKeyFrames

(2)文件IO

(3)List控件模板ItemTemplate

(4)绑定

遇到的困难及解决方法

(1)ListBox横向显示

解决方法
<ListBox.ItemsPanel>
         <ItemsPanelTemplate>
               <StackPanel Orientation="Horizontal" />
         </ItemsPanelTemplate>
</ListBox.ItemsPanel>

(2)ListBox选中状态修改

解决方法
<ListBox.Resources>
                <Style TargetType="ListBoxItem">
                    <Style.Resources>
                        <!--SelectedItem with focus-->
                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue" Opacity=".4"/>
                        <!--SelectedItem without focus-->
                        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Blue" Opacity=".4"/>
                    </Style.Resources>
                </Style>
            </ListBox.Resources>

(3)缩略图与图片同步

后台属性上修改 同时解决有时候放大 有时候不放大的问题

View Code
public string NowImage
        {
            get { return nowImage; }
            set 
            {
                imageList.SelectedItem = value;
                nowImage = value;
                BitmapImage bi = GetImageByName(value);
                if (bi.Width < 200)
                {
                    image1.Stretch = Stretch.None;
                    image2.Stretch = Stretch.None;
                }
                else
                {
                    image1.Stretch = Stretch.Fill;
                    image2.Stretch = Stretch.Fill;
                }
            }
        }

(4)设置图片的TranslateTransform的X不变的问题

要先停止动画

(5)XAML里面写帧动画

View Code
 <Storyboard x:Key="changePicture1">
                <DoubleAnimationUsingKeyFrames Duration="0:0:5"
                                               Storyboard.TargetName="image2Transform"
                                               Storyboard.TargetProperty="X">
                    <LinearDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
                    <LinearDoubleKeyFrame Value="-800" KeyTime="0:0:5"/>
                </DoubleAnimationUsingKeyFrames>
            </Storyboard>

先看效果

顺便记录一下网上抓取图片的方法

View Code
public Bitmap Get_img()
02            {
03                Bitmap img = null;
04                HttpWebRequest req;
05                HttpWebResponse res = null;
06                    try
07                    {
08                        System.Uri httpUrl = new System.Uri("http://xxx.com/xxx.png ");
09                        req = (HttpWebRequest)(WebRequest.Create(httpUrl));
10                        req.Timeout = 180000; //设置超时值10秒
11                           req.UserAgent = "XXXXX";
12                        req.Accept = "XXXXXX";
13                        req.Method = "GET";
14                        res = (HttpWebResponse)(req.GetResponse());     
15                           img = new Bitmap(res.GetResponseStream());//获取图片流                
16                        img.Save(@"E:/" + DateTime.Now.ToFileTime().ToString() + ".png");//随机名
17                    }
18     
19                    catch (Exception ex)
20                    {
21                         string aa= ex.Message;
22                    }
23                    finally
24                    {
25                        res.Close();
26                    }
27                         return img;
28            }

源码

原文地址:https://www.cnblogs.com/goldren/p/2875230.html