Out Of Memory-exception when navigating to page - c#

I'm making a simple Windows Phone 8.1 Silverlight app. The idea is that I can make an entry with a picture (taken with the camera) and add a title and description text to it. Once an entry is saved, a button appears on the main page to view it. So I made 3 entries and they are listed on the main page, but after navigating to their pages a few times, I get a NavigationFailed along with OutOfMemoryException. The pages are simple, they only contain 1 image along with some textblocks.
I thought the issue is that the images are still in memory, that's why I try to set them to null and force the garbage collector, but that didn't help at all. What could cause the OutOfMemory-exception?
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string id= "";
if (NavigationContext.QueryString.TryGetValue("id", out id))
{
foreach (cEntry entry in helper.entries)
{
if (entry.id.ToString() == id)
{
textBlock_viewText.Text = entry.text;
textBlock_viewTitle.Text = entry.title;
using (IsolatedStorageFile userStore = IsolatedStorageFile.GetUserStoreForApplication())
{
if (userStore.FileExists(entry.imageFileName))
{
using (IsolatedStorageFileStream imgStream = userStore.OpenFile(entry.imageFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(imgStream);
image_viewEntryImage.Source = bmp;
bmp = null;
}
}
}
}
}
}
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
image_viewEntryImage.Source = null;
GC.Collect();
}

You may need to freeze the BitmapImage.
As described here there is an issue with WPF (the typical framework for Windows Phone development) where BitmapImages can be incorrectly kept alive. While it was supposedly fixed a while back, people have reported still seeing the problem in some cases.

Instead of setting bmp as null try this.
public static void DisposeImage(BitmapImage image)
{
Uri uri= new Uri("oneXone.png", UriKind.Relative);
StreamResourceInfo sr=Application.GetResourceStream(uri);
try
{
using (Stream stream=sr.Stream)
{
image.DecodePixelWidth=1; //This is essential!
image.SetSource(stream);
}
}
catch { }
}
call this method and set source as this custom method after that assing bmp as null.
The GC not able to clear the memory. details are here Why do I get an OutOfMemoryException when I have images in my ListBox?

Related

Screenshot on Android Xamarin , Wide system screenshot

My problem in short, I want to take a screenshot for the screen that the user see, not my Activity. Let's say my Application is minimized, the screenshot has be taken for the screen itself, not for my app.
I red somewhere that it's possible on non rooted devices since API 19 maybe or 4.0 android, But I couldn't find a way to do that.
I tried many solutions on the internet but nothing work. Red a lot but found nothing.
I found this code but, sure it takes a screenshot for my application ( layout )
Here is the OnCreate Method :
ImageView img;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
Button btn = FindViewById<Button>(Resource.Id.button1);
img = FindViewById<ImageView>(Resource.Id.imageView1);
btn.Click += Btn_Click;
}
public byte[] CaptureScreen()
{
var view = Window.DecorView.RootView;
view.DrawingCacheEnabled = true;
Bitmap bitmap = view.GetDrawingCache(true);
byte[] bitmapData;
using (var stream = new MemoryStream())
{
bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
bitmapData = stream.ToArray();
}
return bitmapData;
}
To add it to an imageView when button click:
private void Btn_Click(object sender, EventArgs e)
{
Bitmap bitmap = BitmapFactory.DecodeByteArray(CaptureScreen(), 0, CaptureScreen().Length);
img.SetImageBitmap(bitmap);
}
but As I said : it takes a screenshot for the layout, not the real screen.
I tried to hide or minimize the app, but nothing happened.
I got it. After searching a lot here and there, I discover that there is a new class from java will do the job for you. It's Media projection. It'll work on Android Lollipop 5.0 or higher and doesn't need root
and all apps in play store using it as I tried to download many apps to get the idea behind taking a screenshot.
Here is the documentation about it :
https://developer.android.com/reference/android/media/projection/MediaProjection

Memory Leak playing videos on Windows IoT | UWP

