Answer the question
In order to leave comments, you need to log in
How to avoid memory overflow due to WriteableBitmap operations?
There is a page in the Windows Phone 8.1 app that initially contains almost nothing:
<Page.BottomAppBar>
<CommandBar>
<AppBarButton Icon="Add" x:Uid="AddPhotoAppButton" Click="AddPhotoClick" IsEnabled="{Binding IsButtonsEnabled}"/>
<AppBarButton Icon="Send" x:Uid="SendAppButton" Click="SendClick" IsEnabled="{Binding IsButtonsEnabled}"/>
</CommandBar>
</Page.BottomAppBar>
<ScrollViewer>
<Grid>
<Grid Grid.Row="2">
<TextBox />
</Grid>
<GridView Grid.Row="3" ItemsSource="{Binding Attachments}">
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel Holding="ListViewItem_Holding">
<Image Margin="10" Height="90" Width="90" Source="{Binding Image}"/>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
</ScrollViewer>
ObservableCollection<FilePost>();
public class FilePost
{
public Guid ID { get; set; }
public WriteableBitmap Image { get; set; }
}
WriteableBitmap wbmp;
using (var stream = await file.OpenAsync(FileAccessMode.Read))
{
wbmp = new WriteableBitmap(1, 1);
wbmp = await wbmp.FromStream(stream);
}
var ratio = wbmp.PixelWidth > wbmp.PixelHeight ? 1920.0f / wbmp.PixelWidth : 1920.0f / wbmp.PixelHeight;
if (ratio < 1)
{
var newWidth = (int)(wbmp.PixelWidth * ratio);
var newHeight = (int)(wbmp.PixelHeight * ratio);
wbmp = wbmp.Resize(newWidth, newHeight, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
}
using (IRandomAccessStream iras = new InMemoryRandomAccessStream())
{
await wbmp.ToStreamAsJpeg(iras);
var bytes = new byte[iras.Size];
var buffer = await iras.ReadAsync(bytes.AsBuffer(), (uint)iras.Size, InputStreamOptions.None);
var response = await Uploader.UploadData(GetString("ServerApi") + "File?tokenID=" + GetToken(), buffer.ToArray());
if (response.Item != Guid.Empty)
Attachments.Add(new FilePost { ID = Guid.Empty, WImage = wbmp });
}
Answer the question
In order to leave comments, you need to log in
for these things there are thumbnails - reduced images of the original, because. your smart does not have a resolution of 100500 MP, you do not need to upload the entire picture to the "thumbnails".
In fact, using bitmaps in a mobile application is evil, and storing collections of bitmaps in memory is generally not an affordable luxury. It is redundant and very heavy format. For image processing, look towards the Nokia Imaging SDK, it is just optimized for working with large images.
I would also advise avoiding such constructions: wbmp = wbmp.Resize ...
During the operation of the wbmp.Resize method, another bitmap is created, and then the wbmp variable refers to the second one, but the first one remains in memory. In theory, the garbage collector should clean up the memory, but it may not have time to do this, UNTIL the next time a large piece of memory is allocated. Therefore, the memory must be cleaned by hand.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question