在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