如何重命名图像

本文关键字:图像 重命名 | 更新日期: 2023-09-27 17:58:37

我有一个图像person1.png和其他四个图像person2.pngperson3.pngperson5.pngperson4.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

使用此处记录的FileInfo.MoveTo方法。将文件移动到相同的路径但使用不同的名称是重命名文件的方式。

FileInfo fInfo = new FileInfo ("path'to'person1.png"); fInfo.MoveTo("path'to'newname.png")

如果您需要操作路径,请使用此处记录的Path.Combine方法

在Windows Phone 7上,复制或移动(重命名)文件的API方法不存在。(请参见http://msdn.microsoft.com/en-us/library/57z06scs(v=VS.95).aspx)因此你必须自己做这件事。

类似于:

var oldName = "file.old"; var newName = "file.new";
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (var readStream = new IsolatedStorageFileStream(oldName, FileMode.Open, store))
    using (var writeStream = new IsolatedStorageFileStream(newName, FileMode.Create, store))
    using (var reader = new StreamReader(readStream))
    using (var writer = new StreamWriter(writeStream))
    {
        writer.Write(reader.ReadToEnd());
    }
    store.DeleteFile(oldName); 
}

上传图像时,此函数会自动将图像名称更改为Full date,并返回保存图像的完整路径及其新名称。

 string path = upload_Image(FileUpload1, "~/images/");
 if (!path.Equals(""))
    {
        //use the path var..
    }

这就是的功能

    string upload_Image(FileUpload fileupload, string ImageSavedPath)
{
    FileUpload fu = fileupload;
    string imagepath = "";
    if (fileupload.HasFile)
    {
        string filepath = Server.MapPath(ImageSavedPath);
        String fileExtension = System.IO.Path.GetExtension(fu.FileName).ToLower();
        String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
        for (int i = 0; i < allowedExtensions.Length; i++)
        {
            if (fileExtension == allowedExtensions[i])
            {
                try
                {
                    string s_newfilename = DateTime.Now.Year.ToString() +
                        DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
                        DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + fileExtension;
                    fu.PostedFile.SaveAs(filepath + s_newfilename);
                    imagepath = ImageSavedPath + s_newfilename;
                }
                catch (Exception ex)
                {
                    return "";
                }
            }
        }
    }
    return imagepath;
}

如果你需要更多帮助,我会尝试:)