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 ...
Related
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;
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.
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);
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.
I want to create an image as a button in code in C# WPF (not a button with BG image but an actual image). I read on this site to use a PictureBox for the image, and I've found that the WPF equivalent is Image. The problem is, that while i've found PictureBox has a .Click that you can set, Image does not. The two things I want to do are:
Create an array of buttons that are images and can be clicked.
Have an image for the unclicked and clicked states of the button.
Is there anything right in front of me I'm missing?
Here is my loop creating the buttons:
sideBarButtons = new Button[infoLoader.categoriesLength];
sideButtons = new Image[infoLoader.categoriesLength];
ImageBrush[] myBg = new ImageBrush[infoLoader.categoriesLength];
for (int i = 0; i < sideBarButtons.Length; i++)
{
myBg[i] = new ImageBrush();
myBg[i].ImageSource = new BitmapImage(graphicLoader.buttonUnselected[(i % myBg.Length)]);
/*sideBarButtons[i] = new Button();
sideBarButtons[i].Content = infoLoader.categories[i].name;
sideBarButtons[i].Background = myBg[i];
//sideBarButtons[i].BorderThickness = ;
sideBarButtons[i].Width = 155;
sideBarButtons[i].Height = 46;
Canvas.SetLeft(sideBarButtons[i], 30);
Canvas.SetTop(sideBarButtons[i], 10 + (46 * i));
sideBarButtons[i].Click += new RoutedEventHandler(this.SideButton_Click);
leftSideBar.Children.Add(sideBarButtons[i]);*/
BitmapImage myBmp = new BitmapImage();
myBmp.BeginInit();
myBmp.UriSource = myBg[i];
myBmp.EndInit();
sideButtons[i] = new Image();
sideButtons[i].Source = myBmp;
sideButtons[i].Width = 155;
sideButtons[i].Height = 46;
Canvas.SetLeft(sideButtons[i], 30);
Canvas.SetTop(sideButtons[i], 10 + (46 * i));
sideButtons[i].Click += new RoutedEventHandler(this.SideButton_Click);
leftSideBar.Children.Add(sideButtons[i]);
}
The first commented out area is when I was creating buttons with buttons and not images, while the second is images and it doesn't work. Thanks in advance.
Two options here:
1.) Instead of using the Click event, which doesn't exist on Image, use MouseDown, which does.
2.) Instead of using Images and repurposing them, use Buttons with a custom style on it. Then you can handle the button's click.
Personally, I'd use the latter, but really either works.