gpt4 book ai didi

c# - 从独立存储中的图像设置辅助磁贴 BackgroundImage

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

这是我从图像 url 获取流的方式:

        using (var httpClient = new HttpClient())
{
response = await httpClient.GetStreamAsync(new Uri(IMAGEURL_HERE, UriKind.Absolute));
}

SaveImage(response);

这就是我将它保存到 IsoloatedStorage 的方式:

    private void SaveImage(Stream result)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(result);
var wb = new WriteableBitmap(bitmap);

using (IsolatedStorageFileStream fileStream = file.CreateFile("FILENAME.jpg"))
{
int width = wb.PixelWidth;
int height = wb.PixelHeight;
if (wb.PixelWidth > 336)
{
width = 336;
}
if (wb.PixelHeight > 336)
{
height = 336;
}
Extensions.SaveJpeg(wb, fileStream, width, height, 0, 100);
}
}
}

假设文件是​​ FILENAME.jpg,我想我可以将它设置为 BackgroundImage 到辅助图 block ,如下所示:

var tileData = new FlipTileData()
{
...
BackgroundImage = new Uri("isostore:/Shared/ShellContent/FILENAME.jpg", UriKind.Absolute),
...

这是行不通的。它不会抛出异常,只有图像不会显示。我想念什么?当然,如果我将图像 Url 作为 Uri 放到 BackgroundImage 中,它就可以工作,但这不是我想要的。

编辑:我在这里看到了类似的问题,但它对我的代码没有帮助。

最佳答案

试试这个。可能是它的帮助。

string imageFolder = @"\Shared\ShellContent"; 
string shareJPEG = "FILENAME.jpg";

private void SaveImage(Stream result)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
if(!myIsolatedStorage.DirectoryExists(imageFolder))
{
myIsolatedStorage.CreateDirectory(imageFolder);
}

if (myIsolatedStorage.FileExists(shareJPEG))
{
myIsolatedStorage.DeleteFile(shareJPEG);
}

string filePath = System.IO.Path.Combine(imageFolder, shareJPEG);
IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(filePath);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(result);
WriteableBitmap wb = new WriteableBitmap(bitmap);

// Encode WriteableBitmap object to a JPEG stream.
int width = wb.PixelWidth;
int height = wb.PixelHeight;
if (wb.PixelWidth > 336)
{
width = 336;
}
if (wb.PixelHeight > 336)
{
height = 336;
}
Extensions.SaveJpeg(wb, fileStream, width, height, 0, 100);

fileStream.Close();


}
}

private void CreateTile()
{
var tileData = new FlipTileData()
{
....
string filePath = System.IO.Path.Combine(imageFolder, shareJPEG);
BackgroundImage = new Uri(@"isostore:" + filePath, UriKind.Absolute);
....
}
}

关于c# - 从独立存储中的图像设置辅助磁贴 BackgroundImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20107640/

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