首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何清除WritableBitmap内存

如何清除WritableBitmap内存
EN

Stack Overflow用户
提问于 2014-10-13 14:01:22
回答 2查看 217关注 0票数 2

我正在使用PhotoChooserTask从图片库中挑选图像,这是我的设置

代码语言:javascript
复制
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
    PhotoChooserTask photo = new PhotoChooserTask();
    photo.Completed += photo_Completed;
    photo.Show();
}

void photo_Completed(object sender, PhotoResult e)
{
    if (e.ChosenPhoto != null)
    {
        WriteableBitmap wbmp1 = PictureDecoder.DecodeJpeg(e.ChoosenPhoto, (int)scrnWidth, (int)scrnHeight);
        ImageBrush iBru = new ImageBrush();
        iBru.ImageSource = wbmp1;
        iBru.Stretch = Stretch.Fill;
        ContentPanel.Background = iBru;
    }
}

Problem:通过这种方式,它只适用于.JPEG图像

为了使它与其他image formats一起工作,我尝试了以下方法:

代码语言:javascript
复制
void photo_Completed(object sender, PhotoResult e)
{
    if (e.ChosenPhoto != null)
    {
        WriteableBitmap wBmp = new WriteableBitmap(0, 0);//6930432
        wBmp.SetSource(e.ChosenPhoto);//23105536
        MemoryStream tmpStream = new MemoryStream();//23105536
        wBmp.SaveJpeg(tmpStream, (int)scrnWidth, (int)scrnHeight, 0, 100);//22831104
        tmpStream.Seek(0, SeekOrigin.Begin);//22831104

        WriteableBitmap wbmp1 = PictureDecoder.DecodeJpeg(tmpStream, (int)scrnWidth, (int)scrnHeight);//24449024
        ImageBrush iBru = new ImageBrush();//24449024
        iBru.ImageSource = wbmp1;//24449024
        iBru.Stretch = Stretch.Fill;
        ContentPanel.Background = iBru;
    }
}

这种方式适用于不同的图像格式,但内存效率不高。

为了更好地理解,我提到了在每一行之后使用的bytes的数量。

问题:在后面的代码片段中,我不再需要wbmp了,如何清除wbmp对象使用的内存?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-13 17:44:22

不要使用WriteableBitmap,而是这样做:

代码语言:javascript
复制
var bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource( e.ChosenPhoto );
var ib = new ImageBrush() { ImageSource = bmp, Stretch = Stretch.Fill };
ContentPanel.Background = ib;
票数 2
EN

Stack Overflow用户

发布于 2014-11-11 09:26:49

正如@Soonts所建议的那样,我使用了BitmapImage,它解决了我的目的,

代码语言:javascript
复制
BitmapImage bmp = new BitmapImage();
bmp.DecodePixelWidth = (int)scrnWidth;
bmp.DecodePixelHeight = (int)scrnHeight;
bmp.SetSource(e.ChosenPhoto);

它消耗较少的memory,我们可以通过scale下载image

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26341861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档