var bitmap = new BitmapImage(new Uri(@"c:\test.bmp"));
建议通过流的方式加载图片:
1 2 3 4 5 6 7 8 9 10 11 12 13
byte[] imageData;
using (var fileStream = new FileStream(@"c:\test.bmp", FileMode.Open, FileAccess.Read)) using (var binaryReader = new BinaryReader(fileStream)) { imageData = binaryReader.ReadBytes((int)fileStream.Length); }
var bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.StreamSource = new MemoryStream(imageData); bitmap.EndInit();
publicclasstest { public ImageSource ImgSource { get; set; } }
List<test> lstImage = new List<test>(); for (int i = 0; i < 100; i++) { test t = new test();
byte[] imageData;
using (var fileStream = new FileStream(lstTest[i], FileMode.Open, FileAccess.Read)) using (var binaryReader = new BinaryReader(fileStream)) { imageData = binaryReader.ReadBytes((int)fileStream.Length); }
var bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.StreamSource = new MemoryStream(imageData); bitmap.EndInit();
publicclasstest { public ImageSource ImgSource { get; set; } }
List<string> lstTest = new List<string>(); List<test> lstShow = new List<test>();
privatevoidbtnLoad_Click(object sender, RoutedEventArgs e) { lstTest.Clear(); for (int i = 0; i < 100; i++) { lstTest.Add(@"d:\1.bmp"); } }
privatevoidbtnPage1_Click(object sender, RoutedEventArgs e) { lstShow.Clear(); for (int i = 0; i < 80; i++) { test t = new test();
byte[] imageData;
using (var fileStream = new FileStream(lstTest[i], FileMode.Open, FileAccess.Read)) using (var binaryReader = new BinaryReader(fileStream)) { imageData = binaryReader.ReadBytes((int)fileStream.Length); }
var bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.StreamSource = new MemoryStream(imageData); bitmap.EndInit();
t.ImgSource = bitmap;
lstShow.Add(t); }
listBox.ItemsSource = lstShow; }
privatevoidbtnPage2_Click(object sender, RoutedEventArgs e) { lstShow.Clear(); for (int i = 80; i < 100; i++) { test t = new test();
byte[] imageData;
using (var fileStream = new FileStream(lstTest[i], FileMode.Open, FileAccess.Read)) using (var binaryReader = new BinaryReader(fileStream)) { imageData = binaryReader.ReadBytes((int)fileStream.Length); }
var bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.StreamSource = new MemoryStream(imageData); bitmap.EndInit();