/// <summary> /// 取当前GIF宽度高度 /// </summary> /// <param name="gif">AnimatedGIF</param> /// <param name="width">宽</param> /// <param name="height">高</param> public static void GetWidthHeight(AnimatedGIF gif, ref double width, ref double height) { //通过Drawing.Image取宽高 Bitmap gb = gif.Bitmap; MemoryStream ms = new MemoryStream(); gb.Save(ms, gb.RawFormat); System.Drawing.Image dimg = System.Drawing.Image.FromStream(ms); width = dimg.Width; height = dimg.Height; ms.Dispose(); ms.Close(); }
private static void BindSource(AnimatedGIF gif) { gif.StopAnimate(); if (gif.Bitmap != null) { gif.Bitmap.Dispose(); } object source = gif.GIFSource; if (source == null) { return; } //根据类型处理 string sourcetype = source.GetType().ToSafeString(); if (sourcetype.Contains("System.String")) { //文件路径 string path = source.ToSafeString(); if (path.IsInvalid()) { return; } if (!Path.IsPathRooted(path)) { source = File.GetPhysicalPath(path); } gif.Bitmap = new Bitmap(path); } else if (sourcetype.Contains("System.IO.Stream")) { //io.Stream gif.Bitmap = new Bitmap(source as Stream); } else if (sourcetype.Contains("System.IO.MemoryStream")) { //io.MemoryStream gif.Bitmap = new Bitmap(source as MemoryStream); } else { //无效 throw new Exception("Unsupported Stream"); } GetWidthHeight(gif, ref imgw, ref imgh); gif.BitmapSource = GetBitmapSource(gif.Bitmap, gif.BitmapSource); gif.StartAnimate(); }
/// <summary> /// 属性更改处理事件 /// </summary> private static void OnSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { AnimatedGIF gif = sender as AnimatedGIF; if (gif == null) { return; } if (!gif.IsLoaded) { return; } BindSource(gif); }
/// <summary> /// 设置图片缩放 /// </summary> /// <param name="zoom">true自适应</param> /// <param name="begin">首次显示</param> public void ImgZoom(bool zoom, bool begin) { if (imgType == null) { return; } double imgw = 0, imgh = 0; AnimatedGIF gifo = null; bool isani = false; UIElement imgui = prewimg.Children[0]; //分类型取值 switch (imgType) { case "bmp": case "jpg": case "png": Image img = (Image)imgui; BitmapImage bi = (BitmapImage)img.Source; imgw = bi.PixelWidth; imgh = bi.PixelHeight; break; case "gif": AnimatedGIF gif = (AnimatedGIF)imgui; gifo = gif; isani = true; break; } if (begin) { if (isani) { imgw = prew.Descs[prew.SelectedId].Width; imgh = prew.Descs[prew.SelectedId].Height; if (imgw < 1 || imgh < 1) { imgw = imgh = double.NaN; } } if (zoom && imgw > prew.imgGrid.ActualWidth || zoom && imgh > prew.imgGrid.ActualHeight) { Width = Height = double.NaN; } else { Width = imgw; Height = imgh; zoom = false; } } else { if (zoom) { Width = Height = double.NaN; } else if (isani) { AnimatedGIF.GetWidthHeight(gifo, ref imgw, ref imgh); Width = imgw; Height = imgh; } else { Width = imgw; Height = imgh; } } isZoom = zoom; }
/// <summary> /// 显示预览 /// </summary> /// <param name="str">各种Stream</param> /// <param name="key">img.ID</param> private void AssignImg(Stream str, int key) { //流下载完毕 try { //提取图片规格类型 string type = GetImgType(str); imgType = type; //记录Stream strs = str; //分显示容器 switch (type) { case "bmp": case "jpg": case "png": //静态图片格式转化 System.Drawing.Image dimg = System.Drawing.Image.FromStream(str); MemoryStream ms = new MemoryStream(); dimg.Save(ms, imgf[type]); BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = new MemoryStream(ms.ToArray()); //ms不能直接用 bi.EndInit(); ms.Close(); //创建静态图片控件 Image img = new Image() { Source = bi, Stretch = Stretch.Uniform, SnapsToDevicePixels = true, StretchDirection = StretchDirection.Both, Margin = new Thickness() { Top = 0, Right = 0, Bottom = 0, Left = 0 } }; //将预览控件加入布局 prewimg.Children.Add(img); break; case "gif": //创建GIF动图控件 AnimatedGIF gif = new AnimatedGIF() { GIFSource = str, Stretch = Stretch.Uniform, SnapsToDevicePixels = true, StretchDirection = StretchDirection.Both }; //将预览控件加入布局 prewimg.Children.Add(gif); break; default: //未支持预览的格式 Dispatcher.Invoke(new UIdelegate(delegate(object ss) { StopLoadImg(key, "不支持" + type); }), ""); return; } //选中的预览图显示出来 if (key == prew.SelectedId) { Visibility = Visibility.Visible; } //隐藏进度条 ProgressPlate.Visibility = Visibility.Hidden; ImgZoom(true); imgloaded = true; } catch (Exception ex1) { Program.Log(ex1, "Read sample img failed"); Dispatcher.Invoke(new UIdelegate(delegate(object ss) { StopLoadImg(key, true, "读取数据失败"); }), ex1); } }