//WindowsBase, WindowsFormsIntegration, PresentationCore, PresentationFramework を参照に追加する
using System.IO;
using System.Windows.Forms.Integration; //ElementHost
using System.Windows.Controls; //Image
using System.Windows.Media; //Stretch
using System.Windows.Media.Imaging; //BitmapImage,BitmapCacheOption
namespace FormPicboxAndWpfImgTest
{
//直感的に分かりやすい名前への読み替え
enum EQuality
{
High = BitmapScalingMode.Fant, //InterpolationMode.HighQualityBilinear と同等の画質
Normal = BitmapScalingMode.Linear, //InterpolationMode.Bilinear と同等の画質
Low = BitmapScalingMode.NearestNeighbor //InterpolationMode.NearestNeighbor と同等の画質
}
//WPFのImageコントロールをホストするElementHost
//・フォームデザインで配置するなら、コンストラクタの呼び出しは不要。代わりにCreateImageControl()を呼び出す。
//・フォームデザインで配置しないなら、コンストラクタからCreateImageControl()を呼び出せばよい。
class WpfImageHost : ElementHost
{
public Image Image { get { return (this.Child as Image); } }
//コンストラクタ
public WpfImageHost() : base()
{
//CreateImageControl();
}
//Imageを作成し、ElementHostにセットする
public void CreateImageControl()
{
Image img = new Image();
this.Child = img;
img.Stretch = Stretch.Uniform; //縦横比を保ってImageがElementHostにフィットする
RenderOptions.SetBitmapScalingMode(img, (BitmapScalingMode)EQuality.High);
}
//BitmapImageを作成し、Imageにセットする
//・引数はFileStream,MemoryStreamを想定。
public bool SetBitmapImage(Stream stream)
{
BitmapImage bmp = new BitmapImage();
try
{
bmp.BeginInit();
bmp.StreamSource = stream;
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
bmp.EndInit();
}
catch { bmp = null; return false; }
if (bmp.CanFreeze) bmp.Freeze();
Image.Source = bmp;
return true;
}
} //class
} //namespace