C# 在Windows应用商店应用程序的宽平铺上使用方形图像
在我的应用程序中,我希望实现辅助磁贴,以便让用户直接访问我的应用程序的特定部分,并在该磁贴上显示特定于部分的信息 对于平铺图像,我想使用将直接访问的部分图标,但我只能访问直接从web链接下载的方形图像(创建平铺时)。由于我没有宽瓷砖的图像,我尝试用透明边框填充它们,以便创建宽瓷砖。我使用了WritableBitmapEx库,但我的代码导致AccessViolationExceptionC# 在Windows应用商店应用程序的宽平铺上使用方形图像,c#,windows-store-apps,image-scaling,live-tile,C#,Windows Store Apps,Image Scaling,Live Tile,在我的应用程序中,我希望实现辅助磁贴,以便让用户直接访问我的应用程序的特定部分,并在该磁贴上显示特定于部分的信息 对于平铺图像,我想使用将直接访问的部分图标,但我只能访问直接从web链接下载的方形图像(创建平铺时)。由于我没有宽瓷砖的图像,我尝试用透明边框填充它们,以便创建宽瓷砖。我使用了WritableBitmapEx库,但我的代码导致AccessViolationException StorageFile file = await ApplicationData.Current.Tempor
StorageFile file = await ApplicationData.Current.TemporaryFolder.GetFileAsync(tempFileName);
IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
WriteableBitmap bitmap = new WriteableBitmap(150, 150);
await bitmap.SetSourceAsync(fileStream);
WriteableBitmap bitmapDummy = BitmapFactory.New(310, 150);
using (bitmapDummy.GetBitmapContext())
{
using (bitmap.GetBitmapContext())
{
bitmapDummy.Blit(new Rect(80.0, 0.0, 150, 150), bitmap, new Rect(0.0, 0.0, 150, 150), WriteableBitmapExtensions.BlendMode.None);
}
}
异常发生在位图.GetBitmapContext()
上,因此我无法创建图像
我在这里做的是创建一个空的宽瓦片图像,并将正方形瓦片图像放在中间。< /P> 最后,我还想将图像转换为透明PNG,但这是另一个问题,因为我还不知道如何创建位图
提前感谢您的帮助。我找到了解决方案。在我尝试访问该图像之前,该图像似乎没有加载到WriteableBitmap中,因此引发了AccessViolationException。因此WriteableBitmap bitmap = new WriteableBitmap(150, 150);
await bitmap.SetSourceAsync(fileStream);
我开始使用
WriteableBitmap bitmap = await BitmapFactory.New(1, 1).FromStream(fileStream);
现在问题解决了
下面是为发现此问题的人调整方形图像大小和填充方形图像的完整代码
public static async Task PadPngImage(StorageFile inputFile, StorageFile outputFile, uint width, uint height, uint boundingBoxWidth, uint boundingBoxHeight)
{
// Resize the input image
StorageFile temporaryFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("PadImageTemporaryFile.png", CreationCollisionOption.ReplaceExisting);
await ResizeImage(inputFile, temporaryFile, width, height);
// Create a stream linked to resized image
using (IRandomAccessStream temporaryFileStream = await temporaryFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
{
WriteableBitmap bitmap = await BitmapFactory.New(1, 1).FromStream(temporaryFileStream);
// Create an empty image with specified bounding dimentions
WriteableBitmap bitmapDummy = BitmapFactory.New((int)boundingBoxWidth, (int)boundingBoxHeight);
using (bitmapDummy.GetBitmapContext())
{
using (bitmap.GetBitmapContext())
{
// Calculate position of the image
Rect center = new Rect((boundingBoxWidth - width) / 2, (boundingBoxHeight - height) / 2, width, height);
bitmapDummy.Blit(center, bitmap, new Rect(0.0, 0.0, width, height), WriteableBitmapExtensions.BlendMode.None);
}
}
using(IRandomAccessStream stream = await outputFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
{
await bitmapDummy.ToStream(stream, BitmapEncoder.PngEncoderId);
}
}
}
public static async Task ResizeImage(StorageFile inputFile, StorageFile outputFile, uint newWidth, uint newHeight)
{
// Input Stream & Decoder
using (IRandomAccessStream fileStream = await inputFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
// Output Stream & Encoder
using (IRandomAccessStream stream = await outputFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
{
BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(stream, decoder);
// Scale the bitmap image
encoder.BitmapTransform.ScaledWidth = newWidth;
encoder.BitmapTransform.ScaledHeight = newHeight;
encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant;
await encoder.FlushAsync();
}
}
}