Saving Ink Canvas to stream 结果为空白.GIF
Saving Ink Canvas to stream results in blank .GIF
我正在用 C# 编写一个 UWP 应用程序 - 这将是一个抽认卡应用程序,具有两个用于卡片 "front" 和 "back" 的 InkCanvases。 InkCanvas 功能似乎运行良好。问题是,当我将 InkCanvas 的笔画容器 "save" 转到 .GIF 文件(在这些 documents 中支持)时,生成的 .GIF 文件不包含墨水。相反,它们大多是空白的。此处示例:
Blank .GIF files
UWP 开发人员可以帮我指明正确的方向吗?这是当前形式的代码:
CreateFlashCard.xaml.cs:
private void AddToDeck(object sender, RoutedEventArgs e)
{
// If there was ink on the canvases
if (inkCanvas.InkPresenter.StrokeContainer.GetStrokes().Count > 0
&& inkCanvas2.InkPresenter.StrokeContainer.GetStrokes().Count > 0)
{
// Create the FlashCard and add it to the deck
FlashCard newCard = new FlashCard
{
Front = inkCanvas.InkPresenter,
Back = inkCanvas2.InkPresenter
};
// Add to the deck
if (Deck == null)
{
Deck = new List<FlashCard>();
}
// Now Add to the Deck
Deck.Add(newCard);
// Save the card
newCard.Save(_FolderName, Deck.Count-1);
// Now make way for new InkCanvases
inkCanvas.InkPresenter.StrokeContainer.Clear();
inkCanvas2.InkPresenter.StrokeContainer.Clear();
}
// The user is adding a new card without drawing on the old one
else
{
return;
}
}
FlashCard.cs:
/// <summary>
/// Save the canvases to GIF files in the deck's folder.
/// </summary>
/// <param name="FolderName"> The folder to save the files in</param>
/// <param name="CardIndex">The "number" of the card that is being saved</param>
public async void Save(string FolderName, int CardIndex)
{
// Find existing folder
StorageFolder folder = await ApplicationData.Current.LocalFolder.GetFolderAsync(FolderName);
if (folder == null)
{
// Error!
} else
{
// Use a utility function to write to files
// Front and Back being InkPresenters from two sep. InkCanvases
WriteWithStream(folder, CardIndex, 'A', Front );
WriteWithStream(folder, CardIndex, 'B', Back);
}
}
private async void WriteWithStream(StorageFolder folder, int CardIndex, char Letter, InkPresenter FrontOrBack)
{
// Generate the ink files, A and B
StorageFile inkFile = await folder.CreateFileAsync("Card" + CardIndex + Letter + ".gif", CreationCollisionOption.ReplaceExisting);
// Prevent changes until we're done
//CachedFileManager.DeferUpdates(inkFile);
// Open a stream
IRandomAccessStream stream = await inkFile.OpenAsync(FileAccessMode.ReadWrite);
// Write out using the stream
using (IOutputStream outputStream = stream.GetOutputStreamAt(0))
{
await FrontOrBack.StrokeContainer.SaveAsync(outputStream);
await outputStream.FlushAsync();
}
// Dispose of the stream
stream.Dispose();
// We are now done with the file
Windows.Storage.Provider.FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(inkFile);
}
谢谢大家!
已解决 - 我的 "Save function" 的异步性质与 canvas 的清除冲突 - 我需要等待文件生成,然后清除 canvas .
我正在用 C# 编写一个 UWP 应用程序 - 这将是一个抽认卡应用程序,具有两个用于卡片 "front" 和 "back" 的 InkCanvases。 InkCanvas 功能似乎运行良好。问题是,当我将 InkCanvas 的笔画容器 "save" 转到 .GIF 文件(在这些 documents 中支持)时,生成的 .GIF 文件不包含墨水。相反,它们大多是空白的。此处示例:
Blank .GIF files
UWP 开发人员可以帮我指明正确的方向吗?这是当前形式的代码:
CreateFlashCard.xaml.cs:
private void AddToDeck(object sender, RoutedEventArgs e)
{
// If there was ink on the canvases
if (inkCanvas.InkPresenter.StrokeContainer.GetStrokes().Count > 0
&& inkCanvas2.InkPresenter.StrokeContainer.GetStrokes().Count > 0)
{
// Create the FlashCard and add it to the deck
FlashCard newCard = new FlashCard
{
Front = inkCanvas.InkPresenter,
Back = inkCanvas2.InkPresenter
};
// Add to the deck
if (Deck == null)
{
Deck = new List<FlashCard>();
}
// Now Add to the Deck
Deck.Add(newCard);
// Save the card
newCard.Save(_FolderName, Deck.Count-1);
// Now make way for new InkCanvases
inkCanvas.InkPresenter.StrokeContainer.Clear();
inkCanvas2.InkPresenter.StrokeContainer.Clear();
}
// The user is adding a new card without drawing on the old one
else
{
return;
}
}
FlashCard.cs:
/// <summary>
/// Save the canvases to GIF files in the deck's folder.
/// </summary>
/// <param name="FolderName"> The folder to save the files in</param>
/// <param name="CardIndex">The "number" of the card that is being saved</param>
public async void Save(string FolderName, int CardIndex)
{
// Find existing folder
StorageFolder folder = await ApplicationData.Current.LocalFolder.GetFolderAsync(FolderName);
if (folder == null)
{
// Error!
} else
{
// Use a utility function to write to files
// Front and Back being InkPresenters from two sep. InkCanvases
WriteWithStream(folder, CardIndex, 'A', Front );
WriteWithStream(folder, CardIndex, 'B', Back);
}
}
private async void WriteWithStream(StorageFolder folder, int CardIndex, char Letter, InkPresenter FrontOrBack)
{
// Generate the ink files, A and B
StorageFile inkFile = await folder.CreateFileAsync("Card" + CardIndex + Letter + ".gif", CreationCollisionOption.ReplaceExisting);
// Prevent changes until we're done
//CachedFileManager.DeferUpdates(inkFile);
// Open a stream
IRandomAccessStream stream = await inkFile.OpenAsync(FileAccessMode.ReadWrite);
// Write out using the stream
using (IOutputStream outputStream = stream.GetOutputStreamAt(0))
{
await FrontOrBack.StrokeContainer.SaveAsync(outputStream);
await outputStream.FlushAsync();
}
// Dispose of the stream
stream.Dispose();
// We are now done with the file
Windows.Storage.Provider.FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(inkFile);
}
谢谢大家!
已解决 - 我的 "Save function" 的异步性质与 canvas 的清除冲突 - 我需要等待文件生成,然后清除 canvas .