Images in grid should get largest size as possible - c#

In my Windows Phone app, I need to have a maximum of 4 pictures horizontal next to each other. The images have always same width/height. Of course the width and also the height of the grid depends on screen. In case there are just 2 pictures, they should scale up to fill the space available. In case there are 4 they have to scale down.
How can I do this on xaml?

Put the images into a horizontal StackPanel inside a Viewbox:
<Viewbox>
<StackPanel Orientation="Horizontal">
<Image Source="http://tile.openstreetmap.org/1/0/0.png"/>
<Image Source="http://tile.openstreetmap.org/1/1/0.png"/>
<Image Source="http://tile.openstreetmap.org/1/0/1.png"/>
<Image Source="http://tile.openstreetmap.org/1/1/1.png"/>
</StackPanel>
</Viewbox>

Related

Scrollviewer to Display a Bigger Image on Windows Phone

I need to display a bigger image at original scale on a Windows Phone 8.1 App.
tried
ScrollViewer
and ViewBox.
None of them seems to work fine.
I want something like when an image is displayed in an html page, scrollable , movable, etc.
Any Idea whether it can be done without embedding a webbrowser ?
<Grid>
<ScrollViewer>
<Image Source="Assets/MyImage.png" Stretch="None" />
</ScrollViewer>
</Grid>
ScrollViewer is designed to handle content that is bigger than the surrounding container.You can scroll image by put image in grid try like this
<ScrollViewer>
<Grid>
<Image Source="Assets/MyImage.png" Stretch="None" />
</Grid>
</ScrollViewer>
In case if Grid is much smaller than the Scroll-viewer then try by giving height and width attribute to scroll viewer

Draw scaled picture over an Image

I have a WPF image, and I'm trying to draw something on it (Lets say a smiley, 2 blue circles for eyes and and a red ellipse for the mouth).
I've made a small Rectangle over the image, and made it stretched.
<Grid Name="mGrid">
<Rectangle Height="0" HorizontalAlignment="Stretch" Stretch="Fill" Name="mImageFrame" VerticalAlignment="Stretch" Width="0" />
</Grid >
I used a DrawingBrush to draw the circles in the rectangle.
Obviously this is wrong, because I have 2 problems:
1. The DrawingBrush seems to have a different scale. It will scale according to the largest item it's drawing.
2. I can only use one brush color.
So, how can I somehow draw over an image so it keeps the same proportions ?
And how can I use a different color for each shape ?
You can put Shapes on a Canvas that overlays the Image, and put the whole thing in a Viewbox. This requires that you define absolute coordinates by either setting the Image's Stretch property to None (and thus force the Image size to that of its Source bitmap) or explicitly set is Width or Height.
<Viewbox>
<Grid>
<Image Source="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"
Stretch="None"/>
<Canvas>
<Ellipse Canvas.Left="420" Canvas.Top="480"
Fill="Red" Width="100" Height="100"/>
... more shapes here ...
</Canvas>
</Grid>
</Viewbox>
One solution would be to put both the drawing and the Image into a ViewBox control. You can find out more about the Viewbox from the Viewbox Class page at MSDN. Basically, it provides the same stretching capabilities as the Image control.
You may also like to view the Shapes and Basic Drawing in WPF Overview page at MSDN.

How do I move an Image in a Canvas without making it bound to the outer Grid it belongs to?

