private void btnOpen_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "JPEG,PNG,BMPファイル (*.jpg,*.png,*.bmp)|*.jpg;*.png;*.bmp"; ofd.Multiselect = false; if (ofd.ShowDialog() != true) return; string fn = ofd.File.Name.ToLower(); IImageDecoder dec; if (fn.EndsWith(".jpg")) { dec = new JpegDecoder(); } else if (fn.EndsWith(".png")) { dec = new PngDecoder(); } else if (fn.EndsWith(".bmp")) { dec = new BmpDecoder(); } else { MessageBox.Show("エラー"); return; } try { FileStream fs = ofd.File.OpenRead(); ImageTools.Image img = new ImageTools.Image(); dec.Decode(img, fs); if (img.Width < 50 || img.Height < 50) { MessageBox.Show("画像サイズが小さすぎます"); return; } WriteableBitmap wb = new WriteableBitmap(img.Width, img.Height); byte[] data = img.GetPixels(); for (int y = 0; y < img.Height; y++) { for (int x = 0; x < img.Width; x++) { int idx = (x + y * img.Width) * 4; wb.SetPixel(x, y, Color.FromArgb(data[idx + 3], data[idx], data[idx + 1], data[idx + 2])); } } SetImage(wb); fs.Close(); } catch { MessageBox.Show("エラー"); } }
//Point? mp = null; //private void resizeHandle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) //{ // mp = e.GetPosition((UIElement)sender); // ((UIElement)sender).CaptureMouse(); //} //private void resizeHandle_MouseMove(object sender, MouseEventArgs e) //{ // if (mp != null) // { // } //} //private void resizeHandle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) //{ // mp = null; // ((Canvas)sender).ReleaseMouseCapture(); //} private void LayoutRoot_Drop(object sender, DragEventArgs e) { var file = ((FileInfo[])e.Data.GetData(DataFormats.FileDrop))[0]; string fn = file.Name.ToLower(); WriteableBitmap wb = null; if (fn.EndsWith(".jpg") || fn.EndsWith(".png")) { BitmapImage bi = new BitmapImage(); try { bi.SetSource(file.OpenRead()); if (bi.PixelWidth < 50 || bi.PixelHeight < 50) { MessageBox.Show("画像サイズが小さすぎます"); return; } } catch { MessageBox.Show("エラー"); return; } wb = new WriteableBitmap(bi); } else if (fn.EndsWith(".bmp")) { IImageDecoder dec = new BmpDecoder(); FileStream fs = file.OpenRead(); ImageTools.Image img = new ImageTools.Image(); try { dec.Decode(img, fs); } catch { MessageBox.Show("エラー"); return; } if (img.Width < 50 || img.Height < 50) { MessageBox.Show("画像サイズが小さすぎます"); return; } wb = new WriteableBitmap(img.Width, img.Height); byte[] data = img.GetPixels(); for (int y = 0; y < img.Height; y++) { for (int x = 0; x < img.Width; x++) { int idx = (x + y * img.Width) * 4; wb.SetPixel(x, y, Color.FromArgb(data[idx + 3], data[idx], data[idx + 1], data[idx + 2])); } } fs.Close(); } else { MessageBox.Show("エラー"); return; } try { SetImage(wb); } catch { MessageBox.Show("エラー"); } }