WPF Image 在运行时动态改变图像源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/397117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:54:46  来源:igfitidea点击:

WPF Image Dynamically changing Image source during runtime

wpfimagedynamicexecution

提问by John Batdorf

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, stretched, and pixelated. These are PNG files I'm working with and they look good prior to setting the source dynamically.

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

Here's the code I'm using to change the image's source.

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

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);

Here are the helper functions.

下面是辅助函数。

    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;
    }

And the XAML

和 XAML

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

And for Ref:

对于参考:

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

Anyone have any ideas what's up?

任何人有任何想法是怎么回事?

回答by Nir

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".

也许问题在于 WinForm 的图像大小调整,如果图像被拉伸,将图像控件上的 Stretch 设置为“Uniform”或“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

第二种选择是图像可能没有与像素网格对齐,您可以在我的博客上阅读它,网址http://www.nbdtech.com/blog/archive/2008/11/20/blurred-images-in-文件

回答by Junior Mayhé

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"));

回答by al.

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

“this”指的是Window对象

回答by yvisek

Like for me -> working is:

对我来说 -> 工作是:

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

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

回答by Jobi Joy

Try Stretch="UniformToFill" on the Image

在图像上尝试 Stretch="UniformToFill"

回答by Adnan Badar

Me.imgAddNew.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/SPMS;component/Images/Cancel__Red-64.png", UriKind.Relative))