WPF Image Dynamically changing Image source during runtime

前端 未结 6 1654
情深已故
情深已故 2020-12-09 02:59

I have a window with a title on it. When the user selects a choice from a drop down list, the title image can change. The problem is when the image loads, it\'s a blurred, s

相关标签:
6条回答
  • 2020-12-09 03:13

    Try Stretch="UniformToFill" on the Image

    0 讨论(0)
  • 2020-12-09 03:25

    I can think of two things:

    First, try loading the image with:

    string strUri2 = String.Format(@"pack://application:,,,/MyAseemby;component/resources/main titles/{0}", CurrenSelection.TitleImage);
    imgTitle.Source = new BitmapImage(new Uri(strUri2));
    

    Maybe the problem is with WinForm's image resizing, if the image is stretched set Stretch on the image control to "Uniform" or "UnfirofmToFill".

    Second option is that maybe the image is not aligned to the pixel grid, you can read about it on my blog at http://www.nbdtech.com/blog/archive/2008/11/20/blurred-images-in-wpf.aspx

    0 讨论(0)
  • 2020-12-09 03:25

    Here is how it worked beautifully for me. In the window resources add the image.

       <Image x:Key="delImg" >
        <Image.Source>
         <BitmapImage UriSource="Images/delitem.gif"></BitmapImage>
        </Image.Source>
       </Image>
    

    Then the code goes like this.

    Image img = new Image()
    
    img.Source = ((Image)this.Resources["delImg"]).Source;
    

    "this" is referring to the Window object

    0 讨论(0)
  • 2020-12-09 03:31

    Hey, this one is kind of ugly but it's one line only:

    imgTitle.Source = new BitmapImage(new Uri(@"pack://application:,,,/YourAssembly;component/your_image.png"));
    
    0 讨论(0)
  • 2020-12-09 03:31

    Like for me -> working is:

    string strUri2 = Directory.GetCurrentDirectory()+@"/Images/ok_progress.png"; image1.Source = new BitmapImage(new Uri(strUri2));

    0 讨论(0)
  • 2020-12-09 03:34
    Me.imgAddNew.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/SPMS;component/Images/Cancel__Red-64.png", UriKind.Relative))
    
    0 讨论(0)
提交回复
热议问题