如何将位图图像的大小调整为 <200 KB 并满足平铺限制 (WinRT) [英] How to resize bitmap image to be &lt;200 KB and meet Tile restrictions (WinRT)

查看:36
本文介绍了如何将位图图像的大小调整为 <200 KB 并满足平铺限制 (WinRT)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个例程来缩放一些位图图像,使其成为我的 Window-8 应用程序的平铺通知的一部分

I am developing a routine to scale some bitmap images to be part of tile notifications for my Window-8 app

平铺图像的尺寸必须小于 200KB 且小于 1024x1024 像素.我可以根据需要使用缩放例程来调整源图像的大小以适应 1024x1024 像素的尺寸限制.

The tile images must be <200KB and less than 1024x1024 px in dimension. I am able to use a scaling routine to resize the source image as necessary to fit the 1024x1024 px dimension limitation.

如何更改源图像以保证满足大小限制?

How can I alter the source image to guarantee the size restriction will be met?

我的第一次尝试是继续缩小图像直到它清除大小阈值,并使用 isTooBig = destFileStream.Size >MaxBytes 来确定大小.但是,下面的代码会导致无限循环.如何可靠地测量目标文件的大小?

My first attempt was to continue to scale down the image until it clears the size threshold, and use isTooBig = destFileStream.Size > MaxBytes to determine the size. But, the code below results in an infinite loop. How can I reliably measure the size of the destination file?

        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);
            }
        }

推荐答案

考虑到方块尺寸为 150x150 的方形瓷砖或 310x150 的宽瓷砖,您应该能够将图像缩小到适当的尺寸,并使用 jpeg 压缩,您会很漂亮保证在200k以下.将压缩质量设置为 80 左右.它将为您提供良好的压缩比,同时保持良好的图像质量.

Considering that tile size either 150x150 for square tiles or 310x150 for wide tiles you should be able to shrink image down to the appropriate size and with jpeg compression you are pretty much guaranteed to be under 200k. Set compression quality around 80. It will give you good compression ratio while keeping decent image quality.

这篇关于如何将位图图像的大小调整为 <200 KB 并满足平铺限制 (WinRT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