I've built an app that can read video files from an USB drive and switch between them using physical buttons. The app works well for a while, but after a while the device (DragonBoard 410c, latest Windows Insider Preview Build 15051) crashes due to the fact that all memory has been consumed by the app.
Looking at the processes in the device portal, I can see the "Working Set" memory jump each time I switch a video file while the "Private Working Set" roughly stays the same (around 30MB).
Here's how I load the video file:
C#
private IReadOnlyList<StorageFile> _videofiles
// list all available video files
public void Init(){
var queryOptions = new QueryOptions();
queryOptions.FolderDepth = depth;
foreach (var fileType in fileTypes)
{
queryOptions.FileTypeFilter.Add(fileType);
}
var query = KnownFolders.RemovableDevices.CreateFileQueryWithOptions(queryOptions);
_videofiles = await query.GetFilesAsync();
}
private async void SelectVideo(int videoId)
{
StorageFile videofile = _videofiles.Where(x => x.DisplayName == videoId.ToString()).FirstOrDefault();
if (videofile != null)
{
Debug.WriteLine($"Video {videofile.DisplayName} was selected");
var stream = await videofile.OpenAsync(FileAccessMode.Read);
VideoPlayer.SetSource(stream, videofile.FileType);
}
}
// since the button interrupt is not on the UI thread, SelectVideo() is called like this
private async void SelectVideoMarshalled(int videoId)
{
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
SelectVideo(videoId);
});
}
XAML
<ContentControl x:Name="VideoPlayer" Content="{x:Bind ViewModel.VideoPlayer, Mode=OneWay}"/>
I have tried running GC.Collect() manually in several places, but no luck yet. Any ideas?
Since you have a StorageFile object, I recommend using the Source property and the file's Path instead of SetSource and opening the Stream manually.
Additionally, you should always null out the MediaElement when you're done with it (best done in OnNavigatingFrom).
Here's your code, simplified:
private void SelectVideo(string videoId)
{
var videofile = _videofiles.FirstOrDefault(x => x.DisplayName == videoId.ToString());
if (videofile == null) return;
Debug.WriteLine($"Video {videofile.DisplayName} was selected");
VideoPlayer.Source = new Uri(videofile.Path);
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
VideoPlayer.Stop();
VideoPlayer.Source = null;
base.OnNavigatedFrom(e);
}
I also have a side comment, you can x:Bind event handlers to the ViewModel.
For example, if your video file list is a ListView of string:
public void VideosListView_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e?.AddedItems?.Count > 0)
{
var fileDisplayName = e.AddedItems.FirstOrDefault() as string;
if (!string.IsNullOrEmpty(fileDisplayName))
SelectVideo(fileDisplayName);
}
}
Notice I only need to change the method signature to public and then in the XAML you can do this:
<ListView ItemsSource="{x:Bind ViewModel.VideoFiles, Mode=OneTime}"
SelectionChanged="{x:Bind ViewModel.VideosListView_OnSelectionChanged}"/>
No need to marshal back to the UI thread :)
Lastly, you can check out the demo here on GitHub where I've implemented something similar to this.
Turns out my code was fine after all. I had a Windows Update stuck / failing several times which I didn't notice.
When the update finally completed successfully the memory leaks were gone.

Capture Image From Camera set to ImageView

How to upload or add an Image to UIImageView directly from iPhone/Ipad Captured camera Image.
I have uploaded an image to UIImageView from photo library.
Now, I want upload an image directly after taken an image through camera to ImageView.
Please suggest me how to implement this.
using IOS 8.0
This can be accomplished very easily with the Xamarin.Mobile component, which is free and works with all platforms.
http://components.xamarin.com/view/xamarin.mobile
From the example they give:
using Xamarin.Media;
// ...
var picker = new MediaPicker ();
if (!picker.IsCameraAvailable)
Console.WriteLine ("No camera!");
else {
try {
MediaFile file = await picker.TakePhotoAsync (new StoreCameraMediaOptions {
Name = "test.jpg",
Directory = "MediaPickerSample"
});
Console.WriteLine (file.Path);
} catch (OperationCanceledException) {
Console.WriteLine ("Canceled");
}
}
After you take the picture, it is saved to the directory you specified, with the name you specified. To easily retrieve this picture and display it with your ImageView using the above example you can do the following:
//file is declared above as type MediaFile
UIImage image = new UIImage(file.Path);
//Fill in with whatever your ImageView is
yourImageView.Image = image;
Edit:
Just a note that the above needs to be asynchronous. So if you want to launch the camera from a button call, for instance, you just slightly modify the .TouchUpInside event:
exampleButton.TouchUpInside += async (object sender, EventArgs e) => {
//Code from above goes in here, make sure you have async after the +=
};
Otherwise you could wrap the code from above in a function and add async to that:
public async void CaptureImage()
{
//Code from above goes here
}
You will need to use AVFoundation to do this. Check out the AVCam sample project in Xcode:
https://developer.apple.com/library/ios/samplecode/AVCam/Introduction/Intro.html

