在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的则不行。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM