How to set image from url to button background? [duplicate] - c#

This question already has an answer here:
Attach Image/ImageBrush from code behind
(1 answer)
Closed 6 years ago.
I would like to set the background image for a button to an image I have from a URL; but the following code does not work:
var button = new Button();
Image image = new Image();
image.Source = new BitmapImage(new Uri("https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-folder-128.png", UriKind.Absolute));
button = image;
Specifically "button = image" doesn't work, because Button isn't Image type.
How should I set an image to be the background image of a button?

You can accomplish this by using WebClient to first download the image locally before displaying it in the Button control
using (WebClient c = new WebClient())
{
c.DownloadFile("https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-folder-128.png", "D:/image.png");
}
button.Image = Image.FromFile("D:/image.png");
Where D:/image.png is the location you would like to save your image to

Ok, I've got a solution.
var button = new Button();
var image = new ImageBrush();
image.ImageSource = new BitmapImage(new Uri(url, UriKind.Absolute));
button.Background = image;

Related

How to set button backgroundimage in c#?

I'm new in this field. I'm working in windows application, I create panel and adding the buttons inside the panel. i want to retrieve the image from database and set to the background image in button.
This is my code,
FileName = objDR["Photopath"].ToString();
byte[] data = Encoding.UTF8.GetBytes(FileName);
MemoryStream ms = new MemoryStream(data);
image = new System.Drawing.Bitmap(ms);
Buttons[i].BackgroundImage = image;
initially put BackgroundImage property ON
OR
You can Code :
button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.Image));

C# WPF Change grid background image [duplicate]

This question already has an answer here:
how can I set a background image in code?
(1 answer)
Closed 7 years ago.
How can I change grid background image of clicked button? I tried this code but it didn't work. I need help.
Code:
WpfApplication5.Properties.Settings.Default.GridImage = "Pictures\file.jpg";
Background can be set by using ImageBrush:
var imgBrush = new ImageBrush();
imgBrush.ImageSource = new BitmapImage(new Uri(#"Pictures\file.jpg", UriKind.Relative));
myGrid.Background = imgBrush;
When using relative path, you need to have Pictures folder with file.jpg in bin\Debug folder.
Like this.You should set Background of Button
<Grid>
<Button Name="button1" Click="button1_Click">
</Button>
</Grid>
private void button1_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("image path", UriKind.Relative);
BitmapImage img = new BitmapImage(uri);
button2.Background = new ImageBrush(img );
}

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.

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