簡體   English   中英

C#/ WinRT:如何將圖像保存到文件

[英]C# / WinRT: How to save image to file

我是一名iOS開發人員,學習Windows Store App開發,沒有以前的Microsoft技術經驗。

我需要將圖像保存到文件中。 我覺得這很容易,但我已經有一天了,我沒有什么可以展示的。 這在Windows中是非常困難的,或者由於無知平台/ API而遺漏了一些東西。

我需要保存的圖像源是Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap RenderTargetBitmap可以將圖像作為Windows.UI.Xaml.Controls.Image的源或IBuffer返回。

我可以驗證RenderTargetBitmap是否正確渲染圖像。 但是,我無法將圖像保存到文件中。 我希望Image有一個Save()方法,但事實並非如此。 所以我認為我需要以某種方式使用IBuffer。 我接近了,我能夠將緩沖區保存到磁盤,但它沒有編碼,所以我無法打開任何東西。

所以,接下來,我嘗試將緩沖區轉換為流,使用BitmapEncoder將其編碼為PNG。 這很有效,但這讓我在IRandomAccessStream中獲得了我的PNG數據。 我不知道如何將IRandomAccessStream保存到文件中。

我嘗試了5-10種不同的復雜方法,例如通過類型轉換地獄試圖將IRandomAccessStream變成IBuffer,這樣我就可以在文件流上使用.WriteAsync()了。 我嘗試在我的IRandomAccessStream上由DataReader提供的文件流上使用DataWriter,但我遇到了類型不匹配問題。 等等

那么,我該如何保存文件? 這是我到目前為止所得到的:

private async void saveButton_Click(object sender, RoutedEventArgs e)
{

    //Create a new temporary image for saving
    //Image tempImage = new Image();

    //Create a render object
    RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
    //Render the app's display buffer
    await renderTargetBitmap.RenderAsync(null);
    //Set the temp image to the contents of the app's display buffer
    //tempImage.Source = renderTargetBitmap;

    //Create a new file picker, set the default name, and extenstion
    FileSavePicker savePicker = new FileSavePicker();
    savePicker.SuggestedFileName = "New LightTable.png";
    savePicker.FileTypeChoices.Add("Image", new List<string>(){".png"});

    //Get the file the user selected
    StorageFile saveFile = await savePicker.PickSaveFileAsync();

    //Only move on if the user actually selected a file
    if (saveFile != null)
    {
        //Get a buffer of the pixels captured from the screen
        Windows.Storage.Streams.IBuffer buffer = await renderTargetBitmap.GetPixelsAsync();

        //Get a stream of the data in the buffer
        System.IO.Stream stream = buffer.AsStream();

        //Convert the stream into a IRandomAccessStream because I don't know what I'm doing.
        Windows.Storage.Streams.IRandomAccessStream raStream = stream.AsRandomAccessStream();

        //Attempt to encode the stream into a PNG
        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, raStream);

        //Get a stream for the file the user selected
        Windows.Storage.Streams.IRandomAccessStream fileStream = await saveFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);

        //FIND SOME WAY TO SAVE raStream TO fileStream
        //Something like:
        // await fileStream.WriteAsync(raStream.AsStreamForRead());

    }

}

要么我只是錯過了最后:如何將我的raStream寫入文件,或者我的方法完全沒有。

我很感激幫助。 請記住,我現在只使用Microsoft技術進行了一周的開發。 我沒有.NET,Silverlight或其他MS技術經驗。 從iOS上的UIImage控件保存編碼圖像是單個方法調用,因此我正在盤旋的解決方案的剪切復雜性讓我覺得我錯過了一些我不知道的非常簡單的東西。

您需要將像素數據設置為StorageFile流。 有關示例,請參見http://basquang.wordpress.com/2013/09/26/windows-store-8-1-save-visual-element-to-bitmap-image-file/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM