开发者

display image in window

I'd like to display an image in the 开发者_如何学Gowindow that expands automatically to the dimensions of that image. Right now, I have a code which shows it but I have to enter height and width of the window and the image to display it properly. Is it possible to do this?


You just need to set SizeToContent="WidthAndHeight" property on the window, see the following proof of concept:

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" SizeToContent="WidthAndHeight">
<Grid>
    <Image Stretch="None" Source="Images\img1.jpg"/>
</Grid>


Of course it's possible. Just before you load the image in your PictureBox control, adjust the size of the control to match the image size, then load the image in PictureBox.

Or even better, let .NET do the job (but this is winforms):

pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;

For WPF try this in your code (I didn't test though):

    image1.Width = width;
    image1.Height = height;

where width and height is the size of the picture you try to load.


A WPF window can be made to fit to its contents. In XAML set the SizeToContent property to "WidthAndHeight"

<Window x:Class ...
        SizeToContent="WidthAndHeight">

Now just make sure your Image container sizes to the source image and you should be set.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