C# 在页面之间传递图像

C# 在页面之间传递图像,c#,.net,windows-phone-7,windows-phone-8,windows-phone,C#,.net,Windows Phone 7,Windows Phone 8,Windows Phone,我想在页面之间传递图像。 在其中一页上加载了一张图片,因此我有了源代码。但是,我不知道如何将此图像(源)传递到另一个页面 我试着转换成字符串,但我认为这不是一个好方法 编辑: 方法1: 首页 WriteableBitmap wb = new WriteableBitmap(logoQrCodeImage, null); if (!IsolatedStorageSettings.ApplicationSettings.Contains("State"))

我想在页面之间传递图像。 在其中一页上加载了一张图片,因此我有了源代码。但是,我不知道如何将此图像(源)传递到另一个页面

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

编辑:

方法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();
            }
            Uri url = new Uri(iconImage, UriKind.Relative);
            BitmapImage bmp = new BitmapImage(url);
            icon.Source = bmp;
 var iconImage = PhoneApplicationService.Current.State["iconLogo"].ToString();      
 Uri url = new Uri(iconImage , UriKind.Absolute);
 BitmapImage bmp = new BitmapImage(url);
 icon.Source = bmp;
错误是一致的:

IsolatedStorageSettings.ApplicationSettings.Save();
错误消息:

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

方法2: 第一页:

PhoneApplicationService.Current.State["iconLogo"] = logoQrCodeImage.Source;
 PhoneApplicationService.Current.State["iconLogo"] = logoCodeImage.Source;
第二页:

                if (IsolatedStorageSettings.ApplicationSettings.Contains("State"))
            {
                WriteableBitmap wb = IsolatedStorageSettings.ApplicationSettings["State"] as WriteableBitmap;
                icon.Source = wb;
                IsolatedStorageSettings.ApplicationSettings.Remove("State");
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
            Uri url = new Uri(iconImage, UriKind.Relative);
            BitmapImage bmp = new BitmapImage(url);
            icon.Source = bmp;
 var iconImage = PhoneApplicationService.Current.State["iconLogo"].ToString();      
 Uri url = new Uri(iconImage , UriKind.Absolute);
 BitmapImage bmp = new BitmapImage(url);
 icon.Source = bmp;
试试这个:

第一页:

PhoneApplicationService.Current.State["iconLogo"] = logoQrCodeImage.Source;
 PhoneApplicationService.Current.State["iconLogo"] = logoCodeImage.Source;
第二页:

                if (IsolatedStorageSettings.ApplicationSettings.Contains("State"))
            {
                WriteableBitmap wb = IsolatedStorageSettings.ApplicationSettings["State"] as WriteableBitmap;
                icon.Source = wb;
                IsolatedStorageSettings.ApplicationSettings.Remove("State");
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
            Uri url = new Uri(iconImage, UriKind.Relative);
            BitmapImage bmp = new BitmapImage(url);
            icon.Source = bmp;
 var iconImage = PhoneApplicationService.Current.State["iconLogo"].ToString();      
 Uri url = new Uri(iconImage , UriKind.Absolute);
 BitmapImage bmp = new BitmapImage(url);
 icon.Source = bmp;

您将获得图像。

将图像作为变量存储在IsolatedStorage设置中,并在第二页取出

像这样的东西会有用的

包括这样一个方法

 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();
            }

图像是静态的或来自web?静态-来自电话库然后您可以传递路径到我有一个错误:行内Uri url=新Uri(iconImage,UriKind.Absolute);用户代码消息未处理System.UriFormatException=无效URI:无法确定URI的格式。请将urikind设置为“urikind.Relative”。很抱歉,这是我的错误。现在没有错误,但图像未传递。(仍然没有源)。首先,您将检查PhoneApplicationService.Current.State[“iconLogo”].ToString()行中是否存在图像字符串?或者你现在能更新你的问题吗?我想看看你是如何试用这个代码的。因为我已经成功地在我的项目中实现了。因此,请更新您的问题。在我的画作“logoCodeImage”中替换了“Image Object”,但问题出在IsolatedStorageSettings.ApplicationSettings.Save()行中\n错误消息:“System.Runtime.Serialization.InvalidDataContractException”发生在System.Runtime.Serialization.ni.dll中,但未在用户代码中处理。如何将图像的可写位图对象替换为图像对象并将该对象作为可写位图对象取出?我收到一条错误消息。我已经用我的代码编辑了我的第一篇文章。明白了,错误纯粹是序列化,请检查新编辑:)。