Open Image from IsolatedStoreage? - c#

I want to open an Image from the IsolatedStorage?
The image was downloaded before and correctly written (I checked that with the Isolated Storage Explorer).
When I try to open the Image with BitmapImage(uriInIsolatedStorage) and set that as source for a Silverlight Image Control it crashs when I listen to the image failed event.
The exception said "AG_E_NETWORK_ERROR"
Does anyone has an idea?
Uri imageSource = new Uri("/cover.jpg", UriKind.Relative);
BitmapImage bi = new BitmapImage(imageSource);
bi.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(MainPage_ImageFailed);
bi.ImageOpened +=new EventHandler<RoutedEventArgs>(bi_ImageOpened);
imageCtrl.Source = bi;

Unfortunately you cannot load an image directly from isolated storage via URI. You have to open the file and do some more steps as described here or (a bit easier) here.
It boils down to:
creating an IsolatedStorageFileStream for your image
creating a BitmapImage from file data
setting BitmapImage as Image source
There is also an isostore: URI scheme but it doesn't work.

Related

Why cannot load actor image from TheMovieDatabase server even the links works on browser normaly?

This is really weird. This simple code can load any picture from net but not from TheMovieDatabase.
(ActorImage is the normal Image created in xaml)
Code is next:
string ImageUrl = "https://image.tmdb.org/t/p/w500/dRLSoufWtc16F5fliK4ECIVs56p.jpg";
BitmapImage ActorBitmapImage = new BitmapImage(new Uri(ImageUrl, UriKind.RelativeOrAbsolute));
ActorImage.Source = ActorBitmapImage;
Most weird thing is that if I open the browser the link works as it should to work.
Any idea what can cause the problem?

Overwriting and Image Open and Currently Binded to In WPF Program

I am binding to 5 different images in my program and wish to leave the capability of the user to replace or update the photos under the same name. So when updating these pictures, the binding will be notified and change while the program is running.
The program is a Digital VMB (visual management board) at my workplace, so it needs to remain running and have these photos be updated on the server without a hitch. Currently I am binding to a property in my C# which is a string containing the image location.
In c#:
public string OpenFilePathProp { get; set; }
In XAML:
<Image x:Name="OpenWOImage" Source="{Binding OpenFilePathProp}" Stretch="Fill" Margin="25,0,25,0"/>
When the user goes to copy and replace the image with the newer one, they can't as it's "currently open in another process". Which I suppose is the data binding in my WPF.
Can this be overcome by opening the images into a filestream and then binding to the stream? If so, I'm completely unsure on how to bind to a filestream; I'm quite new to WPF AND C#.
Thanks for the help. I HAVE tried to look for a solution to this, but I think I'm just getting confused and I don't think it will resolve my problem since it seems the binding is what's "keeping the image open" and I'm not sure how to bind to an Image object AND close that object to allow for overwriting
EDIT: Thought I should mention that I've managed to copy the images in question to the AppData folder for my VMB program,
like,
string AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\";
System.IO.File.Copy(Properties.Settings.Default.OpenWOFilePath, System.IO.Path.Combine(AppData, "ROAMaintenanceVMB\\OpenWOFilePath_Copy.jpeg"), true);
this way I can "check" every so often to see that someone has overwritten the photos on the server, and THEORETICALLY copy the "new" photos to the AppData folder, overwriting the previous versions. THIS is where the issue of the images already being open in another program arises.
Following Peter Duniho suggestion about using WriteableBitmap worked in my instance.
string imagePath = "";
Uri imageUri = new Uri(imagePath);
BitmapImage bitmapImage = new BitmapImage(imageUri);
ImageProperty = new WriteableBitmap(bitmapImage);
<Image Source="{Binding ImageProperty}"/>
What's going on here is:
BitmapImage is created from the given path
WriteableBitmap is created based on the BitmapImage (a "deep copy"?)
The important thing here is the original image is not locked, as the binding is tied to the copy of the image of WriteableBitmap - not to the image itself, so the image can be deleted / replaced freely.

WPF C# Bitmap resource as Imagesource for ImageBrush [duplicate]

