我是一名 iOS 开发人员,正在学习 Windows 应用商店应用开发,但之前没有任何 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 种不同的复杂方法,例如通过类型转换 hell 尝试将 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/举个例子。
关于C#/WinRT : How to save image to file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21151064/
假设我正在使用JavaScript和C#代码编写一个WinRT应用程序,并且我希望我的JavaScript代码在我的C#对象上Hook一个事件。我知道这应该是可能的,但JavaScript代码会是什么样子呢?事件(但是CLR事件的概念在WinRT中表示)如何在JavaScript投影中公开?如果一个具体的例子会有所帮助,假设我的C#对象有这个事件:publiceventEventHandlerInitialized;我如何从JavaScript挂接该事件?(我确定答案隐藏在其中一个//build/videos中,但它们并不完全可搜索。) 最佳答案
我正在尝试为我的WinRT实验设置一些特定于应用程序的项目(例如APIkey)的配置文件。到目前为止,我在项目的根目录中添加了一个“config.xml”文件,在属性中将其标记为资源...然后我就卡住了。我能找到的每个例子似乎都处理JSON资源文件(它们在某种程度上按照惯例与本地化相关并且似乎不适合一般配置内容?),从磁盘加载文件(自从资源被编译到.pri文件中),或使用C#。那么我怎样才能在我的Javascript/HTML5应用程序中使用它呢?我最近的尝试是这样的:varuri=newWindows.Foundation.Uri('ms-resource:///config');v
我想要的从AppData.Local中获取一个xml文件,并将其序列化为一个列表我编码什么错误部分:ListAllTaskList=awaitobjectStorageHelper.LoadAsync();myTask是一个简单的类:publicclassmyTask{publicstringmyTitle{get;set;}publicstringmyDuetime{get;set;}}objectStorageHelper是来自CodePlex的HelpClass,LoadAsync部分如下:publicasyncTaskLoadAsync(){try{StorageFilefil
有没有等价于HttpUtility.HtmlDecode的在WinRT中?我正在开发MetroStyle应用程序并想解码来自XML文档的字符串。 最佳答案 改为使用System.Net.WebUtility 关于xml-WinRT中的HttpUtility.HtmlDecode,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8341882/
有没有办法处理来自流(或图像文件或视频文件)的帧并将帧保存在编码视频文件中。有将帧保存到编码图像文件的选项,但没有使用Windows.Graphics.Imaging命名空间保存到视频文件的选项。在mediaCapture.StartRecordToStreamAsync中,我可以使用StartRecordToStorageFileAsync将流从摄像机的视频文件保存到另一个视频文件。但是没有办法逐帧保存。感谢您的帮助! 最佳答案 MediaCapture示例展示了如何编写媒体基础转换以对每一帧进行图像处理。我怀疑您可以修改MFT以
是否可以将图像放入WinRT中的控件中,然后让用户放大和缩小等? 最佳答案 是的!最简单的方法是将其放在滚动查看器中。您可以随心所欲地放大和缩小! 关于windows-如何在Windows8(WinRT)应用程序中显示可缩放图像?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/15955442/
我正在尝试为D3D应用程序创建一个简单的手势识别器。手势识别器的工作原理是将接收到的每个点存储到容量为3的boost::circular_buffer中,然后计算缓冲区中相似FrameID的数量,如下所示:UINTTrackball::CalculateGestureSize(Windows::UI::Input::PointerPoint^pPoint){//shiftthecircularbufferqueueoneifit'sfull(commoncase)if(m_pointQueue.full()){m_pointQueue.pop_back();}//thenstoreou
我正在寻找一种从Windows8获取某些区域设置的方法。像这样的设置:有什么办法可以得到吗? 最佳答案 我建议您查看Windows.Globalization.NumberFormatting“根据用户的语言和地理区域,提供用于格式化货币、小数、百分比值和千分率值的类。”在此link你可以找到它的一些用法示例://Formatwiththeuser'sdefaultpreferences.StringdecimalCurrent=decimalFormat.Format(randomNumber);//...results.Appe
我正在尝试使用Windows.SystemPowerShell中命名空间,WinRT的一部分。程序集不是dll,而是winmd。将程序集加载到PowerShell的两种方式似乎不起作用#[System.Reflection.Assembly]::LoadFile("C:\ProgramFiles(x86)\WindowsKits\8.0\References\CommonConfiguration\Neutral\Windows.winmd")#Add-Type-Path"C:\ProgramFiles(x86)\WindowsKits\8.0\References\CommonCon
在VisualStudio2015中调试我的通用Windows应用程序时,我注意到输出窗口中存在一些错误:Exceptionthrownat0x00007FFE25967788(KernelBase.dll)inxxx.exe:0x40080201:WinRToriginateerror(parameters:0x0000000080072738,0x00000000000000C2,0x000000217DCFEFD0).Exceptionthrownat0x00007FFE25967788(KernelBase.dll)inxxx.exe:0xE06D7363:MicrosoftC