在媒体库中存储图像 [英] Storing Image in Media Library

查看:81
本文介绍了在媒体库中存储图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将应用程序中的图像存储到媒体库中.

Im having trouble storing images from my app into the media library.

从相机捕获图像或从媒体库获取图像,然后将其存储到列表框中的Image image1中.然后,用户可以点击并按住列表框中的图像,然后会弹出一个上下文菜单,提示用户保存或删除 图像.如果点击了保存选项,则应将其存储在媒体库中.

Images are either captured from camera or taken from the media library and stored into Image image1 which is in a listbox. The user then can tap and hold on an image in the listbox and a context menu is brought up prompting the user to save or delete an image. If the save option is tapped it should store it in the media library.

按照此处给出的示例,我得到了空指针
http://msdn.microsoft.com/zh-cn/library/ff769549%28VS.92%29.aspx

这是代码
空指针出现在
bitmap.SetSource(sri.Stream);

Im getting null pointers following the example given here
http://msdn.microsoft.com/en-us/library/ff769549%28VS.92%29.aspx

here is the code
null pointer occur at
bitmap.SetSource(sri.Stream);

// click event for context menu item Save.
        private void SavePic(object sender, RoutedEventArgs e)
        {
            imgCounter++;
            string picConcat = picName + imgCounter.ToString() + fileFormat;

            // Create a virtual store and file stream. Check for duplicate tempJPEG files.
            var myStore = IsolatedStorageFile.GetUserStoreForApplication();
            if (myStore.FileExists(picConcat))
            {
                myStore.DeleteFile(picConcat);
            }

            IsolatedStorageFileStream myFileStream = myStore.CreateFile(picConcat);


            // Create a stream out of the sample JPEG file.
            // For [Application Name] in the URI, use the project name that you entered 
            // in the previous steps. Also, TestImage.jpg is an example;
            // you must enter your JPEG file name if it is different.
            StreamResourceInfo sri = null;
            Uri uri = new Uri("PicTutorial;component/" + image1, UriKind.Relative);
            sri = Application.GetResourceStream(uri);

            // Create a new WriteableBitmap object and set it to the JPEG stream.
            BitmapImage bitmap = new BitmapImage();
            bitmap.CreateOptions = BitmapCreateOptions.None;
            bitmap.SetSource(sri.Stream);
            WriteableBitmap wb = new WriteableBitmap(bitmap);

            // Encode the WriteableBitmap object to a JPEG stream.
            wb.SaveJpeg(myFileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
            myFileStream.Close();

            // Create a new stream from isolated storage, and save the JPEG file to the media library on Windows Phone.
            myFileStream = myStore.OpenFile(picConcat, FileMode.Open, FileAccess.Read);

            // Save the image to the camera roll or saved pictures album.
            MediaLibrary library = new MediaLibrary();


            Picture pic = library.SavePicture(picConcat, myFileStream);
            MessageBox.Show("Image saved to saved pictures album");

            myFileStream.Close();
        }


推荐答案

这部分


StreamResourceInfo sri = null; Uri uri = new Uri("PicTutorial;component/" + image1, UriKind.Relative); sri = Application.GetResourceStream(uri)

仅引用应用程序内部的图像(如来自已编译xap的资产中的图像).您可以使用此处的代码将图片从应用程序保存到隔离存储中,然后保存在媒体库中, 从相机或媒体库拍摄的照片不是回到媒体库中.

Only refers to images inside your application(as in assets from your compiled xap). You can use the code you have there to save a picture from your application to isolated storage and then in media lib, not pictures taken from camera or media library back in the media lib.


要执行所需的操作,请先将图片保存在隔离存储中,然后在此行中使用隔离存储中的流

To do what you requested, first save the picture in isolated storage, then use the stream from isolated storage in this line

library.SavePicture(picConcat, myISOSTOREFileStream)

要从媒体库图片中获取流,请使用图片"图片的属性.

To obtain a stream from media library pics, use the "image" property of the picture.


这篇关于在媒体库中存储图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