I want to change the background of a button manually in my WPF app.
I have an image imported into my resources and I want to do:
MyButton.Background = MyProject.Properties.Resources.myImage;
But I get the error:
cannot implicitly convert system.drawing.bitmap to media.brush
How can I do this??
You should read about brushes first here.
And then use ImageBrush, something like this:
MyButton.Background = new ImageBrush(...);
(or, maybe, put the brush into resources...)
UPDATE
You can find how to create imageSource from bitmap easilly. for example, here. Like:
var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(MyProject.Properties.Resources.myImage.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
MyButton.Background = new ImageBrush(bitmapSource);
In a WPF application, you do usually not add image resources as you did in WinForms.
Instead you add the image file directly to your Visual Studio project, just like any other file. If there are multiple images, it may make sense to put them in a subfolder of the project (e.g. called "images"). The Build Action of that file has to be set to Resource (which is the default for image files).
Now you can create a BitmapImage from a Pack URI to that file.
Finally you create an ImageBrush from the BitmapImage to set the Background property.
var uri = new Uri("pack://application:,,,/images/myImage.jpg");
var image = new BitmapImage(uri);
MyButton.Background = new ImageBrush(image);

Cannot set image source in code behind

There are numerous questions and answers regarding setting image source in code behind, such as this Setting WPF image source in code.
I have followed all these steps and yet not able to set an image. I code in WPF C# in VS2010. I have all my image files under a folder named "Images" and all the image files are set to Copy always. And Build Action is set to Resource, as instructed from documentation.
My code is as follow. I set a dog.png in XAML and change it to cat.png in code behind.
// my XAML
<Image x:Name="imgAnimal" Source="Images/dog.png" />
// my C#
BitmapImage img = new BitmapImage();
img.UriSource = new Uri(#"pack://application:,,,/FooApplication;component/Images/cat.png");
imgAnimal.Source = img;
Then I get a empty, blank image of emptiness. I do not understand why .NET makes setting an image so complicated..w
[EDIT]
So the following works
imgAnimal.Source = new BitmapImage(new Uri(#"pack://application:,,,/FooApplication;component/Images/cat.png"));
It works, but I do not see any difference between the two code. Why the earlier doesn't work and the latter does? To me they are the same..
Try following:
imgAnimal.Source = new BitmapImage(new Uri("/FooApplication;component/Images/cat.png", UriKind.Relative));
Default UriKind is Absolute, you should rather use Relative one.
[EDIT]
Use BeginInit and EndInit:
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(#"pack://application:,,,/FooApplication;component/Images/cat.png");
img.EndInit();
imgAnimal.Source = img;

c# winforms webbrowser control - how to download an image?

In a Winforms app I have a webbrowser control that is logged in to a site.
Now I want to download an image (that can only be downloaded when logged in to that site) programmatically.
So how do I tell my webbrowser control to download an image, ie. http://www.example.com/image.jpg, and save it somewhere ?
If you don't want to save the file directly to your hard drive, you can download it into a stream. e.g.
WebClient wc = new WebClient();
byte[] bytes = wc.DownloadData("http://www.example.com/image.jpg");
Bitmap b = new Bitmap(new MemoryStream(bytes));
If you then wish to save it to your hard drive, you can call the Bitmap.Save() method. e.g.
b.Save("bitmap.jpg");
I guess you can't do that silently using a WebBrowser. Don't forget that it's an IE instance at the end. What you can do is: Navigate to the ImageURL, then Invoke ShowSaveAsDialog() that will show a Save as dialog to the user to save the Image:
WebBrowser wb = new WebBrowseR();
wb.Navigate("ImageURL");
wb.ShowSaveAsDialog();
A better solution is to get the Image using a WebClient
System.Net.WebClient wc = new System.Net.WebClient();
wc.Credentials = new System.Net.NetworkCredential("username", "password"); //Authenticates to the website - Call it only if the image url needs authentication first
wc.DownloadFile("imageURL", "downloadedImage.jpg"); //Downloads the imageURL to the local file downloadedImage.jpg

Categories

Resources