Coudln't remove file from View Model: IOException was unhandled

I was trying to delete an Image from the view Model after some modification but I'm getting this error message IOException was unhandled
I disposed the current bitmap currImgHandler.CurrentBitmap.Dispose();and also made the Image Source to be null ViewedPhoto.Source = null;
if i click on the button which has the Rotate function (90°) once and clikcing on the delete function which produces the error IOException was unhandled
If i clcik on the button Roate function twicethen click on Delete function; I don't get any error and it deletes image without any problem.
What is the mistake am I doing here? Thank you
Selecting an Image from ListView:
private string saveFilname;
private void showImage(object sender, SelectionChangedEventArgs args)
{
ListBox list = ((ListBox)sender);
if (list != null)
{
int index = list.SelectedIndex;
if (index >= 0)
{
ImageFileViewModel image = imageListBox.SelectedItem as ImageFileViewModel;
if ((image != null))
{
saveFilname = image.FileName.ToString();
currImgHandler.CurrentFileHandler.Load(image.FileName);
PaintImage();
}
}
}
}
Delete Function:
private void bDeletePhoto_Click(object sender, RoutedEventArgs e)
{
ImageFileCollectionViewModel viewModel = imageListBox.DataContext as ImageFileCollectionViewModel;
if (viewModel != null)
{
ImageFileViewModel image = imageListBox.SelectedItem as ImageFileViewModel;
if (image != null)
{
//remove physical file from disk:
currImgHandler.CurrentBitmap.Dispose();
ViewedPhoto.Source = null;
File.Delete(image.FileName);
//remove item from ObservableCollection:
viewModel.AllImages.Remove(image);
}
}
}
//Rotate Function:
private void Button_Click(object sender, RoutedEventArgs e)//Rotate
{
currImgHandler.CurrentRotationHandler.Flip(RotateFlipType.Rotate90FlipNone);
PaintImage();
}
private void PaintImage()
{
System.IO.MemoryStream stream = new System.IO.MemoryStream();
currImgHandler.CurrentBitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
stream.Position = 0;
byte[] data = new byte[stream.Length];
stream.Read(data, 0, Convert.ToInt32(stream.Length));
BitmapImage bmapImage = new BitmapImage();
bmapImage.BeginInit();
bmapImage.CacheOption = BitmapCacheOption.OnLoad;
bmapImage.StreamSource = stream;
bmapImage.EndInit();
ViewedPhoto.Source = bmapImage; //ImageBox
ViewedPhoto.Stretch = Stretch.Uniform;
}
Error message:
Your application is throwing an IOException. Likely culprit:
File.Delete(image.FileName);
IMO You should avoid making calls like this directly in event handlers for the reason demonstrated here - it is hard to catch exceptions thrown directly from an event handler.
If implementing MVVM this doesn't tend to occur.
Perhaps the account privileges under which your application is running are not sufficient to let you delete the file. Please check the credentials of this account against operations on the file you want to delete.
It's only a supposition though, it's hard to be sure without the IOEXception content, and it could also be because the path you're using doesn't exist, etc...
After Drew Rsuggestion i have added a method in the MVVM itself:
public void RemoveOldPhotoItem(ImageFileViewModel imageFile)
{
this._allImages.Remove(imageFile);
this.DataItemsCount++;
File.Delete(imageFile.FileName);
}
I need to dispose the image which was used by a 3rd party .dll....which i disposed properly.
Sorry guys it was my mistake. Thank you for your support!

