How to set the background of Rectangle with a picture? - c#

I create a Rectangle
public void Set(Rectangle maps, int y, int x) {
Map.Children.Add(maps);
maps.SetValue(Grid.RowProperty, x);
maps.SetValue(Grid.ColumnProperty, y);
}
But How to change the background with "Resources/1.jpg"?

Like this:
<Rectangle>
<Rectangle.Fill>
<ImageBrush ImageSource="/YourAppName;component/Resources/1.jpg" />
</Rectangle.Fill>
</Rectangle>
EDITED AGAIN (Sorry)
Or in C#
maps.Fill = new ImageBrush {
ImageSource = new BitmapImage(new Uri(#"pack://application:,,,/YourAppName;component/Resources/1.jpg", UriKind.Absolute))
};

I was having trouble using the "/YourAppName;" portion of the address as suggested by #Jonny Piazzi. It probably works, I just couldn't get it to. Alternatively, I was able to make this method work.
1) I added the image to my project in a folder I created: Images > Backgrounds > JellyFishBackground.jpg
2) I right clicked the image in Solution Explorer > Properties > Set Build Action to Resource
3) Build project
4) Simply target the image as such: (in my case I targeted a row of my grid, and used a Stretch property, which are beyond the scope of this question, just fyi to avoid confusion)
<Rectangle Grid.Row ="0">
<Rectangle.Fill>
<ImageBrush ImageSource="/Images/Backgrounds/JellyFishBackground.jpg" Stretch="UniformToFill"/>
</Rectangle.Fill>
</Rectangle>

Related

WPF - InkCanvas - Background Image

I successfully changed the background of my InkCanvas from code behind with image using following code:
ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = new BitmapImage(new Uri("temp.jpg", UriKind.Relative));
inkCanvas1.Background = imageBrush;
Now I want to resize the resolution of background image only.
For example, if my InkCanvas size is 500 x 500, I want to show the background image in my InkCanvas at center with resolution of 300 x 300.
Is this possible ?
Any help in this regard will be highly appreciated..
This, of course, there are even many ways, for instance, you can set the RelativeTransform property:
<InkCanvas.Background>
<ImageBrush>
<ImageBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform CenterY="0.5" CenterX="0.5" ScaleX="2" ScaleY="2"/>
</TransformGroup>
</ImageBrush.RelativeTransform>
</ImageBrush>
</InkCanvas.Background>
That way, your background image is twice the size of the previous one. If you want more precise control of the background, you can use the VisualBrush, just like below:
<Grid.Background>
<VisualBrush>
<VisualBrush.Visual>
<Image Width="200" Height="200"></Image>
</VisualBrush.Visual>
</VisualBrush>
</Grid.Background>

WPF Static Background Image

How do you get a static background image in WPF. With an image set using the imagebrush it always expands or contracts when I resize or extend my window. How do I make the image static so when I extend a window more of the image is revealed and not resized? ie - similar to web design css or something like that.
set ImageBrush property Stretch to None (default is Fill).
<Window.Background>
<ImageBrush Stretch="None" Source="src"/>
</Window.Background>
Might also need to anchor it to a corner (probably top left) using AlignmentY & AlignmentX.
<Window.Background>
<ImageBrush Stretch="None"
Source="src"
Viewbox="0.25, 0.25, 1, 1"
AlignmentY="Top"
AlignmentX="Left"/>
</Window.Background>

Wrap image around canvas in WPF C#

How can I wrap an image around the canvas like this? The obvious way I can think of is to duplicate the image, and offset by the width/height of the image in the opposite direction. Is there another way to achieve this?
You could fill a Rectangle with an ImageBrush with this image, and set its TileMode and Viewport properties as needed.
For example:
<Rectangle Width="128" Height="128">
<Rectangle.Fill>
<ImageBrush ImageSource="Images\Tile.png" TileMode="Tile"
ViewportUnits="Absolute" Viewport="64,64,128,128"/>
</Rectangle.Fill>
</Rectangle>
The above XAML creates the following output:
from this source image:
No. There is no other way around. You will have to draw all images. Because a control doesn't split up.
You can calculate and then generate Image boxes to show no. of images based on offset.

