Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 451|回复: 0

解决WPF图片模糊最佳方法(绑定PixelWidth与PixelHeight)

[复制链接]
  • TA的每日心情
    奋斗
    2024-4-6 11:05
  • 签到天数: 748 天

    [LV.9]以坛为家II

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    705612
    发表于 2021-5-24 11:55:30 | 显示全部楼层 |阅读模式

         从事WPF开发一年有余,对于图片显示模糊相信很多人都遇到过。网络上查找能得到一堆解决方法,但都是会带来其他负面影响得不到最佳效果。其实,有些图片会因为垂直分辨率/水平分辨率不同而造成在WPF界面上显示出现模糊。WPF默认是96Dpi,但有些图片可能是72DPI甚至更低或更高,这样就会出现图片显示后被放大或缩小。解决的方法是通过绑定图片的Source.PixelHeight与Source.PixelWidth并结合Stretch="Fill"或UseLayoutRounding="True"来限制图片大小达到最佳效果。

     

    1.先看本人制作的两张演示图片(注意是不同分辨率的图片):

            (分辨率96DPI  宽高67*71)       (分辨率72DPI  宽高:50*53)

    2.编写一个简单的WPF应用程序用来显示图片

        2.1 前端代码MainWindow.xaml:

    <Window x:Class="ImgDemo.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="图片显示模糊示例" Height="539.818" Width="671.169">
        <Window.Resources>
            <BitmapImage x:Key="vs1" UriSource="/ImgDemo;component/Images/vs1.png"></BitmapImage>
            <BitmapImage x:Key="vs2" UriSource="/ImgDemo;component/Images/vs2.png"></BitmapImage>       
        </Window.Resources>
        <Grid>
            <Image HorizontalAlignment="Left"  Source="{StaticResource vs1}" Stretch="None"  VerticalAlignment="Top" Margin="77,75,0,0" />
            <Image HorizontalAlignment="Left"  Source="{StaticResource vs2}" Stretch="None"  VerticalAlignment="Top" Margin="486,75,0,0" />
            <TextBlock Name="txt1" HorizontalAlignment="Left" Margin="77,151,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Height="91" Width="167"/>
            <TextBlock Name="txt2" HorizontalAlignment="Left" Margin="486,151,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Height="82" Width="167"/>
    
        </Grid>
    </Window>

     

         2.2 后端代码MainWindow.xaml.cs

    namespace ImgDemo
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                this.Loaded += (s, e) =>
                {
                    BitmapImage vs1 = this.FindResource("vs1") as BitmapImage;
                    BitmapImage vs2 = this.FindResource("vs2") as BitmapImage;
                    txt1.Text = string.Format("Source.PixelWidth:{0}\r\nSource.PixelHeight:{1}", vs1.PixelWidth, vs1.PixelHeight);
                    txt2.Text = string.Format("Source.PixelWidth:{0}\r\nSource.PixelHeight:{1}", vs2.PixelWidth, vs2.PixelHeight);
    
                };
            }
        }
    }

     

       2.3 运行结果:(注意右边的图片因为分辨率原因明显模糊且与实际大小不符合

      

    3.解决方法修改MainWindow.xaml.cs代码如下:

    <Window x:Class="ImgDemo.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="图片显示模糊示例" Height="539.818" Width="671.169">
        <Window.Resources>
            <BitmapImage x:Key="vs1" UriSource="/ImgDemo;component/Images/vs1.png"></BitmapImage>
            <BitmapImage x:Key="vs2" UriSource="/ImgDemo;component/Images/vs2.png"></BitmapImage>       
        </Window.Resources>
        <Grid>
            <Image HorizontalAlignment="Left"  Source="{StaticResource vs1}" Stretch="None"  VerticalAlignment="Top" Margin="77,75,0,0" />
            <Image HorizontalAlignment="Left"  Source="{StaticResource vs2}" Stretch="Fill"  VerticalAlignment="Top" Margin="486,75,0,0" 
                   Width="{Binding Source.PixelWidth, Mode=OneWay, RelativeSource={RelativeSource Self}}" 
                   Height="{Binding Source.PixelHeight, Mode=OneWay, RelativeSource={RelativeSource Self}}"/>
            <TextBlock Name="txt1" HorizontalAlignment="Left" Margin="77,151,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Height="91" Width="167"/>
            <TextBlock Name="txt2" HorizontalAlignment="Left" Margin="486,151,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Height="82" Width="167"/>
    
        </Grid>
    </Window>

    4.最终目标运行效果

       

    5.总结

     对于不同分辨率的图片如果在Winform显示是不会出现放大模糊的情况,但WPF就是让人纠结搞到程序员不停的绕圈找方法。其实,本来这可以避免的,只要要求美工制作标准分辨率的图片96DPI,但作为程序员还是要预防一下,确保界面显示的完美。由于语文水平有限,表达不明确之处请多包涵。

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-18 15:45 , Processed in 0.079744 second(s), 30 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表