如何调整在C#的WinRT图像/ winmd? [英] How to resize Image in C# WinRT/winmd?
问题描述
我有很简单的问题,但到目前为止,我发现没有答案:如何调整JPEG图像在C#的WinRT / WinMD项目并将其保存为新的JPEG
I've got simple question, but so far I've found no answer: how to resize jpeg image in C# WinRT/WinMD project and save it as new jpeg?
我在开发Windows 8 Metro应用下载日常的图像格式的某些网站和一个Live平铺显示它。问题是图像必须小于1024x1024的小,比小200kB的,否则它不会显示在瓷砖:
<一href=\"http://msdn.microsoft.com/en-us/library/windows/apps/hh465403.aspx\">http://msdn.microsoft.com/en-us/library/windows/apps/hh465403.aspx
I'm developing Windows 8 Metro application for downloading daily image form certain site and displaying it on a Live Tile. The problem is the image must be smaller than 1024x1024 and smaller than 200kB, otherwise it won't show on the tile: http://msdn.microsoft.com/en-us/library/windows/apps/hh465403.aspx
如果我得到了较大的图像,如何调整它的大小为动态磁贴是贴合?我想只是简单的调整大小一样宽/ 2和高度/ 2保持纵横比。
If I got larger image, how to resize it to be fit for the Live Tile? I'm thinking just about simple resize like width/2 and height/2 with keeping the aspect ration.
下面的具体要求是:code必须为Windows运行时组件运行,因此WriteableBitmapEx库将不会在这里工作 - 它仅适用于常规WinRT的项目。甚至有一个作为winmd项目WriteableBitmapEx分支,但它准备远远
The specific requirement here is that the code must run as Windows Runtime Component, so WriteableBitmapEx library won't work here - it's only available for regular WinRT projects. There is even a branch for WriteableBitmapEx as winmd project, but it's far from ready.
推荐答案
如何扩展和作物例如,从<一个取href=\"http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/490b9c01-db4b-434f-8aff-d5c495e67e55\">here:
Example of how to scale and crop taken from here:
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
}
这篇关于如何调整在C#的WinRT图像/ winmd?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!