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).
Related
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 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.
Recently I've been strugling with setting the Setting the source of WPF Image control inside code.
I've tried something like:
Image.Source = new BitmapImage(new Uri(#"/hrc.Hands;component/Images/BlueFolder.png", UriKind.RelativeOrAbsolute));
But it doesn't seem to work and thorws Object reference not set to an instance of an object exception. Here's the screenshot of the path to my image inside hierachy:
You can use..
var imageSource = new Uri(#"pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Images/BlueFolder.png", UriKind.Absolute);
Image.Source = imageSource;
Also, make sure that BlueFolder.png is set as a Resource. Right click the image, and select Properties to do this.
In XAML, I learned to specify image source with below code:
<Image Source="/WaivePadBankAnim;component/Images/Consumer/search_button_full.png" />
How would I do it in C# .cs?
Try this
Image image = new Image();
image.UriSource = new Uri("/WaivePadBankAnim;component/Images/Consumer/search_button_full.png")
Try this
Adding WPF Controls Progrrammatically
M working on Windows phone 8 and i used this.
OrderImage1.Source = new BitmapImage(new Uri("///Assets/images/order_bread.png"));
Image img = new Image();
img.Source = new Uri("<your path here>");
I hope this helps.
please check this
just use a class "ImageSourceConverter"
img1.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("/Assets/check.png");
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?