简体   繁体   English

如何在Windows Phone 8的按钮单击上存储图像?

[英]How to store image on button click in windows phone 8?

I am developing an application which has many images & i want to implement that on clicking "save button" the Image should be stored to Photo Album. 我正在开发一个包含许多图像的应用程序,我想实现在单击“保存按钮”后将图像存储到相册。

Please reply i am new to app dev. 请回复,我是应用开发人员的新手。

To work with this function you just need to pass needed Image as parameter in SaveImageToPhotoHub function. 要使用此功能,您只需要在SaveImageToPhotoHub函数中将所需的Image作为参数传递即可。 private bool SaveImageToPhotoHub(WriteableBitmap bmp) { 私人布尔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;
    }

Heres more to it http://www.codeproject.com/Articles/747273/How-to-Save-Image-in-Local-Photos-album-of-Windows Windows的更多信息http://www.codeproject.com/Articles/747273/How-to-Save-Image-in-Local-Photos-album-of-Windows

Try this code. 试试这个代码。 This is in VB.Net but I am sure you can convert it yourself or through online tools for C#: 这是在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()

I found on something like that:source https://msdn.microsoft.com . 我在类似的东西上找到:源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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM