[英]show image in picture gallery in windows phone 7
我在wp7中有一個圖像應用程序。
class Images
{
public string Title {get;set;}
public string Path {get;set;}
}
在頁面級別,我將標題和路徑(相對於我的應用程序)綁定到列表。
我需要的是,當用戶單擊列表項時,相應的圖像在Windows Phone 7的圖片庫中打開。
您應該澄清您的問題,但是我想Path
是您的圖像在隔離存儲中的位置。 提供該Image
是xaml中Image
的名稱
img.Source = GetImage(LoadIfExists(image.Path));
LoadIfExists
返回隔離存儲中文件的二進制數據, LoadIfExists
將其作為WriteableBitmap
返回:
public static WriteableBitmap GetImage(byte[] buffer)
{
int width = buffer[0] * 256 + buffer[1];
int height = buffer[2] * 256 + buffer[3];
long matrixSize = width * height;
WriteableBitmap retVal = new WriteableBitmap(width, height);
int bufferPos = 4;
for (int matrixPos = 0; matrixPos < matrixSize; matrixPos++)
{
int pixel = buffer[bufferPos++];
pixel = pixel << 8 | buffer[bufferPos++];
pixel = pixel << 8 | buffer[bufferPos++];
pixel = pixel << 8 | buffer[bufferPos++];
retVal.Pixels[matrixPos] = pixel;
}
return retVal;
}
public static byte[] LoadIfExists(string fileName)
{
byte[] retVal;
using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
if (iso.FileExists(fileName))
{
using (IsolatedStorageFileStream stream = iso.OpenFile(fileName, FileMode.Open))
{
retVal = new byte[stream.Length];
stream.Read(retVal, 0, retVal.Length);
}
}
else
{
retVal = new byte[0];
}
}
return retVal;
}
如果要將圖像寫入圖片庫,則基本上是相同的過程,最后調用MediaLibrary
SavePictureToCameraRoll()
,如本MSDN文章所述。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.