Error in changing the background dynamically - c#

First i've created a button(named as BackgroundChooser) that is used to change the background image dynamically(they can select their own image as background). But i have already used a default background image(image1.jpg) for my windows phone 7 application. When i click on the choose background button, it directs to our saved pictures. After that i have selected am image as my own background image. But the default background image is still doesn't changed.
Then when i have changed the default background to black, then i can set my own background image(it's working perfectly). Need help!!! Thanks in advance for your hard work!!!
Below is the code i have used-:
private void BackgroundChooser_Click(object sender, MouseEventArgs e)
{
var PhotoChooser = new PhotoChooserTask();
PhotoChooser.Completed += new EventHandler<PhotoResult(PhotoChooser_Completed);
PhotoChooser.Show();
}
void PhotoChooser_Completed(object sender, PhotoResult e)
{
{
if (e.TaskResult == TaskResult.OK)
{
System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
var app = Application.Current as App;
if (app == null)
return;
var imageBrush = new ImageBrush { ImageSource = bmp, Opacity = 0.5d };
app.RootFrame.Background = imageBrush;
//app.RootFrame.Background = new SolidColorBrush(Colors.Black); //we can apply just color too like this
}
}
}

instead of app.RootFrame.Background, try setting the Background property on some display object on your page such as myPanoramaControl.Background = imageBrush; or LayoutRoot.Background = imageBrush; (where LayoutRoot is the name of the default grid control for a new WP Page)

Related

How to get the name of the image when clicked in windows phone 8.1?

I want to know the image name whenever image clicked.
i used this code for adding image to button background:
ImageBrush brush1 = new ImageBrush();
brush1.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/emptyseat.jpg"));
btn.Background = brush1;
now i want to know the name of the image whenever image clicked. please any one help me out.
You can save the name in the buttons Tag property
ImageBrush brush1 = new ImageBrush();
brush1.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/emptyseat.jpg"));
btn.Tag = "emptyseat.jpg";
btn.Background = brush1;
public void OnClick(object sender, RoutedEventArgs e)
{
var btn = (Button)sender;
string name = btn.Tag;
}

Windows Phone 7.1 live tile background images not setting

I am trying to change the background images (front and back) of the live tile for a windows phone 7.1 app however the background images are never set. I've added the images to the project and have made sure to specify their names properly in the Uri() constructor. I can't seem to be able to detect the problem. Here's the code.
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
String result = "Default";
String company = "";
String image = "";
//Method That Executes After Every DownloadStringAsync() Call by WebClient
public void wb_DownloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e)
{
result = e.Result;
int newCount = 1;
// Application Tile is always the first Tile, even if it is not pinned to Start.
ShellTile TileToFind = ShellTile.ActiveTiles.First();
// Application should always be found
if (TileToFind != null)
{
// Set the properties to update for the Application Tile.
// Empty strings for the text values and URIs will result in the property being cleared.
StandardTileData NewTileData = new StandardTileData
{
Title = "Stocks App",
BackgroundImage = new Uri(image, UriKind.Relative),
Count = newCount,
BackTitle = company,
BackBackgroundImage = new Uri(image, UriKind.Relative), //**The problem is here**
BackContent = result
};
// Update the Application Tile
TileToFind.Update(NewTileData);
}
}
//Method for Radio Button When Google is Selected
private void radioButton1_Checked(object sender, RoutedEventArgs e)
{
company = "Google Stock";
image = "google_icon.png";
WebClient wb = new WebClient();
wb.DownloadStringAsync(new Uri("http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=a"));
wb.DownloadStringCompleted += wb_DownloadStringCompleted;
}
//Method for Radio Button When Yahoo is Selected
private void yahooRadioBtn_Checked(object sender, RoutedEventArgs e)
{
company = "Yahoo Stock";
image = "yahoo_icon.png";
WebClient wb = new WebClient();
wb.DownloadStringAsync(new Uri("http://finance.yahoo.com/d/quotes.csv?s=YHOO&f=a"));
wb.DownloadStringCompleted += wb_DownloadStringCompleted;
}
//Method for Radio Button When Apple is Selected
private void appleRadioBtn_Checked(object sender, RoutedEventArgs e)
{
company = "Apple Stock";
image = "apple_icon.png";
WebClient wb = new WebClient();
wb.DownloadStringAsync(new Uri("http://finance.yahoo.com/d/quotes.csv?s=AAPL&f=a"));
wb.DownloadStringCompleted += wb_DownloadStringCompleted;
}
}
Verify that the path of the image and the Build Action is as requested by the documentation
You can find out more at
http://msdn.microsoft.com/en-US/library/windowsphone/develop/microsoft.phone.shell.standardtiledata.backbackgroundimage(v=vs.105).aspx
and
http://msdn.microsoft.com/en-US/library/windowsphone/develop/ff402541(v=vs.105).aspx
Check your files Build Action property is set to Content before any other check

display an image from file in wpf isn't working?

