WPF Image图片后台绑定图片,不对图片照成占用
public void BinDingImag(string ImageName, Image image)
{
using (FileStream FS = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "Images//ManTitle//" + ImageName + ".png", FileMode.Open))
{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(FS);
image.Source = ConverImages.GetImageBrushFrom_PResource(bmp).ImageSource;
image.Stretch = Stretch.Fill;
bmp.Dispose();
FS.Dispose();
FS.Close();
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Media;
ConverImages----类
public class ConverImages
{
/// <summary>
/// 图片转换
/// </summary>
/// <param name="imgSrc"></param>
/// <returns></returns>
public static ImageBrush GetImageBrushFrom_PResource(Bitmap imgSrc)
{
System.Windows.Media.ImageBrush imgBrush = new System.Windows.Media.ImageBrush();
try
{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(imgSrc);
MemoryStream stream = new MemoryStream();
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
System.Windows.Media.ImageSourceConverter isConverter = new System.Windows.Media.ImageSourceConverter();
imgBrush.ImageSource = (System.Windows.Media.ImageSource)isConverter.ConvertFrom(stream);
return imgBrush;
}
catch (Exception ex)
{
Log.WriteLog(LogFile.Error, ex.Message);
return null;
}
}
}