在winform中使用wpf窗体

在winform项目,通过引用dll可以添加WPF窗体,如下

 

但是如果直接在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;
        }

使用的winform项目中Resources.resx资源中的图片,这里图片还要求是png格式的才能成功加载,jpeg的则不行。

posted @ 2018-06-21 14:15  清晨时光  阅读(2097)  评论(0编辑  收藏  举报