gpt4 book ai didi

c# - 如何在 C# WinRT/winmd 中调整图像大小?

转载 作者:可可西里 更新时间:2023-11-01 03:13:25 26 4
gpt4 key购买 nike

我有一个简单的问题,但到目前为止我还没有找到答案:How to resize jpeg image in C# WinRT/WinMD project and save it as new jpeg?

我正在开发 Windows 8 Metro 应用程序,用于从某个站点下载每日图像并将其显示在动态磁贴上。问题是图像必须小于 1024x1024 且小于 200kB,否则它不会显示在图 block 上: http://msdn.microsoft.com/en-us/library/windows/apps/hh465403.aspx

如果我有更大的图像,如何调整它的大小以适合动态磁贴?我正在考虑简单的调整大小,例如 width/2 和 height/2 并保持纵横比。

此处的具体要求是代码必须作为 Windows 运行时组件运行,因此 WriteableBitmapEx 库在这里不起作用 - 它仅适用于常规 WinRT 项目。甚至还有一个 WriteableBitmapEx 分支作为 winmd 项目,但它还远未准备好。

最佳答案

如何缩放和裁剪的示例取自 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# - 如何在 C# WinRT/winmd 中调整图像大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12349611/

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