WPF: Strange image stretching in Buttons

后端 未结 3 1851
没有蜡笔的小新
没有蜡笔的小新 2021-01-23 03:51

I have multiple buttons, each having a 32x32 pixels PNG image. The strange thing is, that both buttons show different sizes (Yes, I triple checked that icons are really 32x32!).

相关标签:
3条回答
  • 2021-01-23 04:03

    The two images probably have different DPIs.

    0 讨论(0)
  • 2021-01-23 04:11

    To follow up on SLaks answer: To use the Pixel dimensions of the Image regardless of the DPI you can bind the Width and Height to the PixelWidth and PixelHeight of the Source like this

    <Button Name="toggleDetails" ToolTip="Details for Item" Click="toggleDetails_Click">  
        <Image Source="Icons/maximize.png"
               Stretch="None"
               Width="{Binding RelativeSource={RelativeSource Self}, Path=Source.PixelWidth}"
               Height="{Binding RelativeSource={RelativeSource Self}, Path=Source.PixelHeight}"/>
    </Button>
    
    0 讨论(0)
  • 2021-01-23 04:13

    If you have set Stretch="None" for the Image, and it still looks larger/smaller/blurry, then it's probably because of the DPI mismatch.

    For example, PNG files store DPI. Windows has a particular DPI. Check your system DPI and check the PNG DPI.

    In Photoshop you can go to Image -> Image Size and it will display the dots/inch box. You can also use it to change the DPI. Make sure you disable Resample Image checkbox so that you only alter the DPI. You need to use the Save for Web dialog for saving that change because the normal Save As won't save that information.

    enter image description here

    In my case, I had a PNG file with size of 24x24, and a DPI 72.009 and my system is at the default DPI. The picture looked larger and blurrier, now it's fine after adjusting the PNG DPI from 72.009 to 72 with Photoshop and using the Save for Web.

    0 讨论(0)
提交回复
热议问题