在页面之间传递图像

时间:2013-11-19 13:01:05

标签: c# .net windows-phone-7 windows-phone-8 windows-phone

我想在页面之间传递图像。 在其中一个页面加载图片,所以我有源。但是,我不知道如何将此图像(源)传递到另一个页面?

我尝试转换为字符串,但我认为这不是一个好方法。

编辑:

方法1: 第一页

WriteableBitmap wb = new WriteableBitmap(logoQrCodeImage, null);
            if (!IsolatedStorageSettings.ApplicationSettings.Contains("State"))
            {
                IsolatedStorageSettings.ApplicationSettings["State"] = wb;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }

第二页:

                if (IsolatedStorageSettings.ApplicationSettings.Contains("State"))
            {
                WriteableBitmap wb = IsolatedStorageSettings.ApplicationSettings["State"] as WriteableBitmap;
                icon.Source = wb;
                IsolatedStorageSettings.ApplicationSettings.Remove("State");
                IsolatedStorageSettings.ApplicationSettings.Save();
            }

错误符合:

IsolatedStorageSettings.ApplicationSettings.Save();

错误讯息:

  

System.Runtime.Serialization.InvalidDataContractException'发生在System.Runtime.Serialization.ni.dll中,但未在用户代码中处理

方法2: 第一页:

PhoneApplicationService.Current.State["iconLogo"] = logoQrCodeImage.Source;

第二页:

            Uri url = new Uri(iconImage, UriKind.Relative);
            BitmapImage bmp = new BitmapImage(url);
            icon.Source = bmp;

2 个答案:

答案 0 :(得分:2)

试试这个:

第一页:

 PhoneApplicationService.Current.State["iconLogo"] = logoCodeImage.Source;

第二页:

 var iconImage = PhoneApplicationService.Current.State["iconLogo"].ToString();      
 Uri url = new Uri(iconImage , UriKind.Absolute);
 BitmapImage bmp = new BitmapImage(url);
 icon.Source = bmp;

你会得到你的形象。

答案 1 :(得分:0)

将图像存储为IsolatedStorageSettings中的变量,并在第二页上将其取出。

这样的事情会起作用。

包括这样的方法

 public static byte[] ConvertToBytes(WriteableBitmap bitmapImage)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                Extensions.SaveJpeg(bitmapImage, ms,
                    bitmapImage.PixelWidth, bitmapImage.PixelHeight, 0, 100);

                return ms.ToArray();
            }
        }

节省 -

如果图像i1是您的徽标图像,那么。

    WritableBitmap wb=new WritableBitmap (i1,null);
Byte [] array=ConvertToBytes(wb)
        if(!IsolatedStorageSettings.ApplicationSettings.Contains("State"))
        {
         IsolatedStorageSettings.ApplicationSettings["State"] = array;
                        IsolatedStorageSettings.ApplicationSettings.Save();
        }

检索 -

    if(IsolatedStorageSettings.ApplicationSettings.Contains("State"))
            {
           Byte [] array=  IsolatedStorageSettings.ApplicationSettings["State"] as Byte[];
 MemoryStream stream = new MemoryStream(array);
                WriteableBitmap wb= new WriteableBitmap(200, 200);
                wb.LoadJpeg(stream);
    //now assign wb as a source to any image control.
        IsolatedStorageSettings.ApplicationSettings.Remove("State");
                            IsolatedStorageSettings.ApplicationSettings.Save();
            }