【问题标题】:How to store image on button click in windows phone 8?如何在 Windows Phone 8 中单击按钮时存储图像?
【发布时间】:2015-07-24 16:13:21
【问题描述】:

我正在开发一个包含许多图像的应用程序,并且我想在单击“保存按钮”时将图像存储到相册中。

请回复我是应用开发新手。

【问题讨论】:

  • 图片来源是什么?
  • 图片存在于我的项目的 Assets 文件夹中并以全景视图显示。
  • 不知道你从哪里得到的图像。但希望这有助于stackoverflow.com/questions/4142169/…

标签: c# vb.net windows-phone-7 windows-phone-8


【解决方案1】:

要使用此函数,您只需将所需的图像作为参数传递给 SaveImageToPhotoHub 函数。 私有 bool SaveImageToPhotoHub(WriteableBitmap bmp) {

        using (var mediaLibrary = new MediaLibrary())
        {
            using (var stream = new MemoryStream())
            {
                var fileName = string.Format("Gs{0}.jpg", Guid.NewGuid());
                bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
                stream.Seek(0, SeekOrigin.Begin);
                var picture = mediaLibrary.SavePicture(fileName, stream);
                if (picture.Name.Contains(fileName)) return true;
            }
        }
        return false;
    }

还有更多内容http://www.codeproject.com/Articles/747273/How-to-Save-Image-in-Local-Photos-album-of-Windows

【讨论】:

    【解决方案2】:

    试试这个代码。这是在 VB.Net 中,但我相信您可以自己转换它或通过 C# 的在线工具:

        ' Create a file name for the JPEG file in isolated storage.
        Dim tempJPEG As String = "dummyImage1"
    
        ' Create a virtual store and file stream. Check for duplicate tempJPEG files.
        Dim myStore = IsolatedStorageFile.GetUserStoreForApplication()
        If myStore.FileExists(tempJPEG) Then
            myStore.DeleteFile(tempJPEG)
        End If
    
        Dim myFileStream As IsolatedStorageFileStream = myStore.CreateFile(tempJPEG)
    
    
        ' Create a stream out of the sample JPEG file.
        ' For [Application Name] in the URI, use the project name that you entered 
        ' in the previous steps. Also, TestImage.jpg is an example;
        ' you must enter your JPEG file name if it is different.
        Dim sri As StreamResourceInfo = Nothing
        Dim uri As New Uri("/projectName;component/Assets/1.jpg", UriKind.Relative)
        sri = Application.GetResourceStream(uri)
    
        ' Create a new WriteableBitmap object and set it to the JPEG stream.
        Dim bitmap As New BitmapImage()
        bitmap.CreateOptions = BitmapCreateOptions.None
        bitmap.SetSource(sri.Stream)
        Dim wb As New WriteableBitmap(bitmap)
    
        ' Encode WriteableBitmap object to a JPEG stream.
        wb.SaveJpeg(myFileStream, wb.PixelWidth, wb.PixelHeight, 0, 85)
        myFileStream.Close()
    
        ' Create a new stream from isolated storage, and save the JPEG file to the media library on Windows Phone.
        myFileStream = myStore.OpenFile(tempJPEG, FileMode.Open, FileAccess.Read)
    
        ' Save the image to the camera roll or saved pictures album.
        Dim library As New MediaLibrary()
    
        ' Save the image to the saved pictures album.
        Dim pic As Picture = library.SavePicture("dummyImage1.jpg", myFileStream)
        MessageBox.Show("Image saved to saved pictures album")
    
        myFileStream.Close()
    

    【讨论】:

      【解决方案3】:

      我在类似的东西上找到了:source https://msdn.microsoft.com

       // Informs when full resolution photo has been taken, saves to local media library and the local folder.
      void cam_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e)
      {
          string fileName = savedCounter + ".jpg";
      
          try
          {   // Write message to the UI thread.
              Deployment.Current.Dispatcher.BeginInvoke(delegate()
              {
                  txtDebug.Text = "Captured image available, saving photo.";
              });
      
              // Save photo to the media library camera roll.
              library.SavePictureToCameraRoll(fileName, e.ImageStream);
      
              // Write message to the UI thread.
              Deployment.Current.Dispatcher.BeginInvoke(delegate()
              {
                  txtDebug.Text = "Photo has been saved to camera roll.";
      
              });
      
              // Set the position of the stream back to start
              e.ImageStream.Seek(0, SeekOrigin.Begin);
      
              // Save photo as JPEG to the local folder.
              using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
              {
                  using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
                  {
                      // Initialize the buffer for 4KB disk pages.
                      byte[] readBuffer = new byte[4096];
                      int bytesRead = -1;
      
                      // Copy the image to the local folder. 
                      while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
                      {
                          targetStream.Write(readBuffer, 0, bytesRead);
                      }
                  }
              }
      
              // Write message to the UI thread.
              Deployment.Current.Dispatcher.BeginInvoke(delegate()
              {
                  txtDebug.Text = "Photo has been saved to the local folder.";
      
              });
          }
          finally
          {
              // Close image stream
              e.ImageStream.Close();
          }
      
      }
      
      // Informs when thumbnail photo has been taken, saves to the local folder
      // User will select this image in the Photos Hub to bring up the full-resolution. 
      public void cam_CaptureThumbnailAvailable(object sender, ContentReadyEventArgs e)
      {
          string fileName = savedCounter + "_th.jpg";
      
          try
          {
              // Write message to UI thread.
              Deployment.Current.Dispatcher.BeginInvoke(delegate()
              {
                  txtDebug.Text = "Captured image available, saving thumbnail.";
              });
      
              // Save thumbnail as JPEG to the local folder.
              using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
              {
                  using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
                  {
                      // Initialize the buffer for 4KB disk pages.
                      byte[] readBuffer = new byte[4096];
                      int bytesRead = -1;
      
                      // Copy the thumbnail to the local folder. 
                      while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
                      {
                          targetStream.Write(readBuffer, 0, bytesRead);
                      }
                  }
              }
      
              // Write message to UI thread.
              Deployment.Current.Dispatcher.BeginInvoke(delegate()
              {
                  txtDebug.Text = "Thumbnail has been saved to the local folder.";
      
              });
          }
          finally
          {
          // Close image stream
          e.ImageStream.Close();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-27
        • 1970-01-01
        • 1970-01-01
        • 2016-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多