WPF PrismとOpenCVSharpを使って画像の表示
先ほど、Prismを使って画像を表示するWPFアプリケーションを作成しました。
○○○○に怖いものなんてない!!: WPF(Prism)で画像の表示
http://kowaimononantenai.blogspot.jp/2017/02/wpfprism.html
画像処理等にはOpenCVSharpを使用することが多いので、
画像入力はOpenCVSharpを使用して、Matで管理するサンプルプログラムに変更しました。
でも、画像の表示はWPFのImageで表示しています。
プログラムは同じようにView,ViewModel,Modelに分けて管理しています。
まずはDisplayImageWindow.xamlです。
次はDisplayImageWindowViewModel.cs。
さきほどと違うのはBitmapImageからWriteableBitmapに変更になったところです。
最後にImageDisplayer.csです。
OpenCVSharpで画像を入力した後、表示するときだけWriteableBitmapに変換して渡します。
○○○○に怖いものなんてない!!: WPF(Prism)で画像の表示
http://kowaimononantenai.blogspot.jp/2017/02/wpfprism.html
画像処理等にはOpenCVSharpを使用することが多いので、
画像入力はOpenCVSharpを使用して、Matで管理するサンプルプログラムに変更しました。
でも、画像の表示はWPFのImageで表示しています。
プログラムは同じようにView,ViewModel,Modelに分けて管理しています。
まずはDisplayImageWindow.xamlです。
次はDisplayImageWindowViewModel.cs。
さきほどと違うのはBitmapImageからWriteableBitmapに変更になったところです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Prism.Mvvm;
using Microsoft.Practices.Prism.Commands;
using DisplayImage.Model;
using System.Windows.Media.Imaging;
namespace DisplayImage.ViewModel
{
class DisplayImageWindowViewModel: BindableBase
{
public DisplayImageWindowViewModel()
{
Displayer = new ImageDisplayer();
}
ImageDisplayer Displayer;
//=================
//Change Property
//=================
private string filePath;
public string FilePath
{
get { return filePath; }
set { SetProperty(ref filePath, value); }
}
private WriteableBitmap bmp;
public WriteableBitmap Bmp
{
get { return bmp; }
set { SetProperty(ref bmp, value); }
}
//=================
//DeligateCommand
//=================
private DelegateCommand fileOpenCommand;
public DelegateCommand FileOpenCommand
{
get { return fileOpenCommand ?? (fileOpenCommand = new DelegateCommand(FileOpen)); }
}
public void FileOpen()
{
Displayer.FileOpen();
FilePath = Displayer.FilePath;
Bmp = Displayer.GetImage();
}
}
}
最後にImageDisplayer.csです。
OpenCVSharpで画像を入力した後、表示するときだけWriteableBitmapに変換して渡します。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
using System.Windows.Media.Imaging;
using OpenCvSharp;
using OpenCvSharp.CPlusPlus;
using OpenCvSharp.Extensions;
namespace DisplayImage.Model
{
class ImageDisplayer
{
public ImageDisplayer()
{
}
public string FilePath { get; set; }
private Mat Img;
public void FileOpen()
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Open Image File.";
dlg.Filter = "Image File|*.jpg";
if (dlg.ShowDialog() == true)
{
FilePath = dlg.FileName;
Img = new Mat(FilePath);
}
}
public WriteableBitmap GetImage()
{
return Img.ToWriteableBitmap();
}
}
}