尝试在 WPF 中缩放位图时,是什么导致 TransformedBitmap.EndInit 上出现 OverflowException?

发布于 2024-10-07 18:10:53 字数 1195 浏览 0 评论 0原文

我有以下代码:

private void Process(string path)
    {
        using (FileStream fs = File.OpenRead(path))
        {
            JpegBitmapDecoder decoder = new JpegBitmapDecoder(fs,BitmapCreateOptions.PreservePixelFormat,BitmapCacheOption.Default);
            BitmapSource bmps = decoder.Frames.First();
            double targetScale = 800.0/600.0;
            double scaleX = bmps.PixelWidth*targetScale;
            double scaleY = bmps.PixelHeight*targetScale;
            TransformedBitmap tbmp = new TransformedBitmap();
            tbmp.BeginInit();
            tbmp.Source = bmps;
            tbmp.Transform = new ScaleTransform(scaleX, scaleY);
            tbmp.EndInit();
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(tbmp));
            using (FileStream fs2 = File.OpenWrite(path+".jpg"))
            {
                Debug.WriteLine(path+".jpg");
                encoder.Save(fs2);
            }
        }
    }

它在 tbmp.EndInit(); 处抛出 OverflowException

知道为什么吗?

更新:可能值得一提的是,该方法是通过 ParallelQuery 调用的。但它并不依赖于不同线程中的任何内容。

I have the following code:

private void Process(string path)
    {
        using (FileStream fs = File.OpenRead(path))
        {
            JpegBitmapDecoder decoder = new JpegBitmapDecoder(fs,BitmapCreateOptions.PreservePixelFormat,BitmapCacheOption.Default);
            BitmapSource bmps = decoder.Frames.First();
            double targetScale = 800.0/600.0;
            double scaleX = bmps.PixelWidth*targetScale;
            double scaleY = bmps.PixelHeight*targetScale;
            TransformedBitmap tbmp = new TransformedBitmap();
            tbmp.BeginInit();
            tbmp.Source = bmps;
            tbmp.Transform = new ScaleTransform(scaleX, scaleY);
            tbmp.EndInit();
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(tbmp));
            using (FileStream fs2 = File.OpenWrite(path+".jpg"))
            {
                Debug.WriteLine(path+".jpg");
                encoder.Save(fs2);
            }
        }
    }

It throws an OverflowException at tbmp.EndInit();

Any idea why?

UPDATE: It might be worth mentioning that this method is called through a ParallelQuery. It doesn't depend on anything that could be in a different thread though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹤仙姿 2024-10-14 18:10:53

您已经计算出所需的缩放比例 800/600。不要乘以图像大小。使固定:

  tbmp.Transform = new ScaleTransform(targetScale, targetScale);

You already calculated the required scaling, 800/600. Don't multiply by the image size. Fix:

  tbmp.Transform = new ScaleTransform(targetScale, targetScale);
徒留西风 2024-10-14 18:10:53

我的猜测是因为你的规模很大。例如,假设原始图片为 1600x1200...然后将其缩放 2,133.33333x1600 倍,最终图片大小为 3,413,333 x 1,920,000 - 这是一张相当大的图片!

我怀疑你想要:

double scaleX = targetScale / bmps.PixelWidth;
double scaleY = targetScale / bmps.PixelHeight;

毕竟,我假设如果原始图片更大,你想要拉伸的是更少,而不是更多

My guess is that it's because your scale is huge. For example, suppose the original picture is 1600x1200... you're then scaling it by a factor of 2,133.33333x1600, giving you a final picture size of 3,413,333 x 1,920,000 - which is a pretty huge picture!

I suspect you wanted:

double scaleX = targetScale / bmps.PixelWidth;
double scaleY = targetScale / bmps.PixelHeight;

After all, I assume that if the original picture is bigger, you want to stretch is less, not more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文