/// <summary> /// Mở hộp thoại dialog để chọn ảnh trên máy tính người chơi /// Gọi hàm CropImage để cắt ảnh /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Play_Click(object sender, RoutedEventArgs e) { var screen = new OpenFileDialog(); if (screen.ShowDialog() == true) { _games = null; initArray(number); setTimeLevel(); var Game = new playImage() { Source = new BitmapImage(), Image = screen.FileName, numCut = number }; Game.Source.BeginInit(); Game.Source.DecodePixelHeight = sizeWidth; Game.Source.DecodePixelWidth = sizeHeight; Game.Source.UriSource = new Uri(screen.FileName, UriKind.Absolute); Game.Source.EndInit(); _games = Game; if (timer != null) { ResetTimer(resetSec); } CropImage(Game); } }
/// <summary> /// Cắt ảnh /// </summary> /// <param name="image"></param> private void CropImage(playImage image) { initArray(number); _games = image; int num = image.numCut; int w = image.cropWidth; int h = image.cropHeight; var source = image.Source; listImages.Clear(); for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { var rect = new Int32Rect(j * w, i * h, w, h); var cropbitmap = new CroppedBitmap(source, rect); var cropImage = new Image(); cropImage.Width = w; cropImage.Height = h; cropImage.Source = cropbitmap; listImages.Add(cropImage); cropImage.Tag = new Tuple <int, int>(i, j); } } // lưu hình trống var cropImage1 = new Image(); cropImage1.Width = w; cropImage1.Height = h; listImages.Add(cropImage1); table.Children.Clear(); swapImage(num, w, h); while (!CheckValid(_puzzle, num) && !checkWin(_puzzle, num)) { table.Children.Clear(); swapImage(num, w, h); } for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { Debug.Write(_puzzle[i, j] + " "); } Debug.WriteLine(""); } ResultImage.Source = image.Source; CountDown(); }
//Load lại game private void LoadGame_Click(object sender, RoutedEventArgs e) { var path = PathToProject() + "save.txt"; try { using (StreamReader readtext = new StreamReader(path)) { if (readtext == null) { return; } //Lấy dữ liệu từ file save.txt number = int.Parse(readtext.ReadLine()); var image = readtext.ReadLine(); var currentSec = int.Parse(readtext.ReadLine()); for (int i = 0; i < number; i++) { for (int j = 0; j < number; j++) { _puzzle[i, j] = int.Parse(readtext.ReadLine()); } } Debug.WriteLine($"{number} - {image}"); for (int i = 0; i < number; i++) { for (int j = 0; j < number; j++) { Debug.WriteLine($"{_puzzle[i, j]} "); } Debug.WriteLine(""); } //Xóa trước khi tải hình vào table.Children.Clear(); listImages.Clear(); //Tạo lại game var Game = new playImage() { Source = new BitmapImage(), Image = image, numCut = number }; Game.Source.BeginInit(); Game.Source.DecodePixelHeight = sizeWidth; Game.Source.DecodePixelWidth = sizeHeight; Game.Source.UriSource = new Uri(image, UriKind.Absolute); Game.Source.EndInit(); //Gán lại hình mẫu ResultImage.Source = Game.Source; var w = Game.cropWidth; var h = Game.cropHeight; var source = Game.Source; _games = Game; //Reset lại thời gian if (timer != null) { ResetTimer(currentSec); } CountDown(); //Cắt hình trước //crop Image for (int i = 0; i < number; i++) { for (int j = 0; j < number; j++) { var rect = new Int32Rect(j * w, i * h, w, h); var cropbitmap = new CroppedBitmap(source, rect); var cropImage = new Image(); Canvas.SetLeft(cropImage, j * (w + 2) + startX); Canvas.SetTop(cropImage, i * (h + 2) + startY); cropImage.Width = w; cropImage.Height = h; cropImage.Source = cropbitmap; listImages.Add(cropImage); cropImage.Tag = new Tuple <int, int>(i, j); } } Notify("Load game is already", true); // Để lại hình theo trật tự cũ for (int i = 0; i < number; i++) { for (int j = 0; j < number; j++) { var value = _puzzle[i, j]; if (value != number * number) { //Xét vị trí và xử lý xự kiện Canvas.SetLeft(listImages[value], j * (w + 2) + startX); Canvas.SetTop(listImages[value], i * (h + 2) + startY); table.Children.Add(listImages[value]); listImages[value].PreviewMouseLeftButtonUp += CropImage_PreviewMouseLeftButtonUp; listImages[value].MouseLeftButtonDown += CropImage_MouseLeftButtonDown; } } } } }catch (Exception es) { Debug.WriteLine(es.Message); Notify("Please save game first", false); } }