【问题标题】:WPF Image Dynamically changing Image source during runtimeWPF Image 在运行时动态更改图像源
【发布时间】:2010-09-28 16:01:45
【问题描述】:

我有一个带有标题的窗口。当用户从下拉列表中选择一个选项时,标题图像可以改变。问题是当图像加载时,它是模糊的、拉伸的和像素化的。这些是我正在使用的 PNG 文件,它们在动态设置源之前看起来不错。

这是我用来更改图像来源的代码。

string strUri2 = String.Format(@"pack://application:,,,/MyAssembly;component/resources/main titles/{0}", CurrenSelection.TitleImage);
Stream iconStream2 = App.GetResourceStream(new Uri(strUri2)).Stream;
imgTitle.Source = HelperFunctions.returnImage(iconStream2);

这里是辅助函数。

    public static BitmapImage returnImage(Stream iconStream)
    {
        Bitmap brush = new Bitmap(iconStream);
        System.Drawing.Image img = brush.GetThumbnailImage(brush.Height, brush.Width, null, System.IntPtr.Zero);
        var imgbrush = new BitmapImage();
        imgbrush.BeginInit();
        imgbrush.StreamSource = ConvertImageToMemoryStream(img);
        imgbrush.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
        imgbrush.EndInit();
        var ib = new ImageBrush(imgbrush);
        return imgbrush;
    }

    public static MemoryStream ConvertImageToMemoryStream(System.Drawing.Image img)
    {
        var ms = new MemoryStream();
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        return ms;
    }

还有 XAML

            <Image x:Name="imgTitle" HorizontalAlignment="Left" VerticalAlignment="Bottom" Grid.Column="1" Grid.Row="1" Stretch="None" d:IsLocked="False"/>

对于参考:

xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

有人知道怎么回事吗?

【问题讨论】:

    标签: wpf image dynamic execution


    【解决方案1】:

    在图像上尝试 Stretch="UniformToFill"

    【讨论】:

      【解决方案2】:

      我能想到两件事:

      首先,尝试加载图片:

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

      可能问题在于 WinForm 的图像大小调整,如果图像被拉伸,请将图像控件上的 Stretch 设置为“Uniform”或“UnfirofmToFill”。

      第二个选项可能是图像未与像素网格对齐,您可以在我的博客http://www.nbdtech.com/blog/archive/2008/11/20/blurred-images-in-wpf.aspx 上了解它

      【讨论】:

        【解决方案3】:

        嘿,这个有点难看,但只有一行:

        imgTitle.Source = new BitmapImage(new Uri(@"pack://application:,,,/YourAssembly;component/your_image.png"));
        

        【讨论】:

        • 如果你有你的图像在一个子文件夹中放 ico.Source = new BitmapImage(new Uri(@"pack://application:,,,/YourAssembly;component/YourFolder/your_image.png") );
        【解决方案4】:

        这就是它对我来说非常有效的方式。 在窗口资源中添加图片。

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

        然后代码是这样的。

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

        “this”指的是Window对象

        【讨论】:

          【解决方案5】:
          Me.imgAddNew.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/SPMS;component/Images/Cancel__Red-64.png", UriKind.Relative))
          

          【讨论】:

            【解决方案6】:

            喜欢我 -> 工作是:

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

            【讨论】:

            • 在下面的代码中 BlackPlayerPic 是 Image 类型。 BitmapImage activeBlackPlayer = new BitmapImage(new Uri("C:\\png\\turn_black.png")); BlackPlayerPic.Source = activeBlackPlayer;
            猜你喜欢
            • 2011-01-26
            • 1970-01-01
            • 2010-10-30
            • 2014-09-25
            • 2015-05-30
            • 1970-01-01
            • 2010-09-12
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多