read local image in wp7 - c#

In my app i need to open a local image for cropping. I tried this by setting the path of the image as source to a BitmapImage but it raising an error. How i can read and assign that local image to BitmapImage in WP Mango.

private WriteableBitmap ReadLocalImage(string Uri)
{
StreamResourceInfo sri = null;
Uri uri = new Uri(Uri, UriKind.Relative);
sri = Application.GetResourceStream(uri);
BitmapImage bitmap = new BitmapImage();
bitmap.CreateOptions = BitmapCreateOptions.None;
bitmap.SetSource(sri.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
return wb;
}
use this method for reading local image.

Related

Bitmap.UriSource is not getting set

This Works:
Sending Uri directly as a parameter to the constructor sets the UriSource of the object photo.
BitmapImage photo = new BitmapImage(new Uri("pack://application:,,,/Images/EmptyImage.jpg"));
Doesn't Work:
But setting the UriSource property keeps the UriSource as null
BitmapImage photo = new BitmapImage();
photo.UriSource = new Uri("pack://application:,,,/Images/EmptyImage.jpg");
According to MSDN
BitmapImage.UriSource must be in a BeginInit/EndInit block.
So you need to set it this way:
BitmapImage photo = new BitmapImage();
photo.BeginInit();
photo.UriSource = new Uri("pack://application:,,,/Images/EmptyImage.jpg");
photo.EndInit();

BitmapSource from embedded image

My goal is to draw image "someImage.png", which is embedded resource, on WPF window, in overridden OnRender method:
protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
{
base.OnRender(drawingContext);
drawingContext.DrawImage(ImageSource, Rect);
}
I found code to get my image from resources to Stream:
public BitmapSource GetSourceForOnRender()
{
System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
Stream myStream = myAssembly.GetManifestResourceStream("KisserConsole.someImage.png");
// What to do now?
return //BitmapSource
}
But how can i get or create BitmapSource now?
You can create a BitmapImage from the stream by setting its StreamSource property:
public BitmapSource GetSourceForOnRender()
{
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var bitmap = new BitmapImage();
using (var stream =
assembly.GetManifestResourceStream("KisserConsole.someImage.png"))
{
bitmap.BeginInit();
bitmap.StreamSource = stream;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
}
return bitmap;
}
That said, you would usually create a BitmapImage from a Resource File Pack URI, like e.g.
new BitmapImage(new Uri(
"pack://application:,,,/KisserConsole.someImage.png"));
You can try to use this:
Uri uri = new Uri( $"pack://application:,,,/YourAssemblyName;component/Resources/images/photo.png", UriKind.Absolute );
BitmapImage bitmap = new BitmapImage( uri );
Make sure the Build Action of the image file is set to Resource.

Loading Image from memory in Windows Phone

I'd like to be able to take a photo, display it, and keep the location so I can save it to a record and be able to display it at a later point.
I've been able to display it fine using the code
BitmapImage bmp = newBitmapImage();
bmp.SetSource(e.ChosenPhoto);
myImage.Source = bmp2;
When myImage is the image being displayed, and e is a PhotoResult object. However, as I need to save this in a record, I tried to use this code to display the photo based on the location.
string imageLoc = e.OriginalFileName;
Uri imageUri = new Uri(imageLoc, UriKind.Relative);
StreamResourceInfo resourceInfo = Application.GetResourceStream(imageUri);
BitmapImage bmp = BitmapImage();
bmp.SetSource(resourceInfo.Stream);
myImage.Source = bmp;
When I run this code, I get a System.NullReferenceException. I assume it's to do with the Application.GetResourceStream, but I'm just not certain what's going wrong.
For clarification, I'd like to be able to load and display a photo from a location such as
'C:\Data\Users\Public\Pictures\Camera Roll\imageExample.jpg'
if you want to save image in windows phone device, you need to user IsolatedStorage.
Save Image =>
String tempJPEG = "logo.jpg";
// Create virtual store and file stream. Check for duplicate tempJPEG files.
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myIsolatedStorage.FileExists(tempJPEG))
{
myIsolatedStorage.DeleteFile(tempJPEG);
}
IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);
StreamResourceInfo sri = null;
Uri uri = new Uri(tempJPEG, UriKind.Relative);
sri = Application.GetResourceStream(uri);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(sri.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
// Encode WriteableBitmap object to a JPEG stream.
Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
//wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
fileStream.Close();
}
Read Image =>
BitmapImage bi = new BitmapImage();
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("logo.jpg", FileMode.Open, FileAccess.Read))
{
bi.SetSource(fileStream);
this.img.Height = bi.PixelHeight;
this.img.Width = bi.PixelWidth;
}
}
this.img.Source = bi;
If you want to get pictures from MediaLibrary (Camera roll, Saved Pictures ..) then you can accomplish your task:
using PhotoChooserTask
or using MediaLibrary and here is good example
Your code can look for example like this (I've edited it to use only Images from Camera Roll):
You get your picture stream with line picture.GetImage() - this method return Stream which you can use for example to copy to IsolatedStorage.
private void MyImg()
{
using (MediaLibrary mediaLibrary = new MediaLibrary())
foreach (PictureAlbum album in mediaLibrary.RootPictureAlbum.Albums)
{
if (album.Name == "Camera Roll")
{
PictureCollection pictures = album.Pictures;
foreach (Picture picture in pictures)
{
// example how to use it as BitmapImage
// BitmapImage image = new BitmapImage();
// image.SetSource(picture.GetImage());
// I've commented that above out, as you can get Memory Exception if
// you try to read all pictures to memory and not handle it properly.
// Example how to copy to IsolatedStorage
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!storage.DirectoryExists("SavedImg"))
storage.CreateDirectory("SavedImg");
if (storage.FileExists("SavedImg" + #"\" + picture.Name))
storage.DeleteFile("SavedImg" + #"\" + picture.Name);
using (IsolatedStorageFileStream file = storage.CreateFile("SavedImg" + #"\" + picture.Name))
picture.GetImage().CopyTo(file);
}
}
}
}
}

Invalid URI escaping (.Net 4.5)

I have follow C# code (.Net 4.5)
var bi = new BitmapImage();
bi.BeginInit();
bi.DecodePixelWidth = 64;
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.UriSource = new Uri(_filename);
bi.EndInit();
bi.Freeze();
return bi;
At the opening Local file
C:\Users\KVV\Desktop\1111111111111\Windows%20XP6.jpg
Сatch exception
System.IO.FileNotFoundException
with FileName = C:\Users\KVV\Desktop\1111111111111\Windows XP6.jpg
%20 was replaced space
How can I create working URL for such files (need only local files).
Or may be can create BitmapImage without create url for local file.
Problem may be resolve with FileStream.
using (var stream = System.IO.File.OpenRead(s))
{
var img1 = new BitmapImage();
img1.BeginInit();
img1.CacheOption = BitmapCacheOption.OnLoad;
img1.StreamSource = stream;
img1.EndInit();
im1.Source = img1;
}
But this behavior Uri is similar to bug.

Display a StorageFile in Windows 8 Metro C#

I would like to display an image file on the UI from the Assets. I managed to store the item as a StorageFile. How can I display it? I've tried to display it in a XAML <Image> tag's Source. Is it possible to covert StorageFile to Image?
string path = #"Assets\mypicture.png";
StorageFile file = await InstallationFolder.GetFileAsync(path);
Try this function
public async Task<Image> GetImageAsync(StorageFile storageFile)
{
BitmapImage bitmapImage = new BitmapImage();
FileRandomAccessStream stream = (FileRandomAccessStream)await storageFile.OpenAsync(FileAccessMode.Read);
bitmapImage.SetSource(stream);
Image image = new Image();
image.Source = bitmapImage;
return image;
}
Try the following:
public async Task<BitmapImage> GetBitmapAsync(StorageFile storageFile)
{
BitmapImage bitmap = new BitmapImage();
IAsyncOperation<IRandomAccessStream> read = storageFile.OpenReadAsync();
IRandomAccessStream stream = await read;
bitmap.SetSource(stream);
return bitmap;
}
Call the function this way:
Image image = new Image();
image.Source = await GetBitmapAsync (file);
image.Source = new BitmapImage(new Uri("file://"+ storageFile.Path))

Categories