gpt4 book ai didi

c# - 尝试将图像保存到 Windows Phone 8 中的媒体库时出现异常

转载 作者:太空宇宙 更新时间:2023-11-03 19:16:44 25 4
gpt4 key购买 nike

尝试将图像保存到 MediaLibrary 时出现以下错误

An exception of type 'System.InvalidOperationException' occurred in Microsoft.Xna.Framework.ni.dll but was not handled in user code

这是我使用的代码

if (SourceImage != null) // Source Image is WriteableBitmap
{
var imageArray = SourceImage.ToByteArray(); // WriteableBitmapExWinPhone (extension method)
var res = await SavePhotoToImageHub(imageArray);
await ShowMessage(res ? AppResources.MEDIA_LIBRARY_SUCCESS_MESSAGE : AppResources.MEDIA_LIBRARY_FAILURE_MESSAGE);
}

使用的方法是

private Task<bool> SavePhotoToImageHub(byte[] imageArray)
{
using (var mediaLibrary = new MediaLibrary())
{
var fileName = string.Format("Gs{0}.jpg", Guid.NewGuid());
var picture = mediaLibrary.SavePicture(fileName, imageArray);
if (picture.Name.Contains(fileName)) return Task.FromResult(true);
}

return Task.FromResult(false);
}

我也试过用流代替字节数组。

最佳答案

您需要检查的第一件事是您是否已将 ID_CAP_MEDIALIB_PHOTO 添加到您的应用程序 list 中。

第二件事是流——你试过把它重置到开头吗?这种方法对我有用:

private bool SavePhotoToImageHub(WriteableBitmap bmp)
{

using (var mediaLibrary = new MediaLibrary())
{
using (var stream = new MemoryStream())
{
var fileName = string.Format("Gs{0}.jpg", Guid.NewGuid());
bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
stream.Seek(0, SeekOrigin.Begin);
var picture = mediaLibrary.SavePicture(fileName,stream);
if (picture.Name.Contains(fileName)) return true;
}
}
return false;
}

关于c# - 尝试将图像保存到 Windows Phone 8 中的媒体库时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15947150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com