for some reasons I have to manipulated some Images within a stackpanel in-code (not in XAML)
<StackPanel x:Name="spImageList"
Orientation="Horizontal"
HorizontalAlignment="Center"
Canvas.ZIndex="1" />
and code to manipulate images:
public void ManipulateArrows()
{
spImageList.Children.Clear();
for (int i=0;i<cout;i++)
{
Image img = new Image();
img.Name = "img" + i.ToString();
img.Source = new BitmapImage(new Uri("/Assets/Image/arrow_blue.png", UriKind.Relative));
spImageList.Children.Add(img);
}
}
Now I need to change the source of the images in that stackpanel, so how can i do it ?
public void ChangeImage(string name)
{
spImageList.Children .. = new BitmapImage(new Uri("/Assets/Image/arrow_red.png", UriKind.Relative));;
// Get the image by the name like "img1","img2"
}
Try this:
public void ChangeImages()
{
for (int j = 0; j < spImageList.Children.Count; j++)
{
Image img = (Image)Stack.FindName("img"+j.ToString());
img.Source = new BitmapImage(new Uri("/Assets/Image/arrow_red.png", UriKind.Relative));
}
}
You can use FindName method,
Image imag1 = (Image)spImageList.FindName("img1");
Image imag2 = (Image)spImageList.FindName("img2");
Related
I'm currently trying to insert random images onto a canvas using mouse click coordinates. However, I am unsure where the X & Y coordinates would be placed in the code. Any pointers would be great thanks!
private void canvas1_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Point p = Mouse.GetPosition(canvas1);
double x = p.X;
double y = p.Y;
Image myImage = new Image();
string[] imageNames = { "greenslime.png", "blueslime.png", "redslime.png", "yellowslime.png" };
var rand = new Random();
string imageName = imageNames[rand.Next(imageNames.Length)];
string imageSlime = string.Concat("", imageName);
myImage.Source = new BitmapImage(new Uri(imageSlime, UriKind.Relative));
myImage.Width = 200;
myImage.Height = 200;
canvas1.Children.Add(myImage);
}
You should use the Canvas.Top/Canvas.Left attached dependency properties.
In code behind you should use:
myImage.Source = new BitmapImage(new Uri(imageSlime, UriKind.Relative));
myImage.Width = 200;
myImage.Height = 200;
Canvas.SetLeft(myImage, x);
Canvas.SetTop(myImage, y);
canvas1.Children.Add(myImage);
Because the image is placed in the canvas, the canvas will use these properties.
In XAML it would be:
<Canvas x:Name="canvas1">
<Image Canvas.Top="10" Canvas.Left="20" Width="200" Height="200" />
</Canvas>
XAML:
<Grid x:Name="LayoutRoot" VerticalAlignment="Top">
<ScrollViewer x:Name="ScrollViewer1" Margin="0,0,0,0">
<StackPanel x:Name="myStackPanel"/>
</ScrollViewer>
</Grid>
C#:
Image[] image2 = new Image[30];
for (int n = 1; n <= 29; n++)
{
image2[n] = new Image();
BitmapImage bitmapa = downloadBitmap(n);
image2[n].Source = bitmapa;
myStackPanel.Children.Add(image2[n]);
}
I'm downloading BitmapImage from IsolatedSotrage -> downloadBitmap(n).
When I start the app I have a black image in my phone becouse photos are not visible(why?!), but when I lock the phone and unlock I have all the pictures, everything is ok.
When I remove ScrollViewer i don't have any problem.
Why this is happening? Please help me.
Dipak - I used the Grid - I hold each picture in a separate Grid:
Image[] image2 = new Image[30];
for (int n = 1; n <= 29; n++)
{
Grid myGrid = new Grid();
Image2[n] = new Image();
aktualizacja2 bitmapa = new aktualizacja2(n, path);
Image2[n].Source = bitmapa.getBitmap();
myGrid.Children.Add(Image2[n]);
myStackPanel.Children.Add(myGrid);
}
it works, but loading images takes much longer...
I am wondering how I could set an Image to "grid_Main", if I have created all my images in a loop (See Code).
Code:
private void MoleImageMaker()
{
NumofImages = TUtils.GetIniInt(Moleini, "NumPictures", "pictures", 8);
String MoleImageFunction = TUtils.GetIniFileString(Moleini, "ImagePath", "PictureFile", Root + "mole2.png");
for (int i = 0; i < NumofImages; i++)
{
Image mole = new Image();
mole.Source = new BitmapImage(new Uri(MoleImageFunction));
mole.Name = "Mole" + i;
}
}
Something like this
for (int i = 0; i < NumofImages; i++)
{
Image mole = new Image();
mole.Source = new BitmapImage(new Uri(MoleImageFunction));
mole.Name = "Mole" + i;
Grid.SetColumn(mole, i);
grid_Main.Children.Add(mole);
}
My problem is that the image that I am setting to my grid is not appearing, the only thing appearing is the black background, so I know the grid is working. I am a noob, and I am very confused. Thanks for the Help :)
Code:
public partial class MainWindow : Window
{
static String ImgNameMole = "C:/Users/MonAmi/Desktop/mole2.png";
public MainWindow()
{
InitializeComponent();
GridMain();
}
private void GridMain()
{
Grid grid_Main = new Grid();
MainWindow1.Content = grid_Main;
grid_Main.Height = 350;
grid_Main.Width = 525;
grid_Main.Background = Brushes.GreenYellow;
CreateImage();
}
private Image CreateImage()
{
Image Mole = new Image();
Mole.Width = 25;
Mole.Height = 25;
ImageSource MoleImage = new BitmapImage(new Uri(ImgNameMole));
Mole.Source = MoleImage;
return Mole;
}
}
Nowhere in your code you are calling CreateImage(), so:
var img = CreateImage();
Grid.SetRow(img, 0);
Grid.SetColumn(img, 0);
grid_Main.Children.Add(img);
assuming that you have added at least one row and one column to your grid.
I'm trying to add Image stored in ..\Resources\ebi.png in a stackpanel. Most of the times, the same image will be displayed in Stackpanel, depending on Textbox input "EtReqCount". Below is the sample code tried but getting error saying
"Specified Visual is already a child of another Visual or the root of a CompositionTarget"
Below is the code tried:
private BitmapImage bmp = new BitmapImage(new Uri("WpfApplication1;component/Resources/ebi.png", UriKind.RelativeOrAbsolute));
private void EtReqCount_TextChanged(object sender, TextChangedEventArgs e)
{
StackPanel dynamicStackPanel = new StackPanel();
dynamicStackPanel.Width = 300;
dynamicStackPanel.Height = 200;
dynamicStackPanel.Background = new SolidColorBrush(Colors.LightBlue);
dynamicStackPanel.Orientation = Orientation.Vertical;
if (EtReqCount.Text != "")
{
for (int k = 1; k <= Int32.Parse(EtReqCount.Text); k++)
{
Image img = new System.Windows.Controls.Image(); // This makes the difference.
img.Source = bmp;
dynamicStackPanel.Children.Add(img);
}
}
}
XAML Code:
It is apparent. The same image is already a child of the StackPanel, you cannot add it again and again.
Moreover, you should use a private memeber to hold the repetitively used bmp to save resource:
outside the class method, and inside class.cs:
private BitmapImage bmp=new BitmapImage(new Uri("/....../ebi.png", UriKind.RelativeOrAbsolute);
You can create new instances of the image:
for(int i=0; i<...;i++)
{
img = new System.Windows.Controls.Image(); // This makes the difference.
img.Source = bmp;
dynamicStackPanel.Children.Add(img);
}