C# WPF Change grid background image [duplicate] - c#

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

Related

Crash on changing the Image source property

<Image x:Name="pageImg" Margin="-19,-1,37,19" Source="/img/1.png" Stretch="Uniform" />
The images property also set to Build Action = Resource and Copy Output Directory = Cope if newer
When the button get clicked the application crashes:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
pageImg.Source = new BitmapImage(new Uri(#"/img/2.png"));
}
But when I pass C:\Users\myuser\Desktop\2.png instead of /img/2.png it works just fine.
Why this is happening?
An image resource file (with Build Action set to Resource) should be loaded by a Resource File Pack URI:
pageImg.Source = new BitmapImage(new Uri("pack://application:,,,/img/2.png"));
It is not necessary to get it copied to the output directory.
This solved my problem :
pageImg.Source = new BitmapImage(new Uri(#"/img/2.png", UriKind.Relative));

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

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;

Change background image source on button click in wpf forms

My default background image is "lobby.jpg" and when I click the "Lights" button I want it to swap with "lobby1.jpg" and vice versa. These images are stored in "obj\Debug\Images\".
Also I'd like to implement relative(?) imagesource uris so that I can access the images on any machine (without using the whole uri, just the "obj\Debug\Images\").
Edit: So the main issue seems to be that I tried changing the window background without realising that it was getting "covered" by the grid background of the page. So what I did is I set the main window background to "lobby.jpg", I made the grid background invisible and used the code from the answer to swap between the 2 backgrounds.
You can use AppDomain basepath to exe (this is are simplest way)
var basePath= AppDomain.CurrentDomain.BaseDirectory;
var imageDirPath = $"{basePath}\\Images\\";
Example:
bool clicked = false;
private void button_Click(object sender, RoutedEventArgs e)
{
var basePath = AppDomain.CurrentDomain.BaseDirectory;
var imageDirPath = $"{basePath}\\Images\\";
if (clicked)
image.Source = new BitmapImage(new Uri(imageDirPath+ "lobby.jpg"));
else
image.Source = new BitmapImage(new Uri(imageDirPath + "lobby1.jpg"));
clicked = !clicked;
}

WPF Directory Not Found Exception and PresentationCore.pdb not loaded

I am trying to add a background image to a textbox but on textChange event, the image goes away as expected, however if I backspace or delete text in the textbox so that it is empty, I get the DirectoryNotFoundException was handeled.
And the directory:
Could not find a part of the path 'C:\myProjectFolder\bin\Debug..img\txtBackground.png'.
The XAML:
<TextBox Name="myTextBox" Width="200" TextChanged="myTextBox_TextChanged">
<TextBox.Background>
<ImageBrush ImageSource="img/txtBackground.png" />
</TextBox.Background>
C# Code:
private void myTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (myTextBox.Text == "")
{
ImageBrush textImageBrush = new ImageBrush();
textImageBrush.ImageSource =
new BitmapImage(
new Uri(#"..img/txtBackground.png", UriKind.Relative)
);
myTextBox.Background = textImageBrush;
}
else
{
myTextBox.Background = null;
}
}
Deleted the references, re-added them, build/clean solution and rebuild but nothing.
These errors only occur when I try to add the background to the textbox.
Assuming that the img folder that has image is under the Project (not under the debug folder, ideally it should not be in Debug folder) and image's BuildAction is set to Resource, you can try this:
new BitmapImage(new Uri("pack://application:,,,/img/txtBackground.png", UriKind.Absolute));
If you have img in Debug folder then you have to reach upto that
new Uri(#"bin/Debug/img/txtBackground.png", UriKind.Relative)
Your Uri should read
new Uri(#"/img/txtBackground.png", UriKind.Relative)
At least that's what the error message says.
You can also try this:
textImageBrush.ImageSource =new BitmapImage("/Projectname;Component/img/txtBackground.png");
The image's Build Action must be set to Resource.

Change canvas background - metro app

A note: I've just started approaching Windows 8 so, please be gentle. I want to set a background image to a canvas:
private void InsertImage(object sender, RoutedEventArgs e) {
var uri = new System.Uri("inkpen:///Assets/01.jpeg");
var photo_background = new ImageBrush {
ImageSource = new BitmapImage {
UriSource = uri
}
};
panelcanvas.Background = photo_background;
}
xaml:
<Canvas Name="panelcanvas" Background="White" Margin="47.5,57,327.5,153"/>
The background changes from white to black and the image doesn't load. How can I fix this?
Thank you.
The problem was the Uri
ms-appx:///Assets/01.jpeg
fixed it
http://msdn.microsoft.com/en-us/library/windows/apps/hh781215.aspx

Categories

Resources