Set an image source from code behind - silverlight

I have a textbox where user will enter the url of the image :
suppose the user enters the following string -> C:\Users\malcolm\Desktop\img.png
imgSilverPart is a image control AND imageUrl is a string what i am getting from a textbox.
imgSilverPart.Source = new BitmapImage(new Uri(imageUrl, UriKind.RelativeOrAbsolute));
But the image is not being displayed.

This won't work. Silverlight runs in a safe Sandbox and you can't just access a file on the desktop.
So you have to call an OpenFileDialog, get the Stream to the file the user selected and set the Stream as source of the BitmapImage.
Add a Button in XAML and do the following in the Click event handler:
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDlg = new OpenFileDialog();
if (openFileDlg.ShowDialog().Value)
{
using (var stream = openFileDlg.File.OpenRead())
{
var bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
imgSilverPart.Source = bitmapImage;
}
}
}
As an alternative it's possible to use some special folders if your application runs in elevated trust mode as Out-Of-Browser app.

Maybe the kind of Uri was not determined corectly. Try tu use UriKind.Relative or UriKind.Absolute with valid relative or absolute url string.

Related

URI path in WPF window background image

I'm trying to assign a background to my WPF window.
I have a .jpg in bin\debug\StoredData\wallpaper.jpg
(I want to obtain the .jpg from there).
I came to putting this code inside the .cs file (newly created file):
InitializeComponent();
ImageBrush myBrush = new ImageBrush();
myBrush.ImageSource =
new BitmapImage(new Uri("\\StoredData\\login_wallpaper.jpg", UriKind.Absolute));
this.Background = myBrush;
But I get a "Invalid URI: The format of the URI could not be determined" message.
What should I change?
\StoredData\login_wallpaper.jpg is a relative URI. You can either change the UriKind to UriKind.Relative, or enter an absolute URI, depending on what behavior you want.

How to display the url of navigated page in web browser VB.NET WPF

I am making a web browser in VB.Net in WPF. I want to show the URL of current page in a text block. I am unable to do so. There is no URL property in WPF as it is in WinForms. There is UID property but I don't understand what it is for. There is a SOURCE property again I couldn't get what it does.
As I navigate ahead the URL of that page should be displayed.
For example, I am on google so address bar(which is the text block) should show the URL of GOOGLE when I navigate to YOUTUBE from GOOGLE, so URL should change as per my navigation.
Help PLease. I have less time.
Thanks
Handle the SourceUpdated and Navigated events and set the Text property of the TextBlock to the AbsoluteUri of the Uri:
private void wb_SourceUpdated(object sender, DataTransferEventArgs e)
{
WebBrowser wb = sender as WebBrowser;
textBlock.Text = wb.Source.AbsoluteUri;
}
private void wb_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
WebBrowser wb = sender as WebBrowser;
textBlock.Text = e.Uri.AbsoluteUri;
}

change canvas image on button click in wpf

On button click I want to change the canvas image(background image) to another image which is in a folder(images is the folder name). This is what I have tried:
private void Button_Click_2(object sender, RoutedEventArgs e)
{
drawingCanvas.Children.Clear();
ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = new BitmapImage((new Uri(#"../Images/canvas.png", UriKind.Relative)));
drawingCanvas.Background = imageBrush;
}
First I'm clearing the contents of the canvas, deleting the previous image. Now want to set the background to another image. error: URI not handled exception. Any solutions will be appreciated. Thanks.
An image resource is loaded by a Resource File Pack URI:
imageBrush.ImageSource = new BitmapImage(
new Uri("pack://application:,,,/Images/canvas.png"));
In addition, the Build Actionof the image file has to be set to Resource, as shown in this answer.

I want to open Msexcel inside my wpf application any one have any idea how to open inside wpf application

void Page1_Loaded(object sender, RoutedEventArgs e)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "C:\\Users\\ss.prajapati\\Desktop\\Demo.xlsx";
startInfo.Arguments = file;
Process.Start(startInfo);
}
I know how to open with process, Can any one give me example how to open excel inside My wpf application . I want to do like this
Have you had a look at this: https://social.msdn.microsoft.com/Forums/vstudio/en-US/acbf043d-8381-4113-b8c4-572acf3c8711/open-exe-program-inside-wpf-window-possible?forum=wpf - you could use findwindow and setwindow to set the parent of excel to be your application

using BitmapSource as Image source in WPF

I'm trying to update an Image (_browserScreenshot below) object in XAML by changing the source image every time an event determines the source needs updating. Right now I have this:
public BitmapSource GetScreen()
{
Bitmap bitmap = new Bitmap(app.Browser.ClientRectangle.Width, app.Browser.ClientRectangle.Height);
app.Browser.DrawToBitmap(bitmap, app.Browser.Bounds);
BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
bitmapSource.Freeze();
bitmap.Dispose();
bitmap = null;
return bitmapSource;
}
Then I have an event handler as shown:
app.BitmapSource.Changed += new EventHandler(BitmapSource_Changed);
void BitmapSource_Changed(object sender, EventArgs e)
{
Window1._browserScreenshot.Source = app.GetScreen();
}
Now whenever this event fires a new screenshot is taken and the source of the Image (called _browserScreenshot here) control should be updated. I keep getting an error about changing the IsFrozen propery, but I can't figure out how to change this correctly and have this work the way I want it to. Thanks in advance everyone.
In all likelyhood you want to Freeze the object. The problem you are having is that you want to create a completely new BitmapSource every time and let the garbage collector dispose of the old image.
The following line turned out to be my problem:
bitmapSource.Freeze();

Resources