本文介绍了如何重命名图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
限时送ChatGPT账号..我有一张图片,person1.png
,还有另外四张图片,person2.png
、person3.png
、person5.png
和 person4.png
.我想在 C# 代码中重命名这些图像.我该怎么做?
I have an image, person1.png
, and four other images, person2.png
, person3.png
, person5.png
, and person4.png
. I want to rename these images in C# code. How would I do this?
推荐答案
由于 PNG 文件在您的 XAP 中,您可以像这样将它们保存到您的独立存储中:
Since the PNG files are in your XAP, you can save them into your IsolatedStorage like this:
//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
将其保存为您想要的任何文件名.
Here you can save it to whatever file name you want by changing IMAGE_NAME
.
要再次朗读,您可以这样做:
To read it out again, you can do this:
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
这篇关于如何重命名图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论