Get images from directory - c#

I'm trying to get all images from a directory in WP7. I already use
var temp = Directory.GetFiles(#"\Pictures");
but it doesn't seem to work.
by the way i also tried the isolated storage solution. but none of those solution seems to work.
Is i a wp7 limitation?

you shoud use following code for images from the resources:
Uri uri = new Uri(uriString, UriKind.Relative);
String originalUriString = uri.OriginalString;
Uri resourceStreamUri = originalUriString.StartsWith("/", StringComparison.Ordinal) ? new Uri(originalUriString.TrimStart('/'), UriKind.Relative) : uri;
StreamResourceInfo streamResourceInfo = Application.GetResourceStream(resourceStreamUri);
if (null != streamResourceInfo)
{
stream = streamResourceInfo.Stream;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
Image image = new Image();
image.Source = bitmapImage;
}

Probably, you should use IsolatedStorageFile.GetFileNames instead.
Good tutorial: How to: Find Existing Files and Directories in Isolated Storage

You probalby have your pictures as Resource and not as Content. Right click on images in your solution and change the Build Action to Content.

Related

How to set source for Image Control where the image resides in application folder

I am using an image control. i am setting the source for Image control like below.
But the error shows invalid URI.
string strUri = String.Format(#"..\..\Assets\Images\Image001.jpeg");
previewImage.Source = BitmapFromUri(new Uri(strUri));
public static ImageSource BitmapFromUri(Uri source)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = source;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
return bitmap;
}
You'll have to specify that it is a relative URI by setting UriKind.Relative:
var uri = new Uri(#"..\..\Assets\Images\Image001.jpeg", UriKind.Relative);
That said, your BitmapFromUri method might be redundant (unless you have a reason to set BitmapCacheOption.OnLoad). You may probably as well use the BitmapImage constructor that takes an Uri parameter:
previewImage.Source = new BitmapImage(uri);
Please note also that the above assumes that image file is actually copied to the specified relative path. If it is part of your Visual Studio project, it might perhaps better be loaded as embedded resource via a WPF Pack URI. You would therefore have to set its Build Action to Resource.

How to set the image of an Image control at run time?

I am struggling to get a path and add it to an Image control.
Here is my code:
var uriSource1 = new Uri(#"/Earnest_Individuals:,,,;component/Images/Section5bb1.png", UriKind.Relative);
image2.Source = new BitmapImage(uriSource1);
Nothing is displayed in the image. I think something is wrong with the image path.
Assuming that Earnest_Individuals is the name of the assembly with Images/Section5bb1.png you can try
var uriSource1 = new Uri(#"pack://application:,,,/Earnest_Individuals;component/Images/Section5bb1.png");
Pack URIs in WPF
In code behind you have to explicitly write this as a Resource File Pack URI (provided that Earnest_Individuals is the name of a referenced assembly and that Section5bb1.png is a resource):
var uriSource1 = new Uri(
"pack://application:,,,/Earnest_Individuals;component/Images/Section5bb1.png");
image2.Source = new BitmapImage(uriSource1);

Programmatically set the Source of an Image (XAML)

I am working on a Windows 8 app. I need to know how to programmatically set the Source of an Image. I assumed that the Silverlight approach would work. However, it doesn't. Does anybody know how to do this? The following will not work:
string pictureUrl = GetImageUrl();
Image image = new Image();
image.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri(pictureUrl, UriKind.Relative));
image.Stretch = Stretch.None;
image.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
image.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center;
I get an Exception that says: "The given System.Uri cannot be converted into a Windows.Foundation.Uri."
However, I can't seem to find the Windows.Foundation.Uri type.
I just tried
Image.Source = new BitmapImage(
new Uri("http://yourdomain.com/image.jpg", UriKind.Absolute));
And it works without problems... I'm using System.Uri here. Maybe you have a malformed URI or you have to use an absolute URI and use UriKind.Absolute instead?
This is what I use:
string url = "ms-appx:///Assets/placeHolder.png";
image.Source = RandomAccessStreamReference.CreateFromUri(new Uri(url));
Well, Windows.Foundation.Uri is documented like this:
.NET: This type appears as System.Uri.
So the tricky bit isn't converting it into a Windows.Foundation.Uri yourself - it looks like WinRT does that for you. It looks like the problem is with the URI you're using. What is it relative to in this case? I suspect you really just need to find the right format for the URI.
This example uses a FileOpenPicker object to obtain the storage file.
You can use whatever method you need to access your file as a StorageFile object.
Logo is the name of the image control.
Reference the following code:
var fileOpenPicker = new FileOpenPicker();
fileOpenPicker.ViewMode = PickerViewMode.Thumbnail;
fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
fileOpenPicker.FileTypeFilter.Add(".png");
fileOpenPicker.FileTypeFilter.Add(".jpg");
fileOpenPicker.FileTypeFilter.Add(".jpeg");
fileOpenPicker.FileTypeFilter.Add(".bmp");
var storageFile = await fileOpenPicker.PickSingleFileAsync();
if (storageFile != null)
{
// Ensure the stream is disposed once the image is loaded
using (IRandomAccessStream fileStream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
// Set the image source to the selected bitmap
BitmapImage bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(fileStream);
Logo.Source = bitmapImage;
}
}
check your pictureUrl since it was what resulted in the exception.
but this should work as well
img.Source = new BitmapImage(new Uri(pictureUrl, UriKind.Absolute));
it should have nothing to do with Windows.Foundation.Uri. since winrt will handle it for you.
Try this format:
ms-appx:/Images/800x600/BackgroundTile.bmp
The given System.Uri cannot be converted into a Windows.Foundation.Uri
<Image Name="Img" Stretch="UniformToFill" />
var file = await KnownFolders.PicturesLibrary.GetFileAsync("2.jpg");
using(var fileStream = (await file.OpenAsync(Windows.Storage.FileAccessMode.Read))){
var bitImg= new BitmapImage();
bitImg.SetSource(fileStream);
Img.Source = bitImg;
}

Opening an image file into WritableBitmap

Here is the problem.
I want to open a file from local drives, then make it into a WritableBitmap so i can edit it.
But the problem is, i cannot create a WritableBitmap from Uri or something like that. Also i know how to open a file into BitmapImage but i cannot figure out how to open a file as WritableBitmap.
Is there way to open a file directly into a WritableBitmap,if there is not, is there a way to convert a BitmapImage to a WritableBitmap?
Thanks guys.
You can load your image file into a BitmapImage and use that as a source for your WriteableBitmap:
BitmapImage bitmap = new BitmapImage(new Uri("YourImage.jpg", UriKind.Relative));
WriteableBitmap writeableBitmap = new WriteableBitmap(bitmap);
I'm no expert and don't have immediate access to intellisense and whatnot, but here goes...
var fileBytes = File.ReadAllBytes(fileName);
var stream = new MemoryStream(fileBytes);
var bitmap = new BitmapImage(stream);
var writeableBitmap = new WritableBitmap(bitmap);
Even if not a perfect example this should be enough to point you in the right direction. Hope so.
In Silverlight 5 you can use below method to open file from local disk and convert it to BitmapImage and make it in to WriteableBitmap;
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.ShowDialog();
byte[] byteArray = new byte[] { };
if (dlg.File == null) return;
BitmapImage bi = new BitmapImage();
bi.CreateOptions = BitmapCreateOptions.None;
// bi.UriSource = new Uri(#"C:\Users\saw\Desktop\image.jpg", UriKind.RelativeOrAbsolute);
bi.SetSource(dlg.File.OpenRead());
WriteableBitmap eb=new WriteableBitmap(bi);
setting new Uri gave me error (Object reference not set to an instance of an object) while trying to create WriteableBitmap

Change image source in code behind - Wpf

I need to set image source dynamically, please note my image is in somewhere on the Network, here is my code
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri(#"pack://application:,,,\\myserver\\folder1\\Customer Data\\sample.png");
logo.EndInit(); // Getting the exception here
ImageViewer1.Source = logo;
Exception:
The URI prefix is not recognized
None of the above solutions worked for me. But this did:
myImage.Source = new BitmapImage(new Uri(#"/Images/foo.png", UriKind.Relative));
You just need one line:
ImageViewer1.Source = new BitmapImage(new Uri(#"\myserver\folder1\Customer Data\sample.png"));
The pack syntax you are using here is for an image that is contained as a Resource within your application, not for a loose file in the file system.
You simply want to pass the actual path to the UriSource:
logo.UriSource = new Uri(#"\\myserver\folder1\Customer Data\sample.png");
None of the methods worked for me as i needed to pull the image from a folder instead of adding it to the application.
The below code worked:
TestImage.Source = GetImage("/Content/Images/test.png")
private static BitmapImage GetImage(string imageUri)
{
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.UriSource = new Uri("pack://siteoforigin:,,,/" + imageUri, UriKind.RelativeOrAbsolute);
bitmapImage.EndInit();
return bitmapImage;
}
You are all wrong!
Why? Because all you need is this code to work:
(image View) / C# Img is : your Image box
Keep this as is, without change ("ms-appx:///) this is code not your app name
Images is your folder in your project you can change it.
dog.png is your file in your folder, as well as i do my folder 'Images' and file 'dog.png'
So the uri is :"ms-appx:///Images/dog.png"
and my code :
private void Button_Click(object sender, RoutedEventArgs e)
{
img.Source = new BitmapImage(new Uri("ms-appx:///Images/dog.png"));
}

Categories