如何重命名图像

编程入门 行业动态 更新时间:2024-10-28 05:24:34
本文介绍了如何重命名图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一张图片,person1.png,还有另外四张图片,person2.pngperson3.pngperson5.pngperson4.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:关键词]

本文发布于:2023-04-25 14:33:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117470.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重命名   图像

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!