WinForm 调用 WPF窗体

1.创建WPF项目
2.创建WinForm项目并添加WPF相关的引用
WinForm 调用 WPF窗体_第1张图片
但是如果直接在winform的项目中添加wpf窗体还是有部分问题,图片的显示。
直接在XAML界面中用Source属性设置图片会出现错误。必须通过后台代码的方式来实现。

image1.Source = GetImageIcon(global::Com.JunXinEastern.Jcj.Properties.Resources.loginImg);

 Image image = new Image();
            image.Source = GetImageIcon(global::Com.JunXinEastern.Jcj.Properties.Resources.login_csyj1);
            ImageBrush ib = new ImageBrush();
            ib.ImageSource = image.Source;
            grid1.Background = ib;
private static BitmapImage GetImageIcon(System.Drawing.Bitmap bitmap)
        {
            BitmapImage bitmapImage = new BitmapImage();

            try
            {

                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                bitmap.Save(ms, bitmap.RawFormat);
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = ms;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                bitmapImage.Freeze();


            }
            catch (Exception ex)
            {

                //Utilities.ShowExceptionMessage(ex);
            }

            return bitmapImage;
        }

3.WPF窗体显示相关设置

namespace Mask
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow(double height, double width)
        {
            InitializeComponent();
            this.WindowStyle = WindowStyle.None;//设置窗体无边框
            this.AllowsTransparency = true;//窗体支持透明度
            this.Opacity = 0.6;//设置透明度为0.4
            this.Height = height; //设置窗体高度
            this.Width = width; // 设置窗体宽度
        }

        /// 
        /// 自定义属性
        /// 
        public bool showflg { get; set; }
    }
}

你可能感兴趣的:(C#,c#)