WPF Image
后台设置Image的Souce
方法一:
BitmapImage imgSource = new BitmapImage(new Uri("location",UriKind.Relative)); Image image = new Image(); image.Souce = imgSource;
方法二:
MemoryStream ms = new MemoryStream(); ----初始化ms------ BitmapImage imgSource = new BitmapImage(); imgSouce.BeginInit(); imgSouce.StreamSouce = new MemoryStream(ms.ToArray()); imgSouce.EndInit(); Image image = new Image(); image.Souce = imgSource;
参考:http://www.360doc.com/content/13/0514/12/12098313_285327507.shtml
https://www.cnblogs.com/Big-Head/p/12068230.html
使用Rectangle+ImageBrush来代替Image,解决图片模糊的问题
参考:https://www.cnblogs.com/tommy-huang/p/4974677.html
“正由另一进程使用,因此该进程无法访问该文件”的问题解决方法
https://www.cnblogs.com/ZXdeveloper/p/3779755.html
Image 图片模糊处理
<Rectangle x:Name="rectangle" HorizontalAlignment="Left" Height="{TemplateBinding Height}" Width="140" Margin="7,0,0,0" Stroke="{x:Null}" RenderTransformOrigin="0.5,0.5"> <Rectangle.RenderTransform> <TransformGroup> <ScaleTransform/> <SkewTransform/> <RotateTransform/> <TranslateTransform/> </TransformGroup> </Rectangle.RenderTransform> <Rectangle.Fill> <ImageBrush ImageSource="pack://application:,,,/images/demo.png"/> </Rectangle.Fill> </Rectangle>
异步加载网络图片
示例调用:
<Window xmlns:cvt="clr-namespace:Micro.Converter;assembly=Micro.Common" ...> <Window.Resources> <cvt:BitmapImageConverter x:Key="BitmapImageConverter"/> </Window.Resources> <Grid> <Image Source="{Binding HeadUrl,IsAsync=True,Converter={StaticResource BitmapImageConverter}}" Stretch="Fill"/> </Grid> </Window>
C# 转换器
namespace Micro.Converter { /// <summary> /// 加载网络图片 /// </summary> public class BitmapImageConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) return null; string imageUrl = value.ToString(); if (String.IsNullOrEmpty(imageUrl)) return new BitmapImage(new Uri(@"pack://application:,,,/icons/logo32x32.png")); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imageUrl); WebResponse response = request.GetResponse(); System.Drawing.Image img = System.Drawing.Image.FromStream(response.GetResponseStream()); Bitmap bitMap = new Bitmap(img); return BitmapToBitmapImage(bitMap); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } public BitmapImage BitmapToBitmapImage(Bitmap bitmap) { Bitmap bitmapSource = new Bitmap(bitmap.Width, bitmap.Height); int i, j; for (i = 0; i < bitmap.Width; i++) for (j = 0; j < bitmap.Height; j++) { System.Drawing.Color pixelColor = bitmap.GetPixel(i, j); System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixelColor.R, pixelColor.G, pixelColor.B); bitmapSource.SetPixel(i, j, newColor); } MemoryStream ms = new MemoryStream(); bitmapSource.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = new MemoryStream(ms.ToArray()); bitmapImage.EndInit(); return bitmapImage; } } }