I want to place an Image on the top of a certain tile in a tile based game I am making. Problem is, when I generate the random map, when the Rectangle children of the Canvas is drawn, it draws over the Image. I solved this issue by placing making the Image part of the outer Grid of the Window. Problem is that when I want to place the Image on a certain Rectangle within the Canvas, the co-ordinates are off since the Grid is larger than the Canvas. How can I limit the Image Margins to only that of the Canvas (the canvas borders as the limit) as opposed to the Grid borders as the limits?
E.g. Giving the Image a value of Margin.Left = 50 will place it in the perfect spot in the canvas, but it would be placed on a different location in the grid since its scale is larger.
// Image displays, but does not display when I add child Rectangles to the Canvas
<Canvas Height="700" HorizontalAlignment="Left" Name="canvas1" VerticalAlignment="Top" Width="700">
<Image Name="heroImage" Stretch="Fill" Source="hero.png" Height="74" Canvas.Left="558" Canvas.Top="602" />
</Canvas>
<Grid>
<Canvas Height="700" HorizontalAlignment="Left" Name="canvas1" VerticalAlignment="Top" Width="700">
// Removed the Image
</Canvas>
// Placed the Image outside the Canvas, but now it will draw according to the Grid's Margin limits and not the Canvas Margin limits
<Image Name="heroImage" Stretch="Fill" Source="hero.png" Height="74" Canvas.Left="558" Canvas.Top="602" />
</Grid>
Image inside the Canvas, perfectly positioned
Image in the grid, wrongly placed since I used Canvas Margin co-ordinates
The first way you did it was good, your image should be a child of your Canvas to do Canvas.Left...
To solve you problem can you just put your image on top of all other rectangle, with ZIndex?
Canvas.ZIndex="3 or whatever"
The image with value 2 is drawn over the Rectangle with value 1. So you can pu a high number...
I think that you could use your image inside the Canvas and if you want the image over the Rectangle, the you need set the image's (Palen.ZIndex or Canvas.ZIndex) property greater than the Rectangles one.
For instance:
<Canvas Height="700" HorizontalAlignment="Left" Name="canvas1" VerticalAlignment="Top" Width="700">
<Image Name="heroImage" Stretch="Fill" Source="hero.png" Height="74" Canvas.Left="558" Canvas.Top="602" Canvas.ZIndex="1000" />
<Rectangle x:Name="Some Rectangle" Canvas.ZIndex="1"/>
</Canvas>
The image will be showed over the rectangle. I think this is the solution. Putting the image out the canvas cause will be very complex to set the exactly position you want.

Zooming into image in Windows Store apps

I have a ScrollViewer with an Image Control in it. It displays a rather large image. I want my user to be able to zoom into the image using gestures. I therefore enabled the ZoomMode on the Scrollviewer. However the Scrollviewer automatically scrolls back to the left "edge" of the image whenever the user releases its finger, making effectively zooming in and out of the image impossible.
This is the Template i am using:
<DataTemplate x:Key="SingleItemTemplate">
<ScrollViewer ZoomMode="Enabled">
<Grid Margin="5,0,5,0">
<Image Source="{Binding ImageUrlHighRes}">
</Image>
</Grid>
</ScrollViewer>
</DataTemplate>
How can i solve this problem?
You need to enable the horizontal scrolling as well (turned off by default)
HorizontalScrollBarVisibility = "Auto"

Silverlight zoom area with canvas and scrollview

I have simple canvas with items and i need to add for scroll view as parent for my canvas.
But i fased with problem that after set
canvas.RenderTransform=new ScaleTransform(){...}
Scroolbars not appears or working not correctly.
Will be glad for any information.
The render transform occurs much later in the UI rendering process. It ultimately performs a matrix transform on controls rendering. The scroll viewer will be completely unware of this transform, its scrollbars will be based on the un-transformed size of the original Canvas.
The silverlight toolkit contains a LayoutTransformer control. This control applies a transform to its content as part of the layout process and reports the post-transform size as its desired size.
Consider this:-
<ScrollViewer Width="200" Height="200" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<toolkit:LayoutTransformer>
<toolkit:LayoutTransformer.LayoutTransform>
<ScaleTransform ScaleX="2" ScaleY="2" />
</toolkit:LayoutTransformer.LayoutTransform>
<Canvas Width="150" Height="150" Background="Aquamarine">
<Rectangle Fill="Blue" Canvas.Top="10" Canvas.Left="10" Width="30" Height="30" />
</Canvas>
</toolkit:LayoutTransformer>
</ScrollViewer>
Whilst the Canvas has a size (150) smaller than the containing scroll viewer (200), it is scaled so that it would be larger (300). The LayoutTransformer reports its desired size as 300, the post-transform size of the canvas. Hence the ScrollViewer displays scroll bars to accomodate it. Without the benefit of the LayoutTransformer the ScrollViewer would only see the Canvas as having a size 150 despite any applied RenderTransform.

Categories

Resources