我有一个简单的问题,但到目前为止,我还没有找到答案:如何在C#WinRT / WinMD项目中调整jpeg图像的大小并将其另存为新的jpeg?
我正在开发Windows 8 Metro应用程序,用于从某些站点下载每日图像并将其显示在Live Tile上。问题是图像必须小于1024x1024且小于200kB,否则它将不会显示在图块上:http : //msdn.microsoft.com/zh- cn/library/windows/apps/hh465403.aspx
如果我得到更大的图像,如何调整其大小以适合Live Tile?我在考虑保持宽高比的简单调整大小,例如width / 2和height / 2。
此处的特定要求是代码必须作为Windows运行时组件运行,因此WriteableBitmapEx库在这里不起作用- 仅适用于常规WinRT项目。甚至有一个分支将WriteableBitmapEx作为winmd项目,但还远远没有完成。
从此处获取的如何缩放和裁剪的示例:
async private void BitmapTransformTest() { // hard coded image location string filePath = "C:\\Users\\Public\\Pictures\\Sample Pictures\\fantasy-dragons-wallpaper.jpg"; StorageFile file = await StorageFile.GetFileFromPathAsync(filePath); if (file == null) return; // create a stream from the file and decode the image var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); // create a new stream and encoder for the new image InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream(); BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(ras, decoder); // convert the entire bitmap to a 100px by 100px bitmap enc.BitmapTransform.ScaledHeight = 100; enc.BitmapTransform.ScaledWidth = 100; BitmapBounds bounds = new BitmapBounds(); bounds.Height = 50; bounds.Width = 50; bounds.X = 50; bounds.Y = 50; enc.BitmapTransform.Bounds = bounds; // write out to the stream try { await enc.FlushAsync(); } catch (Exception ex) { string s = ex.ToString(); } // render the stream to the screen BitmapImage bImg = new BitmapImage(); bImg.SetSource(ras); img.Source = bImg; // image element in xaml }