- Home /
How do I write an Texture2D image to disk on WSA App ?
Hi Folks,
I have been trying to write an image(Texture2D) on a new file on disk on WSA Platform.
By following the Microsoft's documentation just as an sample and using a Plugin for File Picker I have been able to save a text file and also been able to write some text into it. https://docs.microsoft.com/en-us/windows/uwp/files/quickstart-reading-and-writing-files
That works fine. Now my main target is to write an image on a file (either a new file or an already existing one)
fileData = screenShot.EncodeToPNG();
Windows.Storage.StorageFolder storageFolder = Windows.Storage.KnownFolders.PicturesLibrary;
string byteConvertedtoSting = fileData.ToString();
Windows.Storage.StorageFile sampleFile = await storageFolder.CreateFileAsync(fileName, Windows.Storage.CreationCollisionOption.ReplaceExisting);
Windows.Storage.StorageFile sampleFile1 = await storageFolder.GetFileAsync(fileName);
var buffer = Windows.Security.Cryptography.CryptographicBuffer.CreateFromByteArray(fileData);
await Windows.Storage.FileIO.WriteBufferAsync(sampleFile1, buffer);
By using this code I am able to create a .png file on disk, but unable to writes the Image bytes on the file. The file always shows the size of 0kb , as its simply empty.
Am I missing something in this or is ther any other way to do this ?
Answer by dajohnso · Apr 29, 2020 at 07:21 PM
not 100% sure but it looks to me like you are trying to write data as a string and thats not going to work. Strings are a series of characters and behind the scene they terminate at the first 0 (null). You cant convert a BYTE series of data to characters so you must write them to the file as a series (blocks) of BYTEs
Actually I am using bytes to write the file, if you see the first line
fileData = screenShot.EncodeToPNG();
I am using this fileData (bytes) to write on the file. I think this line of code Confused you, I am sorry about that
string byteConvertedtoSting = fileData.ToString();
I forgot to remove this line while asking the question, this string is not used anywhere in the code.