Set Height and Width for images as WPF background - wpf

I need to set a image as background for WPF Window. I have set it like that:
<Window x:Class="DiagramView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="418" Width="1185" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
<Window.Background>
<ImageBrush ImageSource="../Images/Med.jpg" ></ImageBrush>
</Window.Background>
</Window>
But If I have height and width for window as orginal image, the image at window isn't not sharpen as it should be. I guess it happens because part of height and width is taken by window itself. What I properties I should use to have images with orginal width/height

In order to retain the original image size set the Stretch property to None:
<Window.Background>
<ImageBrush ImageSource="../Images/Med.jpg" Stretch="None" />
</Window.Background>

If you look on the ImageBrush Class page on MSDN, you'll see that there are a number of properties that you can use to position the image within the Brush. The following are of particular interest for positioning:
AlignmentX
AlignmentY
Stretch
Viewbox
ViewboxUnits
Viewport
ViewportUnits
Please see the linked page for their descriptions.

Related

Strange image stretch="none" behaviour

I have noticed some strange image display behaviour with stretch property set to "none"
For image which width > screen width & height > screen width, all area that is overlapping is clipped. (As expected)
Result: http://www.bildites.lv/images/urha41iqz8oz9hq8a6lu.png
For image which width > screen width & height < screen height, it's overlapping sides are clipped and it is centered vertically. (As expected)
Result: http://www.bildites.lv/images/kt4cc6jz0nogajyqmmc2.png
But for image which width < screen width & height > screen height, it get's resized to a thumbnail like size. (NOT as expected)
Result: http://www.bildites.lv/images/rqo9acdqg50lygndk4g.png
Here is a simple WPF app that demonstrates it:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" WindowState="Maximized" WindowStyle="None" Height="350" Width="525">
<Grid>
<Image Source="" Stretch="None"></Image>
</Grid>
Can someone please explain the third result?
Images I used for demonstration:
1) http://oi59.tinypic.com/2edqyis.jpg (it is resized, it was actually something like 4kx4k px)
2) http://www.bildites.lv/images/rjt4rl15g2ib7r74t77.jpg
3) http://www.bildites.lv/images/3f05cd42b2oqc1utmltt.jpg
The third image has its resolution set to 1000 DPI. You may change that to 96 DPI. See here.

Display a Huge image in WPF application

I wish to display a HD size 1920X1080 image in my WPF application. When I set the image source in the image control, only part of the image is displayed, size of image control.
I do not wish to auto-fit the HD image into the image control. Say the image control is size 640 X 480 then the 640X480 of HD image should be displayed. But when I pan the image then the next 640 X 480 of the image should be displayed.
I already have implemented TransformGroup
TransformGroup group = new TransformGroup();
ScaleTransform xform = new ScaleTransform();
group.Children.Add(xform);
TranslateTransform tt = new TranslateTransform();
group.Children.Add(tt);
But my problem is that entire image is not loaded initially.
If all you want is the ability to scroll your image...then use:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ScrollViewer HorizontalScrollBarVisibility="Visible">
<Image Stretch="None" Source="c:\mytestimage.png" />
</ScrollViewer>
</Grid>
</Window>
See these handy posts if you want to be more sophisticated/efficient:
WPF Image Zooming
Pan & Zoom Image
http://www.codeproject.com/Articles/85603/A-WPF-custom-control-for-zooming-and-panning
There is an alternate way to display the image. Set the max-es on the image, so it wont visually load larger than what is needed.
<Image Source="{Binding Source}"
Stretch="UniformToFill"
MaxHeight="200"
MaxWidth="400"/>
Image below shows two images loaded as is, a small one and a large one. The left side shows the large one exploding past the boundaries. The right one shows the proper shrinkage for the large one.
To keep the smaller image from oversizing (as shown on the right side) set the StretchDirection="DownOnly" to keep the smaller one from expanding.

Format and video Resolution like WMP with MediaElement WPF

Hey I would like to know if its possible to do something like that with the MediaElement in WPF:
http://img11.hostingpics.net/pics/374632Captur2e.png
And Like that:
http://img11.hostingpics.net/pics/403059Capture.png
So I When I resize my window the Default resolution stay the same.
Thanks
Just have a look at the code below. It is a MediaElement in a Border in a Grid. The Border is only there to show that the Grid is filled completely. Running this code and resizing the containing window shows that MediaElement well preserves the aspect ratio of its content (a behaviour that may be altered by setting the Stretch property).
<Grid>
<Border BorderThickness="5" BorderBrush="White" Background="Black">
<MediaElement Source="C:\Users\Public\Videos\Sample Videos\Wildlife.wmv"/>
</Border>
</Grid>

WPF window size

I am creating a Notepad like application in WPF. I want to set the window form height and width according to screen size . How can i get Screen height and width ?
Just bind SystemParameters properties to your window properties.
<Window x:Class="YourWindow"
Height="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}"
Width="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Width}">
See System.Windows.SystemParameters
You have properties like
PrimaryScreenWidth
PrimaryScreenHeight
VirtualScreenHeight
VirtualScreenWidth
WorkArea
etc.
This question might help as well: How can I get the active screen dimensions?

How to relative scale size of User Control?

How to relative scale size of User Control like image (in image editor).
Example (100%):
(source: stegnar.com)
Scaled down UC (70%):
(source: stegnar.com)
Well I achieve this in picture editor, but I would like in WPF. :) I need this to adjust my application to different screen resolution, while nothing hiding (no scrollbars).
You could try the ViewBox control that scales up/down its content so that it fills the available space.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1">
<Grid>
<Viewbox StretchDirection="Both" Stretch="Uniform">
<local:UserControl1 Height="600" Width="600"/>
</Viewbox>
</Grid>
you can place the whole container into a ViewBox
<Viewbox StretchDirection="Both" Stretch="Uniform">
<Grid>...</Grid>
</Viewbox>
you don't have to place each single textblock in it!
Use of Viewbox (as said by Milan Nankov) is a great idea. One thing that I must warn you is that it also zooms in or out other visual aspects as well.
For example, a Textbox with dimension 200 X 1000 is very different from a Textbox with dimension 20 X 100 zoomed in 10x.
WPF provides many layouting options which can change dimension of the controls according to the size of the container. But it doesn't change the size of the text. Viewbox overcomes this issue, but it introduces another issue. Check the image below which shows the same textbox in a viewbox before and after zooming.
One trick which could be used is to place every textblock in a viewbox. But I guess that would be an overkill, and I seriously don't having any backing for this trick. Please do check for yourself and reply whether it's practical or not.
Another trick could be to bind the control's height to the font size. We would be needing a converter in that case. Please refer to this reply.. Resize font in TextBox in Grid

Resources