Set Background to canvas - c#

how i can set background on string path?
canvas1.Background = "/path/";
please help!!! thanks :)

I think you are looking for the ImageBrush class:
ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = new BitmapImage(new Uri(#"/path/", UriKind.Relative));
canvas1.Background = imageBrush;

Use this
mycanvas.Background = new ImageBrush()
{ ImageSource = new BitmapImage((new Uri(mypath, UriKind.Absolute))) };

Related

Windows Phone 8 C# - Change the name of an xaml item in a foreach loop

I have a small problem here and I don't know how to fix it myself.
What i'm trying to do, is i want to change the image source of every item in the array (names).
This is what my code looks like now:
(the m at m.Source is where the name should go)
private void Stars()
{
string[] LevelPick = new string[] {"Stage1Level1", "Stage1Level2", "Stage3Level3" };
foreach (string m in LevelPick)
{
string Stars = appSettings[""+m+""].ToString();
if(Stars == "0")
{
BitmapImage bm = new BitmapImage(new Uri("/Resources/Images/GeenSter.png", UriKind.RelativeOrAbsolute));
m.Source = bm; // error here
}
else
{
BitmapImage bm = new BitmapImage(new Uri("/Resources/Images/GeenSter.png", UriKind.RelativeOrAbsolute));
m.Source = bm; // same here
}
}
}
The error that i get is:
'string' does not contain a definition of 'Source'.
Does anybody know how to fix this?
Thanks in advance!
You could simply have an array of your Image controls, instead of an array of their names:
var images = new Image[] { Stage1Level1, Stage1Level2, Stage3Level3 };
Now you could iterate over these images:
foreach (var image in images)
{
var stars = appSettings[image.Name].ToString();
if (stars == "0")
{
image.Source = new BitmapImage(new Uri(...));
}
...
}

How to add a PushPin item to a map? Windows Phone

I'm trying to draw a pushpin to a map in the map page of my application but when I add the code to overlay a pushpin to the map, I get the following errors:
The type or namespace name PushPin could not be found (are you missing a using directive or an assembly reference?)
and
Microsoft.Phone.Maps.Controls.Map does not contain a definition for Children and no extension method Children accepting a first argument of type Microsoft.Phone.Maps.Controls.Map could be found
I understand from this that I'm missing a reference? I have references for controls.maps and contols.toolkit, so I can't understand why.
Also this is the code I'm using to draw the pushpin:
PushPin myPin = new Pushpin();
myPin.Location = MyGeoPosition;
myPin.Content = "My car";
MyMap.Children.Add(myPin);
Try this.
MapLayer layer1 = new MapLayer();
Pushpin pushpin1 = new Pushpin();
pushpin1.GeoCoordinate = MyGeoPosition;
pushpin1.Content = "My car";
MapOverlay overlay1 = new MapOverlay();
overlay1.Content = pushpin1;
overlay1.GeoCoordinate = MyGeoPosition;
layer1.Add(overlay1);
myMap.Layers.Add(layer1);
You create new overlay for each pushpin, add all overlays to a layer, and add the layer to the map element.
You can try..
BitmapImage myImage1; myImage1 = new BitmapImage(new Uri("/Assets/Images/pushpin-google-hi.png", UriKind.RelativeOrAbsolute));
var image = new Image();
image.Width = 50;
image.Height = 50;
image.Opacity = 100;
image.Source = myImage1;
var mapOverlay = new MapOverlay();
mapOverlay.Content = image;
mapOverlay.GeoCoordinate = new GeoCoordinate(lats, lons);
var mapLayer = new MapLayer();
mapLayer.Add(mapOverlay);
MyMap.Layers.Add(mapLayer);
MyMap.SetView(new GeoCoordinate(lats, lons), 16);
Try this. First I have this in a utils class:
public MapLayer GetCurrentPosition (out MapOverlay myPositionOverlay)
{
MapLayer myPositionLayer = new MapLayer();
myPositionOverlay = new MapOverlay();
Image myPositionImage = new Image();
myPositionImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Assets/myImagePin.png", UriKind.RelativeOrAbsolute));
myPositionImage.Height = 70;
myPositionImage.Visibility = Visibility.Visible;
myPositionImage.HorizontalAlignment = HorizontalAlignment.Center;
myPositionOverlay.Content = myPositionImage;
myPositionLayer.Add(myPositionOverlay);
return myPositionLayer;
}
Then this goes on your MapLoaded method:
MapLayer mypositionArrow = placePushPins.GetCurrentPosition(out myPositionOverlay);
MyMap.Layers.Add(mypositionArrow);
Then, if you want your pushpin not to be static, to move according to your position, add this to your watcher:
myPositionOverlay.GeoCoordinate = myCurrentPositionCoordinate;
To make the pushpin move smoothly just implement animation like this: http://blogs.bing.com/maps/2014/09/25/part-2-bring-your-maps-to-life-creating-animations-with-bing-maps-net/

Array of image in WPF

How to put into Array some source to WPF Controls called Image? O this forum i find, but how to make array?
BitmapImage logo = new BitmapImage()
logo.BeginInit();
logo.UriSource = new Uri("pack://application:,,,/img/3.jpg");
logo.EndInit();
tmpimage.Source = logo;
But i need sometring like this:
Image[] img = new Image[3];
img[0].Source = new Uri("pack://application:,,,/img/3.jpg");
tmpimage.Source = img[0];
Image[] images = new Image[3] { new Image(), new Image(), new Image() };
images[0].Source = new BitmapImage(new Uri("pack://application:,,,/img/3.jpg"));
images[1].Source = new BitmapImage... // etc...
Alternatively, make your image factory a function and use LINQ:
Image CreateBitmap(string uri)
{
return new Image() { Source = new BitmapImage(new Uri(uri)) };
}
Image[] GetImages()
{
var imageUris = new[]
{
"pack://application:,,,/img/3.jpg",
"pack://application:,,,/img/elephant.jpg",
"pack://application:,,,/img/banana.jpg"
};
return imageUris.Select(CreateBitmap).ToArray();
}
Dynamic Array of BitmapImage :
BitmapImage[] iHab;
BitmapImage CreateBitmap(string uri)
{
return new BitmapImage(new Uri(uri));
}
BitmapImage[] GetImages()
{
string currDir = Directory.GetCurrentDirectory();
string[] imageUris;
//Get directory path of myData
string temp = currDir + "\\Media\\hcia\\";
imageUris = Directory.GetFiles(temp, "habitation*.png");
return imageUris.Select(CreateBitmap).ToArray();
}
private void Rec_hab_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
iHab = GetImages();
pointer.Source = iHab[7]; // the 7th image : can be manipulated with an i++
}

How to Create ImageBrush in C# code

In XAML:
<Rectangle Stroke="Aqua" Opacity="0.7" StrokeThickness="10" Canvas.Left="24" Canvas.Top="22" Height="86" Width="102">
<Rectangle.Fill>
<ImageBrush ImageSource="C:\Users\xiaorui.dong\Pictures\profile.jpeg"></ImageBrush>
</Rectangle.Fill>
</Rectangle>
XAML works fine, but how to create the above ImageBrush in C# code:
C# should be like this:
Rectangle rectangle = new Rectangle();
rectangle.StrokeThickness = 10;
rectangle.Height = 200;
rectangle.Width = 100;
rectangle.SetValue(Canvas.LeftProperty, 100d);
rectangle.SetValue(Canvas.TopProperty, 100d);
rectangle.Fill = new ImageBrush(new BitmapImage(new Uri(#"C:\Users\xiaorui.dong\Pictures\profile.jpeg")));
I'm guessing the problem is with locating the image and that the exception you're getting is because you don't provide the UriKind parameter. Try giving UriKind.Relative as a parameter to the Uri:
rectangle.Fill = new ImageBrush(new BitmapImage(
new Uri(#"C:\Users\xiaorui.dong\Pictures\profile.jpeg", UriKind.Relative)));
In c# you can use it like , first thing is remove the fill from XAML then use the same code in c# as you have used it . It must work.
ImageBrush ib = new ImageBrush();
ib.ImageSource = new BitmapImage(new Uri("your path",UriKind.Relative));
rectangle.Fill = ib;
This code creates ImageBrush using image "Assets/image.png" which is part of the UWP project. Relative paths are unavailable in UWP version of ImageBrush.
var imageBrush = new ImageBrush();
imageBrush.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/image.png"));

WPF Setter Class

I want to create a button in c# with no content but just a image within it.
I dont know how to use the setter. Can anyone enlighten me?
Setter setter = new Setter();
setter.Property = Image.SourceProperty;
setter.Value = "asd.jpg"; //<--error
Style Rstyle = new Style();
Rstyle.TargetType = typeof(Button);
Rstyle.Setters.Add(setter);
Button _Button = new Button()
{
Width = 41,
Height = 41
};
_Button.Style = Rstyle;
What should i put at setter.Value to obtain the image which resides in the project directory. "asd.jpg"
Button has a Content property that you can just dump a BitmapImage in it:
BitmapImage image = new BitmapImage(new Uri("your source"));
TheButton.Content = image;
the setter is not used in c# code, its a helper class to build styles in xamls.
Just use this code.
BitmapImage bmp = new BitmapImage()
bmp.BeginInit();
bmp.UriSource = new Uri("pack://application:,,,/ApplicationName;component/Resources/myImage.png");
bmp.EndInit();
Image image = new Image();
image.Source = bmp;
myButton.Content = image;
<ControlTemplate x:Key="image"> //XAML in window resources tag
<Image Source="Save.png"/>
</ControlTemplate>
btn.Template = (ControlTemplate)FindResource("image");
I think this is more flexible and reusable.

Categories

Resources