I have a button b3 and an image named pictureBox1 . Im using WPF, however I'm using the winforms openFileDialog instead of the one that comes with WPF :
below is the code that I put inside the click event of my button :
private void b3_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.OpenFileDialog openDialogIcon = new System.Windows.Forms.OpenFileDialog();
if (openDialogIcon.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
Image i = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(openDialogIcon.FileName, UriKind.Absolute);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
i.Source = src;
i.Stretch = Stretch.Uniform;
//int q = src.PixelHeight; // Image loads here
}
}
When I click the button and select an icon. The icon doesn't appear in the pictureBox1.
Can someone please explain why the code above doesn't show the icon inside the pictureBox?
You need to assign your image to the pictureBox, else you wont see it on your screen and you only made the image object in memory.
private void b3_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.OpenFileDialog openDialogIcon = new System.Windows.Forms.OpenFileDialog();
if (openDialogIcon.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(openDialogIcon.FileName, UriKind.Absolute);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
pictureBox1.Source = src;
}
}
Try to drag and drop a Image control in your window
...
//imageStretch <- the name of Image control
i.Stretch = Stretch.Uniform;
//int q = src.PixelHeight; // Image loads here
imageStretch.Source = src;
...

how to display image in a grid using C# for WP8?

i need to insert an image in a WP8. i have a stack of images. once i click the image, it has to be set as background for a grid. so i created an empty "grid1" and button. wrote the below code in the button click event, but the image doesnot get displayed !
private void bg6_Click(object sender, RoutedEventArgs e)
{
System.Windows.Media.ImageBrush myBrush = new System.Windows.Media.ImageBrush();
Image image = new Image();
image.Source = new System.Windows.Media.Imaging.BitmapImage(
new Uri("\\PhoneApp2\\PhoneApp2\\Assets\\bg\\bg5.jpg"));
myBrush.ImageSource = image.Source;
// Grid grid1 = new Grid();
grid1.Background = myBrush;
}
It is hard to know if your image file is in the correct place and set to the right build type. I'd suggest adding an event handler to the Image failed event.
private void bg6_Click(object sender, RoutedEventArgs e)
{
System.Windows.Media.ImageBrush myBrush = new System.Windows.Media.ImageBrush();
Image image = new Image();
image.ImageFailed += (s, e) => MessageBox.Show("Failed to load: " + e.ErrorException.Message);
image.Source = new System.Windows.Media.Imaging.BitmapImage(
new Uri("\\PhoneApp2\\PhoneApp2\\Assets\\bg\\bg5.jpg"));
myBrush.ImageSource = image.Source;
// Grid grid1 = new Grid();
grid1.Background = myBrush;
}
First, you don't need to use Image to fill the background from URI.
private void bg6_Click(object sender, RoutedEventArgs e)
{
System.Windows.Media.ImageBrush myBrush = new System.Windows.Media.ImageBrush(new Uri("\\PhoneApp2\\PhoneApp2\\Assets\\bg\\bg5.jpg"));
// Grid grid1 = new Grid();
grid1.Background = myBrush;
}
Second, it is a WAY better to design it in XAML and manipulate it visibility and source from code by creating the helper class with visibility and source property. Don't forget to implement INotifyPropertyChanged interface into that class.
<Grid x:Name="myGrid" DataContext="{Binding}" Visibility="{Binding Path=VisibleProperty}">
<Grid.Background>
<ImageBrush x:Name="myBrush" ImageSource="{Binding Path=SourceProperty}"></ImageBrush>
</Grid.Background>
And in code:
private void bg6_Click(object sender, RoutedEventArgs e)
{
myGrid.DataContext=new myImagePresenterClass(new Uri("\\PhoneApp2\\PhoneApp2\\Assets\\bg\\bg5.jpg"), Visibility.Visible)
}
public class myImagePresenterClass:INotifyPropertyChanged
{
private URI sourceProperty
Public URI SourceProperty
{
get
{
return sourceProperty;
}
set
{
sourceProperty=value;
if(PropertyChanged!=null){PropertyChanged(this, new PropertyChangedEventArgs("SourceProperty"));}
}
}
//Don't forget to implement the Visible property the same way as SourceProperty and the class constructor.
}
i found the mistake... i'm sorry guys. i didn't follow the syntax correctly. i missed the '#' in the Uri method. the correct way to represent this is
private void bg1_Click(object sender, RoutedEventArgs e)
{
System.Windows.Media.ImageBrush myBrush = new System.Windows.Media.ImageBrush();
Image image = new Image();
image.ImageFailed += (s, i) => MessageBox.Show("Failed to load: " + i.ErrorException.Message);
image.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(#"/Assets/bg/bg1.jpg/", UriKind.RelativeOrAbsolute));
myBrush.ImageSource = image.Source;
grid1.Background = myBrush;
}

Implement font menu item in notepad using c#

while i am trying to implement font menu item in notepad ,
im not getting text changed , is there any mistake in my code
this is my code
private void fontMenuItem_Click(object sender, EventArgs e)
{
FontDialog objFontForm = new FontDialog();
objFontForm.ShowDialog();
}
Here's a really simple example taken from DotNetPearls
//Create FontDialog instance
FontDialog fontDialog1 = new FontDialog();
// Show the dialog.
DialogResult result = fontDialog1.ShowDialog();
// See if OK was pressed.
if (result == DialogResult.OK)
{
// Get Font.
Font font = fontDialog1.Font;
// Set TextBox properties.
this.textBox1.Text = string.Format("Font is: {0}", font.Name);
this.textBox1.Font = font;
}

Categories