WPF Image运行时动态更改图像源 [英] WPF Image Dynamically changing Image source during runtime

查看:1555
本文介绍了WPF Image运行时动态更改图像源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  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 =imgTitleHorizo​​ntalAlignment =LeftVerticalAlignment =BottomGrid.Column =1Grid.Row =1Stretch =None D:IsLocked = 假/> 

而对于参考文献:

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

任何人都有任何想法什么事情?

解决方案

我可以想到两件事:



首先,尝试加载图像:

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

也许问题是WinForm的图像调整大小,如果图像被拉伸,将图像控件上的拉伸Uniform或UnfirofmToFill。



第二个选项是,可能图像与像素网格不对齐,您可以在我的博客上阅读 http://www.nbdtech.com/blog/档案/ 2008/11/20 / blur-images-in-wpf.aspx


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.

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

            <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?

解决方案

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

这篇关于WPF Image运行时动态更改图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