I want to add an Image dynamically to the wpf application.
What I have tried :
Image img = new Image();
img.Height = 100;
img.Width = 100;
img.Visibility = Visibility.Visible;
Thickness th = new Thickness(100, 100, 0, 0);
img.Margin = th;
string strUri2 = String.Format(#"pack://application:,,,/MyFirstWPF;component/Images/tt.jpg");
img.Source = new BitmapImage(new Uri(strUri2));
I know, the image won't display unless I add it to the Content.
this.Content=img;
but with this the existing controls(shapes) on the app are lost.
Now, my question is how to add the image to the content without losing existing controls from the app.
When you are going to load and show an image dynamically, you still need to think about the layout of your application. I would suggest to add an (initially empty) Image control in XAML (for example in a Grid), and later set the Source property of this control in code.
<Grid>
... other controls ...
<Image Name="img" Grid.Row="..." Grid.Column="..."
Width="100" Height="100" Margin="100,100,0,0"/>
</Grid>
Set Source in code:
var uri = new Uri("pack://application:,,,/MyFirstWPF;component/Images/tt.jpg");
img.Source = new BitmapImage(uri);
by default the window content is a grid so try
(this.Content as Grid).Children.Add(img);
Related
Xaml
<ScrollViewer x:Name="_ScrollViwer" />
Code behind
Image image = new Image();
_ScrollViwer.Children.Add(image);
Error
ScrollViewer' does not contain a definition for 'Children' and no accessible extension method Children'
Injecting UI control into ScrollViewer programmatically
ScrollViewer is not Panel, if you want to add Image into ScrollViewer, please pass it to ScrollViewer's content. And if you want to add more controls into ScrollViewer, you need use panel to package elements.
For example
Image image = new Image();
Image image1 = new Image();
var stackpanel = new StackPanel()
{
Children =
{
image,
image1
}
};
_ScrollViwer.Content = stackpanel;
Here is the code and the image is not showing. If I use the text content it shows up but the image will not. What am I missing?
//Begin btnLine Code.
btnLine = new System.Windows.Controls.Button();
// Create the image element.
Image simpleImage = new Image();
simpleImage.Width = 200;
simpleImage.Margin = new Thickness(5);
// Create source.
BitmapImage bi = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block.
bi.BeginInit();
bi.UriSource = new Uri(InstallDir+#"\ToolbarImages\line1.jpg",UriKind.RelativeOrAbsolute);
bi.EndInit();
// Set the image source.
simpleImage.Source = bi;
btnLine.Content= bi;//image button Does not work
//btnLine.Content= simpleImage;//image button Does not work
//btnLine.Content= "o-o";//text button Does work
btnLine.Click += btnLine_Click;
chartWindow.MainMenu.Add(btnLine);
Normally it is much easier to do these things in XAML. it is fairly straight forward to set an image to a a local resource. This is a simple version how.
<Grid>
<Button>
<Image Source="ToolbarImages\image1.jpg"/>
</Button>
</Grid>
If you are looking for a dynamic way to do this, you should use a binding on the source image that way you are leveraging the power of wpf.
change
btnLine.Content= bi;
to
btnLine.Content= simpleImage;
I want to create a button in my windows 8 desktop app, which will have an image and a text block. I have to do this using C# coding.
My code is as follows,
Button btn = new Button();
StackPanel btnContentPanel = new StackPanel();
btnContentPanel.Orientation = Orientation.Horizontal;
Image img = new Image();
img.Source = new BitmapImage(newUri(#"C:\Users\Desktop\Images\download.jpg"));
img.Stretch = Stretch.Uniform;
btnContentPanel.Children.Add(img);
TextBlock txBlock = new TextBlock();
txBlock.Text = "My Button";
btnContentPanel.Children.Add(txBlock);
btn.Content = btnContentPanel;
This is not giving any error but the image is not getting displayed. If I add another text block in place of the image, then its appearing, but not the image.
Am I missing anything ? Please help, thank you.
Try building your button like this:
Button btn= new Button
{
Width = 30,
Height = 30,
Content = new Image
{
Source = new BitmapImage(#"C:\Users\Desktop\Images\download.jpg"))
}
};
In the case of a 'missing' image there are several things to consider:
When Xaml can't locate a resource it might ignore it (when it won't throw a XamlParseException)
The Resource must be properly added and defined:
make sure it exists in your project where expected.
Make sure it is built with your project as a Resource.
(Right click -> Properties -> BuildAction='Resource')
Another thing to try in similar cases, which is also useful for reusing of the image (or any other resource):
Define your Image as a Resource in your Xaml:
<UserCondrol.Resources>
<Image x:Key="MyImage" Source.../>
</UserControl.Resources>
And later use it in your desired control/controls:
<Button Content={StaticResource MyImage} />
I have 20 images, they are ball1, ball2, ..., ball20.
Supposedly, I inserted the images using
Image x:Name="ball1" Source="/Images/ball1.png" Canvas.Left="150" Canvas.Top="200" in .xaml.
Currently, I tried to insert it in this way
Uri uri = new Uri("/Images/ball1.png", UriKind.Relative);
ImageSource img = new System.Windows.Media.Imaging.BitmapImage(uri);
image.SetValue(Image.SourceProperty, img);
cv.Children.Add(image);
However, I could not use it this way because it does not specify the position that I want to insert it.
I want to avoid doing it through .xaml, how can I do it using array in .cs?
You can declare the Image objects in XAML and then update the Source properties from your code behind or view model if you prefer. For this method, you will need one property per image in your view model or code behind:
public string Image1SourcePath
{
get { return image1SourcePath; }
set { image1SourcePath = value; NotifyPropertyChanged("Image1SourcePath"); }
}
...
public string Image20SourcePath
{
get { return image20SourcePath; }
set { image20SourcePath = value; NotifyPropertyChanged("Image20SourcePath"); }
}
Ensure you implement some form of INotifyPropertyChanged interface
<Image Source="{Binding Image1SourcePath}" Canvas.Left="150" Canvas.Top="200" />
...
<Image Source="{Binding Image20SourcePath}" Canvas.Left="1500" Canvas.Top="200" />
Then in your view model or code behind:
Image1SourcePath = "/YourApplicationName;component/Images/ball1.png";
...
Image20SourcePath = "/YourApplicationName;component/Images/ball20.png";
It's a lot of code, but it allows you to update the Image.Source properties from your code behind and set the positions in XAML.
You can do this by calling the appropriate static methods on the Canvas class that set the attached properties (Canvas.Left & Canvas.Top).
Uri uri = new Uri("/Images/ball1.png", UriKind.Relative);
ImageSource img = new System.Windows.Media.Imaging.BitmapImage(uri);
image.SetValue(Image.SourceProperty, img);
cv.Children.Add(image);
// Position the image on the canvas
Canvas.SetLeft(150);
Canvas.SetTop(200);
If you have a list of images to display, you could do something like:
List<Uri> imageUris = new List<Uri>()
{
new Uri(#"C:\Users\Grant\Pictures\Heron_zoomed.png"),
new Uri(#"C:\Users\Grant\Pictures\bridge.jpg")
};
int left = 20;
int top = 10;
foreach (var uri in imageUris)
{
Image image = new Image { Source = new BitmapImage(uri) };
Canvas.SetLeft(image, left);
Canvas.SetTop(image, top);
MainCanvas.Children.Add(image);
left += 400;
}
The code above assumes you have something like the following in your window xaml and that the file names in the imageUris list exist.
<Grid>
<Canvas x:Name="MainCanvas">
</Canvas>
</Grid>
I have no idea what you are trying to do with these images. If you just want to display them in a grid you could use one of the WPF collection controls to do this without any code.
One way of doing this is Displaying images in grid with WPF
I suspect there are better alternatives but this would be a start.
I have small doubt in wpf,i kept image on button.so when we click on button that image should be set as back ground?what should i do? any one have idea>
please let me know.
Thanks in Adavance
Developer
If you need to put an image as a background of any control (button or grid, didn't understancd you), just set, in code, the Background property to an ImageBrush with an ImageSource of your image.
This code is for button, edit it ...
Button button = new Button();
button.Margin = new Thickness(220, -880, 0, 0);
button.Width = w;
button.Height = h;
/////////////////////////////////////////////////////////
ImageBrush berriesBrush = new ImageBrush();
berriesBrush.ImageSource = new BitmapImage(new Uri(#"Image/country.PNG", UriKind.Relative));
button.Background = berriesBrush;
button.BorderThickness = new Thickness(0);
This might you ...