2013-06-06 12 views
5

Voglio salvare un'immagine all'interno degli appunti in Winrt su file. ma non ho trovato modo. puoi aiutare per favore?Salvataggio del modulo immagine Appunti

var dataPackage = Clipboard.GetContent(); 
      var t = await dataPackage.GetBitmapAsync(); 
      var t2 = await t.OpenReadAsync(); 
      t2.AsStream(); 
      t2.Seek(0); 
      BitmapImage bitmapImage = new BitmapImage(); 
      bitmapImage.SetSource(t2); 
      Image image = new Image(); 
      image.Source = bitmapImage;< 
+1

Qual è il problema? Questo errore? – DonBoitnott

+0

nessun errore. ma non so come posso salvarlo in un file. – Taladan

+0

Hai un oggetto 'Immagine' ... così:' image.Save ("C: \\ myDir \\ myFile.png", System.Drawing.Imaging.ImageFormat.Png); ' – DonBoitnott

risposta

0

Tanto semplice come quello, appena aggiunto l'ultima riga al codice esistente (ad esempio per PNG)

var dataPackage = Clipboard.GetContent(); 
      var t = await dataPackage.GetBitmapAsync(); 
      var t2 = await t.OpenReadAsync(); 
      t2.AsStream(); 
      t2.Seek(0); 
      BitmapImage bitmapImage = new BitmapImage(); 
      bitmapImage.SetSource(t2); 
      Image image = new Image(); 
      image.Source = bitmapImage; 
      image.Save("ImagePathToStore.png", System.Drawing.Imaging.ImageFormat.PNG); 

Link for Supported imageformates

+0

Image.save non è disponibile in Windows.ui.controls.image su Windows RT. – Taladan

+0

'image.Save' non è la parte delle API WinRT. – Xyroid

4

Qui si va :)

Si prega di notare che si non è possibile utilizzare QUALSIASI cartella per salvare. Ho passato ApplicationData.Current.LocalFolder.Path come desinazione. È possibile utilizzare FolderPicker e quindi passare il percorso della cartella selezionata.

private async Task StoreImageFromClipboardAsync() 
{ 
    var dataPackage = Clipboard.GetContent(); 
    var formats = dataPackage.AvailableFormats; 
    if (formats.Contains("Bitmap")) 
    { 
     var t = await dataPackage.GetBitmapAsync(); 
     var file = await ChangeIRASRToStorageFileAsync(t, ApplicationData.Current.LocalFolder.Path, "Clipboard.png"); 
    } 
} 

private async Task<StorageFile> ChangeIRASRToStorageFileAsync(IRandomAccessStreamReference MyIRASR, String StorageFolderPath, String StorageFileName) 
{ 
    IRandomAccessStreamWithContentType MyIRASWCT = await MyIRASR.OpenReadAsync(); 
    StorageFolder MyStorageFolder = await StorageFolder.GetFolderFromPathAsync(StorageFolderPath); 
    StorageFile MyStorageFile = await MyStorageFolder.CreateFileAsync(StorageFileName, CreationCollisionOption.ReplaceExisting); 
    Windows.Storage.Streams.Buffer MyBuffer = new Windows.Storage.Streams.Buffer(Convert.ToUInt32(MyIRASWCT.Size)); 
    IBuffer iBuf = await MyIRASWCT.ReadAsync(MyBuffer, MyBuffer.Capacity, InputStreamOptions.None); 
    await FileIO.WriteBufferAsync(MyStorageFile, iBuf); 
    return MyStorageFile; 
} 
0

L'ultimo esempio di post salva un'immagine BMP invece di un'immagine PNG. La seguente soluzione funziona per me se voglio salvare i file PNG dagli Appunti:

private async Task StoreImageFromClipboardAsync() 
{ 
    var dataPackage = Clipboard.GetContent(); 
    if (datapackage.Contains(StandardDataFormats.Bitmap)) 
    { 
    var t = await dataPackage.GetBitmapAsync(); 
    var file = await SaveToPngTaskFile(t, ApplicationData.Current.LocalFolder, 
     "Clipboard.png"); 
    } 
} 

public static async Task<StorageFile> SaveToPngTaskFile 
    (IRandomAccessStreamReference rndAccessStreamReference, 
    StorageFolder storageFolder, String storageFileName) 
{ 
    IRandomAccessStreamWithContentType rndAccessStreamWithContentType = 
    await rndAccessStreamReference.OpenReadAsync(); 
    StorageFile storageFile = 
    await storageFolder.CreateFileAsync(storageFileName, 
     CreationCollisionOption.GenerateUniqueName); 
    var decoder = await BitmapDecoder.CreateAsync(rndAccessStreamWithContentType); 
    var pixels = await decoder.GetPixelDataAsync(); 
    var outStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite); 
    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, outStream); 
    encoder.SetPixelData(decoder.BitmapPixelFormat, BitmapAlphaMode.Ignore, 
    decoder.OrientedPixelWidth, decoder.OrientedPixelHeight, 
    decoder.DpiX, decoder.DpiY, 
    pixels.DetachPixelData()); 
    await encoder.FlushAsync(); 
    outStream.Dispose(); 
    return storageFile; 
} 
Problemi correlati