Display a Huge image in WPF application - wpf

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.

Related

Set Height and Width for images as WPF background

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.

WPF ZoomControl and Adorners

This my first post on stack overflow, I hope I get it right. I am using the ZoomControl from WPF Extensions to display an image with pan and zoom support:
<DockPanel Grid.Row="1" x:Name="canvas">
<Controls:ZoomControl x:Name="zoomControl">
<Canvas x:Name="canvas">
<Image x:Name="imageControl" Stretch="None" />
</Canvas>
</Controls:ZoomControl>
</DockPanel>
When the user selects an image with a bowse dialog, I load that image like so:
bmp = new BitmapImage(new Uri(fileName));
this.imageControul.Source = bmp;
I would like to added rectangles\adorners to specific locations (pixel coordinates) on the image the user loaded based on some image processing.
var r = new Rectangle();
r.StrokeThickness = 5;
r.Stroke = Brushes.Black;
r.Fill = Brushes.Transparent;
r.Width = width;
r.Height = height;
Canvas.SetLeft(r, y);
Canvas.SetTop(r, x);
canvas.Children.Add(r);
However, the rectangles are not placed in the expected locations? Wrong scale and location.
Thanks,
John
I expect the problem is that your Canvas is expanding to fill the space rather than being locked to the rectangle. Have a look with a tool like Snoop and see what the bounding boxes of the two are.
You might be able to fix it with Horizontal and VercticalAlignment on the canvas, set them to anything other than Stretch.
If that doesn't work restructure it like this
<ZoomBox>
<Grid>
<Image/>
<Canvas/>
</Grid>
</ZoomBox>
So the Image and the canvas are grouped by the parent Grid which is being transformed.

How do I display a bitmap image in my Wpf window using only XAML?

I have a blank window and i want a image in the center.I first tried dragging and dropping in the designer.Doesn't work.Then i tried an assortment of other examples on the internet.Couldn't get it to work either.one kinda worked but it wouldn't let me change the image's position.
Put an Image control in a Grid:
<Grid>
<Image Source=... Stretch="None" />
</Grid>

WPF - Zoom image (inside an constrained sized item control)

I would like to zoom an image in WPF and that the image visual render be inside a constrained sized item control.
For example:
<Canvas x:Name="m_canvas" MaxWidth="300" MaxHeight="300" >
<Image Source="..."
Width="300"
Height="300" />
</Canvas>
The zoom code:
var matrix = ((MatrixTransform)m_image.RenderTransform).Matrix;
var center = new Point(m_image.ActualWidth / 2, m_image.ActualHeight / 2);
center = matrix.Transform(center);
matrix.ScaleAt(delta.Scale.X, delta.Scale.Y, center.X, center.Y);
((MatrixTransform)m_image.RenderTransform).Matrix = matrix;
The problem is that when I'm zooming the image render size go larger that the canvas limit (300x300). I would like if the image can zoom only in the canvas.
I don't want to limit the max zoom, I want that if the render size of the image is larger that the canvas, it's stay inside. I don't want that it overlap the canvas
You could clip to the bounds of the Canvas:
<Canvas ClipToBounds="True" ...>
But I don't understand why you're using a Canvas in the first place. It's likely that there's a much nicer way to approach your particular problem without the need for hard-coded widths and heights and without any Canvas at all.

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