System.Windows.Controls.Button image as content using WPF c# - c#

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;

Related

Adding image to a button in WPF C#, image not appearing

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} />

How to change panorama background programmatically?

Background of the panorama should change depending on FlowDirection of the current language.
So I thought I should do this programmatically (is it possible in the XAML?)
I added this to the OnNavigatedTo event handler of the page:
ImageBrush ib = new ImageBrush();
if (AppResources.ResourceFlowDirection == "RightToLeft")
{
ib.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/CustomBackgroundMirror.png"));
PanoramaRoot.Background = ib;
}
else
{
ib.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/CustomBackground.png"));
PanoramaRoot.Background = ib;
}
but after running, there is no image on the background, it is black.
I have a doubt that I am referring to the image badly.
How can I solve this problem?
or if the way is right, how can I get sure that the brush has an image?
Update: I have tested this too, but no difference:
ib.ImageSource = new BitmapImage(new Uri("/Assets/CustomBackground.png", UriKind.Relative));
Your updated code,
ib.ImageSource = new BitmapImage(new Uri("/Assets/CustomBackground.png", UriKind.Relative);
works properly.
You just have to make sure that the Build Action property of the image is set to Content.

Adding Image to wpf

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);

Dynamically created button in WPF

I'm try to do a bit of WPF, only really done windows forms until now and not a lot of that...
All I'm trying to do is to dynamically within code (not xaml) set a button to show an image and set the size of the button to auto size to the image.
The code below loads the image but it goes when the mouse is over the button and the button doesn't auto size to the image.
tbButtonPicture contains a local path on the PC to a bitmap e.g. C:\temp\my Artwork\test1.bmp
This what I have thus far which sits inside a loop:
Console.WriteLine(tbButtonPicture);
System.Windows.Controls.Button newBtn = new Button();
//newBtn.Content = i.ToString();
newBtn.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), tbButtonPicture)));
newBtn.Name = "Button" + i.ToString();
sp.Children.Add(newBtn);
i++;
Wrap your image in an Image control and set this as the button content and you should have your desired effect.
System.Windows.Controls.Button newBtn = new Button();
Image imageControl = new Image();
imageControl.Source = new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), tbButtonPicture));
newBtn.Content = imageControl;
newBtn.Name = "Button" + i.ToString();
sp.Children.Add(newBtn);
i++;
But I totally agree with above comments:
try to solve your issues in xaml its much more easier. Read the suggested resources, they are really helpful.

How set dynamically windows back ground image in wpf

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 ...

Categories

Resources