Adding collection of Images in Xaml

I would like to paint Images to the XAML page in Windows Store App.
The main goal is that:
Adding Images (e.g. flower leaf) to a circle on the center like that:
I have a simple solution to that, but its is very redundant.
<Image Height="200" Width="200" Source="{Binding ActualImage.NormalUri}">
<Image.RenderTransform>
<RotateTransform Angle="12"></RotateTransform>
</Image.RenderTransform>
</Image>
... // And 28 other like these
<Image Height="200" Width="200" Source="{Binding ActualImage.NormalUri}">
<Image.RenderTransform>
<RotateTransform Angle="360"></RotateTransform>
</Image.RenderTransform>
</Image>
How can I do that with a binding of a Image collection? What XAML control should I use?
Use a custom class which inherits from ItemsControl. You can then override the necessary functions, such as ones to determine the angle to rotate between each item. I think it's likely you'll want to use this PrepareContainerForItemOverride for this.
One thing to note that you will have to do is to define a new ItemsPanel. The default is a StackPanel, which will not work. You'll likely want to use something like a Canvas, which allows you to explicitly position items in it.
The Solution of Nate Diamond is much more nicer and better, but I solved it from code-behind for earn the easier way:
foreach (Petal petalObject in MainPageViewModel.Petals)
{
var petalImage = new Image
{
Height = petalObject.Height,
Width = petalObject.Width,
RenderTransform = new RotateTransform() {Angle = petalObject.Angle},
Source = new BitmapImage(new Uri(petalObject.NormalUri)),
};
PetalsGrid.Children.Add(petalImage);
}

Two Image Layers and OpacityMask

I'm trying to crop a circle from one image, and put it on top another image in WPF.
The Circle's center changes according to the mouse movements, and needs to be bounded dynamically.
I tried to position two images on top of each other, and use a third image that I draw in real time as an opacity mask.
Could you please provide short code to solve this problem efficiently ?
The code below describes what you can do with an OpacityMask. It's a little counterintuitive, because we expect a XAML rendering to layer elements bottom-to-top.
However, in this case you want your "background" image to layer on top of the foreground, because the OpacityMask will serve to display only that portion of the foreground described by the position and size of the VisualBrush, rendering the rest transparent. It's given as follows:
<Grid x:Name="MainGrid" MouseMove="Grid_MouseMove">
<Rectangle Fill="Red" ></Rectangle>
<Rectangle Fill="Green">
<Rectangle.OpacityMask>
<VisualBrush Stretch="None" >
<VisualBrush.Visual>
<Ellipse Width="40" Height="40" StrokeThickness="1" Fill="Black" />
</VisualBrush.Visual>
<VisualBrush.RelativeTransform>
<TransformGroup>
<TranslateTransform x:Name="OpacityFilterTransform" X="1" Y="1"/>
</TransformGroup>
</VisualBrush.RelativeTransform>
</VisualBrush>
</Rectangle.OpacityMask>
</Rectangle>
</Grid>
Then, this event handler code computes the position of the ellipse and applies it to the OpacityFilter's TranslateTransform object, giving you control over the position of the image.
private void Grid_MouseMove(object sender, MouseEventArgs e)
{
var position = e.GetPosition(this);
var height = MainGrid.ActualHeight;
var width = MainGrid.ActualWidth;
// with the position values, interpolate a TranslateTransform for the opacity mask
var transX = position.X / width;
var transY = position.Y / height;
OpacityFilterTransform.X = transX - 0.5;
OpacityFilterTransform.Y = transY - 0.5;
}
This solution should work for any descendant of Visual you care to layer.

Categories

Resources