Exception: Parameter is not valid (on passing new image to pictureBox)

I already had an image inside PictureBox control, and now I want to pass a new one to it.
What happens, is that allpication Disposes (and I catch an exception: "Parameter is not valid").
This is my code:
using (Image img = Image.FromFile(open.FileName))
{
part.Picture = img;
pictureBox1.InitialImage = null;
pictureBox1.Image = img;
}
So when the code goes out of the method, it goes streight to Displose of this and main form. I catch the exception only on line where Form1 was started. On this one there is no excpetions what so ever.
It must be something wrong while pictureBox is painting (inside Paint event), but I am not surbsribed to it.
I really dont have any more clue how to salve this issue. I have even tried to use to clear all resources (by calling garbage collection), but nothing seems to work.
One more thing: "part" is a reference of the List, so when I try to Delete the current image (to replace it with a new one) I got another exception, like:
"The process cannot access the file because it is being used by another process".
Does this has something to do with the 1st exception (when new image is not painted in pictureBox)?
As Reed noted, the Image you are pulling from open.Filename is being disposed once you exit the using() statement. Your picturebox is still referencing this image in memory, so when it is disposed, you lose what was stored in your picturebox too.
What you really need is a unique copy of the image you are pulling.
using (Image sourceImg = Image.FromFile(open.Filename))
{
Image clonedImg = new Bitmap(sourceImg.Width, sourceImg.Height, PixelFormat.Format32bppArgb);
using (var copy = Graphics.FromImage(clonedImg))
{
copy.DrawImage(sourceImg, 0, 0);
}
pictureBox1.InitialImage = null;
pictureBox1.Image = clonedImg;
}
This way, your file will be unlocked as soon as you exit this block, and you'll keep a unique copy of your image in the picturebox.
The problem is that, after this code executes, pictureBox1.Image is referring to an Image which has been disposed.
If you do not wrap the Image creation in a using, it should correct your issue.
Image img = Image.FromFile(open.FileName);
part.Picture = img;
pictureBox1.InitialImage = null;
pictureBox1.Image = img; // You can't dispose of this, or it won't be valid when PictureBox uses it!
You could also do something like the following create a method that loads the images and then pass it back to the Image Control for example this is what I am using when I want to populate an Image Ctrl
I have a windows form with 3 different Images that I want to load but I am only showing the code for One since I call the same method for all 3 Image Controls
#region Codes for browsing for a picture
/// <summary>
/// this.picStudent the name of the Image Control
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStudentPic_Click(object sender, EventArgs e)
{
Image picture = (Image)BrowseForPicture();
this.picStudent.Image = picture;
this.picStudent.SizeMode = PictureBoxSizeMode.StretchImage;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private Bitmap BrowseForPicture()
{
// Bitmap picture = null;
try
{
if (this.fdlgStudentPic.ShowDialog() == DialogResult.OK)
{
byte[] imageBytes = File.ReadAllBytes(this.fdlgStudentPic.FileName);
StudentPic = new Bitmap( this.fdlgStudentPic.FileName);
StuInfo.StudentPic = imageBytes;
}
else
{
StudentPic = Properties.Resources.NoPhotoAvailable;
}
}
catch (Exception)
{
MessageBox.Show("That was not a picture.", "Browse for picture");
StudentPic = this.BrowseForPicture();
}
return StudentPic;
}
#endregion
Yes, this is now working, but strange, I would almost swear I tried this way too.
Ok, never mind, just that it works.
What is troubling me is something else too, which is in my opinion the same as your code, but its not working, its again trying to Dispose application (with same exception).
This is an example code:
using(Image img = Image.FromFile(open.FileName))
{
part.Picture = img;
}
pictureBox1.InitialImage = null;
pictureBox1.Image = part.Picture; //Picture is a propery in a class
Now I pass an actual image into a generic list, and try to assign new image to pictureBox from it, but, again as I said, exception is thrown (and application is terminated).
Why?

Categories