You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.1 KiB

2 years ago
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ImageLoader
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private List<string> _imageList;
private List<Image> _imageControlList;
public MainWindow()
{
InitializeComponent();
InitInstance();
}
private void InitInstance()
{
_imageList = new List<string>();
_imageControlList = new List<Image>();
}
private void btnFolder_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == true)
{
string fullPath = dlg.FileName;
string fileName = dlg.SafeFileName;
string path = fullPath.Replace(fileName, "");
txtFolder.Text = path;
string[] files = Directory.GetFiles(path);
_imageList = files.Where(f => f.IndexOf(".jpg", StringComparison.OrdinalIgnoreCase) >= 0
|| f.IndexOf(".png", StringComparison.OrdinalIgnoreCase) >= 0)
.ToList();
}
CreateImage(_imageList);
}
private void CreateImage(List<string> imageList)
{
for (int i = 0; i < imageList.Count; i++)
{
Image image = CreateBitmap(imageList[i]);
_imageControlList.Add(image);
image.MouseDown += Image_MouseDown;
pnlImage.Children.Add(image);
}
}
private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
Image image = sender as Image;
if (image == null)
return;
//WrapPanel parent = image.Parent as WrapPanel;
//if (parent != null)
// parent.Children.Clear();
2 years ago
Image newImage = CreateBitmap(image.Source.ToString(), (int)cvsImage.ActualWidth);
2 years ago
newImage.Stretch = Stretch.UniformToFill;
cvsImage.Children.Clear();
cvsImage.Children.Add(newImage);
//CreateImage(_imageList);
}
private Image CreateBitmap(string path, int widthPixel = 300)
{
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnDemand;
bitmap.CreateOptions = BitmapCreateOptions.DelayCreation;
bitmap.DecodePixelWidth = widthPixel;
bitmap.UriSource = new Uri(path);
bitmap.EndInit();
Image img = new Image();
img.Source = bitmap;
return img;
}
}
}