After several attempts to change the background of a button, and getting errors of casting and things like that I finnaly got to this point:
Uri dir = new Uri("red_flag.png",UriKind.RelativeOrAbsolute);
ImageSource source = new System.Windows.Media.Imaging.BitmapImage(dir);
ImageBrush bru = new ImageBrush();
bru.ImageSource=source;
bru.Opacity = 100;
This code does not generate errors, but I can't see the changes when I call:
button1.background = bru;
It just makes anything! :(
found the answer myelf after reading Mick's answer, I share with you what I did:
Uri dir = new Uri("red_flag.png", UriKind.Relative);
ImageSource source = new System.Windows.Media.Imaging.BitmapImage(dir);
Image image = new Image();
image.Source = source;
StackPanel stack = new StackPanel();
stack.Children.Add(image);
myButton.Content = stack;
Thanks for your help
Update 1:
For best results set the padding property of your button to 0 (in each of the cases) so the image can resize automatically to fill all the button, please note this could hide your actual content, in my case this was what I wanted.
If this code is part of the click event handler for the same button you will have this problem.
Peter Torr explains why here and offers a solution.
Why can't I change the Background of my Button on a Click event?
Related
I was just wondering if there would be a way to change the Image background of a button using cs and then keep it the same when disabled as when it is enabled and when switching between the two states, Thanks.
bt1.Background = "/Assets/Image1.png";
bt1.IsEnabled = false;
// at this point the background of button becomes invisible
Here is the 3 step approach for adding an image to button in Windows UWP:
// Step 1: Create an image object
Image img = new Image();
img.Source = new BitmapImage(new Uri(#"Assets\Image1.png"));
// Step 2: Add the image to a stack panel
StackPanel sp = new StackPanel();
sp.Children.Add(buttonImage);
// Add other items to stack panel
// Step 3: Set the stack panel as the button content
Button btn = new Button();
btn.Content = sp;
I've never really worked with styling in C # and don't know if you use CSS for the styling. But if so you could probably use something like this in your css file
button {
background-image: url("paper.gif");
}
button:hover {
background-image: url("paper.gif");
}
button:disabled,
button[disabled]{
background-image: url("paper.gif");
}
I have to create a small pizza delivery program in C# using Visual Studio 2019 Community Edition :
It has 3 Radio Buttons ( for 3 different pizza's), A Textblock for text ( to show the ingredients), and an image to show the type of pizza.
So I want that when I click on one of those 3 Radio Buttons, a certain text & image appears.
The Radio Buttons and TextBlock is ok, but the image I'm lost at how to properly configure it using the code behind.
Can anyone check the code, and maybe enlighten me?
https://ideone.com/e.js/MjEBnd
private void radiobtnHawai_Checked(object sender, RoutedEventArgs e)
{
string Hawai = "Ananas, Kip, Kaas";
lbl1.Text = Hawai;
Image Hawai.jpg = new Image();
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri("smiley_stackpanel.PNG", UriKind.Relative);
bi3.EndInit();
Hawai.jpg.Stretch = Stretch.Fill;
Hawai.jpg.Source = bi3;
}
Here is an Extra Image : https://i.stack.imgur.com/iiHRn.png
Thanks in Advance,
If more information is needed, please don't hesitate to ask.
Suppose you have a image control defined like this
<Image Name="ImageViewer1" Height="400" Width="400" />
On your radio checked command , you have to assign the source of "ImageViewer1".
For example, in your case it will look like this
private void radiobtnHawai_Checked(object sender, RoutedEventArgs e)
{
string Hawai = "Ananas, Kip, Kaas";
lbl1.Text = Hawai;
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri("smiley_stackpanel.PNG", UriKind.Relative);
bi3.EndInit();
ImageViewer1.Stretch = Stretch.Fill;
ImageViewer1.Source = bi3;
}
Also make sure that, smiley_stackpanel.PNG, exists in your Environment.CurrentDirectory.
By specifying relative path it will look images from the path where your exe resides
I am using a button control. I want get the android:background (image source) from code behind. Based on that I will continue with my code.
How can I get the image source from code behind?
<Button
android:id="#+id/btnPlayPause"
android:layout_width="34dp"
android:layout_height="34dp"
android:layout_marginLeft="15dp"
android:visibility="gone"
android:background="#drawable/play"/>
Are you looking for something like this?
Button btn = new Button(Context);
Drawable btnDrawable = btn.Background;
now you can compare the drawable with others (for example from Android.Resource)
You can try this code
var beachImage = new Image { Aspect = Aspect.AspectFit };
btnPlayPause.Image = ImageSource.FromFile("imageName.png");
I am trying add a background image using the image property in button. The issue I'm facing is that i can't set StreamImageSource as button background. I encountered the error given below if I try to do so.
The Code I use to set Image:
ImageSource iconsource =ImageSource.FromStream(() => new MemoryStream(ImgASBytes));
Button Icon = new Button ();
Icon.Image = iconsource ;
The Error I encounter:
Error CS0266: Cannot implicitly convert type 'Xamarin.Forms.ImageSource' to 'Xamarin.Forms.FileImageSource'. An explicit conversion exists (are you missing a cast?)
ImageSource.FromStream () returns a StreamImageSource (see docs). Button.Image accepts only FileImageSource (see docs).
It means that what you're trying to achieve won't work, no matter how hard you try to cast one into the other.
Button.Image will accept images stored as resources in your platform projects, and loaded either with:
Icon.Image = ImageSource.FromFile ("foobar.png");
or
Icon.Image = "foobar.png";
The accepted answer is true that you can't cast StreamImageSource to FileImageSource, I think that the real question is about how to share images in a PCL and use them on a button, just like one would when creating an Image forms control.
The answer is to have a Grid which contains both a Button and an Image object, where the Image overlaps the Button.
For example, the C# code might look like this:
ImageSource imageSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
Button iconButton = new Button ();
iconButton.VerticalOptions = LayoutOptions.FillAndExpand;
iconButton.HorizontalOptions = LayoutOptions.FillAndExpand;
var image = new Image();
image.Source = imageSource;
// So it doesn't eat up clicks that should go to the button:
image.InputTransparent = true;
// Give it a margin so it doesn't extend to the edge of the grid
image.Margin = new Thickness(10);
var grid = new Grid();
// If we don't set a width request, it may stretch horizontally in a stack
grid.WidthRequest = 48;
// Add the button first, so it is under the image...
grid.Children.Add(iconButton);
// ...then add the image
grid.Children.Add(image);
You may have to play with the sizes and thickness values but this should get you a clickable button with an icon.
As of Xamarin.Forms 3.4.0 you can now use ImageButton. You can use embedded images by using an extension method explained in this MS document
Careful with upper- and lowercase in filenames.
I was wondering, why my button-images were shown properly on the simulator, but not on my iPhone.
On the device the filename must match exactly, the simulator doesn't care about upper- and lowercase in filenames.
I use this and it works
var imageA = new Image();
imageA.Source=(FileImageSource)ImageSource.FromFile(allergeneLocation)};
or
var imageA = new Image()
{
BackgroundColor = Color.Teal,
Source = (FileImageSource)ImageSource.FromFile(allergeneLocation)},
};
Here is what I tried:
Button refreshBut = new Button
{
Image = (FileImageSource)
(ImageSource.FromFile("refreshBut.png"))
};
While it compiles I then get an unhandled null reference exception with the description: Object reference not set to an instance of an object. I am not sure if this will help anyone else try to solve this but I am at the same wall.
Image myImage3 = new Image();
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri(#"C:\Users\User\Desktop\Awesomeness\PicProgram\Program\bin\Debug\flower.png", UriKind.Relative);
bi3.EndInit();
myImage3.Stretch = Stretch.Fill;
myImage3.Source = bi3;
I was wondering how I get myImage3 added to the window.
I've tried this.content.add.myImage3 or this.add(myImage3) and other variations none seem to work.
Any suggestions?
Thanks
Seems like a basic wpf usage question, although doing something that is simple in xaml can get a bit complex in code behind.....
You have to add your image to some panel or content control in your window, not the window itself. So if there is a grid in your window, give the grid a name like m_grid, then do m_grid.children.add(myImage3).