Windows 8 如何将位图图像调整为<;200 KB并满足磁贴限制(WinRT)
我正在开发一个例程来缩放一些位图图像,作为我的Window-8应用程序的磁贴通知的一部分 平铺图像必须为MaxBytes,才能确定大小。但是,下面的代码会导致无限循环。如何可靠地测量目标文件的大小Windows 8 如何将位图图像调整为<;200 KB并满足磁贴限制(WinRT),windows-8,Windows 8,我正在开发一个例程来缩放一些位图图像,作为我的Window-8应用程序的磁贴通知的一部分 平铺图像必须为MaxBytes,才能确定大小。但是,下面的代码会导致无限循环。如何可靠地测量目标文件的大小 bool isTooBig = true; int count = 0; while (isTooBig) { // create a stream from the file and decode the ima
bool isTooBig = true;
int count = 0;
while (isTooBig)
{
// create a stream from the file and decode the image
using (var sourceFileStream = await sourceFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
using (var destFileStream = await destFile.OpenAsync(FileAccessMode.ReadWrite))
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(sourceFileStream);
BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(destFileStream, decoder);
double h = decoder.OrientedPixelHeight;
double w = decoder.OrientedPixelWidth;
if (h > baselinesize || w > baselinesize)
{
uint scaledHeight, scaledWidth;
if (h >= w)
{
scaledHeight = (uint)baselinesize;
scaledWidth = (uint)((double)baselinesize * (w / h));
}
else
{
scaledWidth = (uint)baselinesize;
scaledHeight = (uint)((double)baselinesize * (h / w));
}
//Scale the bitmap to fit
enc.BitmapTransform.ScaledHeight = scaledHeight;
enc.BitmapTransform.ScaledWidth = scaledWidth;
}
// write out to the stream
await enc.FlushAsync();
await destFileStream.FlushAsync();
isTooBig = destFileStream.Size > MaxBytes;
baselinesize *= .90d * ((double)MaxBytes / (double)destFileStream.Size);
}
}
不能使用宽度x高度x着色深度(其中着色深度以字节为单位,因此32位=4字节)计算它吗。大概你是在保持纵横比,所以你只需要缩小宽度/高度,直到你发现它小于200KB
这假设输出是位图,因此未压缩。考虑到方形瓷砖的瓷砖大小为150x150,宽瓷砖的瓷砖大小为310x150,您应该能够将图像缩小到适当的大小,使用jpeg压缩,您几乎可以保证图像小于200k。将压缩质量设置为80左右。它将提供良好的压缩比,同时保持良好的图像质量。谢谢——我将对此进行调查。我是位图操作的新手