jil*_*luo 96 size wpf resize image
我在WPF中显示图像时遇到问题.
这是我的代码:
<Button HorizontalAlignment="Left" Grid.Column="1" Grid.Row="5" Margin="0,5">
<Button.Content>
<StackPanel Orientation="Horizontal" Margin="10,0">
<Image Source="/images/user_add.png" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center" Width="24" Height="24" />
<TextBlock Text="??" />
</StackPanel>
</Button.Content>
</Button>
Run Code Online (Sandbox Code Playgroud)
我有一个原始大小为32*32的图像,但是当我运行上面的代码时,图像将拉伸以填充所有空间,超出其原始大小.我还将"Stretch"属性设置为"None",但似乎它不起作用.
那么,我该如何解决这个问题呢?谢谢!
Pay*_*aya 124
这是一个类似的问题.一般来说设定Stretch="None"
就够了.
DPI在元数据中设置图像也非常重要.我花了很长时间才弄清楚如果图像的DPI与显示器的DPI(通常为96)不同,WPF会自动调整图像大小,因为它试图与DPI无关.
<Image Source="Images/Background.png" UseLayoutRounding="True" SnapsToDevicePixels="True" Width="600" Height="800" Stretch="Fill" />
Run Code Online (Sandbox Code Playgroud)
这个对我有用,对于600x800 pixels
和和的图像96dpi
.
@ rishad2m8如果大小未知,可以先用https://msdn.microsoft.com/en-us/library/system.drawing.image.size(v=vs.110).aspx检测大小.我猜.
尽量不要指定宽度或高度,请改为使用它:
<Image Source="/images/user_add.png" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center" />
Run Code Online (Sandbox Code Playgroud)