我有一张图片,person1.png
,还有另外四张图片,person2.png
,person3.png
,person5 .png
和 person4.png
。我想用 C# 代码重命名这些图像。我该怎么做?
由于 PNG 文件在您的 XAP 中,您可以像这样将它们保存到您的 IsolatedStorage 中:
//make sure PNG_IMAGE is set as 'Content' build type
var pngStream= Application.GetResourceStream(new Uri(PNG_IMAGE, UriKind.Relative)).Stream;
int counter;
byte[] buffer = new byte[1024];
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(IMAGE_NAME, FileMode.Create, isf))
{
counter = 0;
while (0 < (counter = pngStream.Read(buffer, 0, buffer.Length)))
{
isfs.Write(buffer, 0, counter);
}
pngStream.Close();
}
}
在这里您可以通过更改IMAGE_NAME
将其保存为您想要的任何文件名。
要再次读出来,你可以这样做:
byte[] streamData;
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isfs = isf.OpenFile("image.png", FileMode.Open, FileAccess.Read))
{
streamData = new byte[isfs.Length];
isfs.Read(streamData, 0, streamData.Length);
}
}
MemoryStream ms = new MemoryStream(streamData);
BitmapImage bmpImage= new BitmapImage();
bmpImage.SetSource(ms);
image1.Source = bmpImage; //image1 being your Image control
我是一名优秀的程序员,十分优秀!