티스토리 뷰

귀하의 GetGlowingImage()방법에서 새로운ImageSource

이 링크가 도움이 될 수 있습니다. 코드에서 WPF 이미지 소스 설정

편집하다:

차이점은 WindowsForms 코드에는 Properties.Resources._64px_Andromedahero ___ copia가 이미지 데이터를 포함하는 Image 변수의 이름이라는 것입니다. WPF 코드에서 "filename ...."문자열은 이미지 또는 이미지 소스가 아니며 파일 경로를 나타내는 문자열 일뿐입니다. 해당 경로를 사용하여 이미지 파일을로드해야합니다.

디자인 타임에 파일 이름을 지정할 수 있고 ImageSource가 자동으로 빌드되기 때문에 이해가되지 않는다는 것을 알고 있습니다. 코드에서 ImageSource (또는 파생 개체, 즉 BitmapSource)를 만들고 적절한 이미지를 여기에로드해야합니다.

편집 : 테스트되지 않은 시도 (위의 내 링크 확인) :

    public ImageSource GetGlowingImage(string name)
    {
            string fileName = string.Empty;
            
                    switch (name)
                            {
                                        case "Andromeda":
                                                        {
                                                                            fileName = "HeroGlowIcons/64px-Andromeda.gif";
                                                                                                break;
                                                                                                                }
                                                                                                                        }
                                                                                                                        
                                                                                                                                BitmapImage glowIcon = new BitmapImage();
                                                                                                                                
                                                                                                                                
                                                                                                                                        glowIcon.BeginInit();
                                                                                                                                                glowIcon.UriSource = new Uri("pack://application:,,,/ApplicationName;component/" + fileName);
                                                                                                                                                        glowIcon.EndInit();
                                                                                                                                                        
                                                                                                                                                                return glowIcon;
                                                                                                                                                                    }
                                                                                                                                                                    
-------------------

편집을 통해 리소스에 정적 이미지 세트가있는 것에 만족합니다. 맞다면 다음과 같이 할 수 있습니다.

<Application.Resources>
  <BitmapImage x:Key="andromeda64" UriSource="andromeda_64px.jpg" />
  </Application.Resources>
  

그런 다음 다음과 같이로드하십시오.

public ImageSource GetGlowingImage(string name)
{
  switch (name)
    {
        case "Andromeda":
              return (ImageSource)FindResource("andromeda64");
                  default:
                        return null;
                          }
                          }
                          

FindResource를 사용하려면 시각적 트리 (예 : 이벤트 처리기)에서 코드 숨김 상태 여야합니다. 그렇지 않은 경우 또는 리소스 사전에없는 항목을로드하려면 Cory의 답변을 참조하십시오 . 리소스 사용 및 재사용의 장점은 BitmapImage를 사용할 때마다 생성 할 필요가 없다는 것입니다 (이 경우이를 수행하는 데 드는 오버 헤드 비용은 미미합니다).

-------------------

다음과 같은 것 :

BitmapImage myBitmapImage = new BitmapImage(new Uri("../../../../Images/folder.png", UriKind.Relative));
myBitmapImage.CacheOption = BitmapCacheOption.OnLoad;
Image.Source = myBitmapImage;
-------------------

아마도 BitmapSource. MSDN 문서 에는 BitmapSource이미지 파일에서 만드는 방법에 대한 예제가 있습니다.

-------------------

코드 뒤에서 마술 문자열을 엉망으로 만드는 대신 EventTrigger를 사용하여 XAML에서 완전히 수행하는 것이 좋습니다.

이 링크는 도움이 될 것입니다

-------------------

버튼 클릭으로 System.Windows.Media.Brush를 지원하는 Grid 또는 기타 패널의 배경을 변경하려는 경우 자세히 알아보기

private void btnBackgroundImage_Click(object sender, RoutedEventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
        dlg.Filter = "Image Files (*.bmp;*.png;*.jpg;)|*.bmp;*.png;*.jpg";
            dlg.Multiselect = false;
                dlg.RestoreDirectory = true;
                
                    if (dlg.ShowDialog() == true)
                        {
                                txtBackImageName.Text = dlg.FileName;
                                        _layoutRoot.Background = new System.Windows.Media.ImageBrush(GetImageSource(dlg.FileName));
                                            }
                                            }
                                            
                                            
                                            public ImageSource GetImageSource(string filename)
                                            {
                                                string _fileName = filename;
                                                
                                                    BitmapImage glowIcon = new BitmapImage();
                                                    
                                                        glowIcon.BeginInit();
                                                            glowIcon.UriSource = new Uri(_fileName);
                                                                glowIcon.EndInit();
                                                                
                                                                    return glowIcon;
                                                                    }
                                                                    
-------------------
var converter = new Xamarin.Forms.ImageSourceConverter();
var imgSource = (Xamarin.Forms.ImageSource) converter.ConvertFromInvariantString(item.Poster);


출처
https://stackoverflow.com/questions/1904962
댓글
공지사항
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31