如何在将bitmapsource发送到剪贴板之前减小它的大小? [英] How can I reduce the size of a bitmapsource before sending it to the clip board?

查看:104
本文介绍了如何在将bitmapsource发送到剪贴板之前减小它的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将FrameworkElement呈现给BitmapSource,然后将其复制到剪贴板。一切都按预期工作,除了图像的大小是2.5MB +。如果我将其粘贴到MS绘画中并保存为PNG文件,则大小减少到25k。如何在复制到剪贴板之前缩小图像尺寸?



I'm using the following code to render a FrameworkElement to a BitmapSource and then copying that to the clip board. Everything works as intended except the size of the image is 2.5MB+. If I paste it into MS paint and save as a PNG file the size is reduced down to 25k. How can I reduce the image size before copying to the clip board?

public static BitmapSource ConvertToBitmapSource(FrameworkElement element)
        {
            var pageSize = new System.Windows.Size(element.ActualWidth, element.ActualHeight);

            element.Measure(pageSize);
            element.Arrange(new Rect(pageSize));
            element.UpdateLayout();

            RenderTargetBitmap bmp = new RenderTargetBitmap((int)pageSize.Width, (int)pageSize.Height, 96, 96, PixelFormats.Pbgra32);
            bmp.Render(element);            
            
            return bmp;
        }



然后我用它来复制到剪贴板。


Then I use this to copy to the clip board.

Clipboard.SetImage(bitmapsource);





我尝试过:



我尝试降低PixelFormat的质量,但WPF使用的是Pbgra32。可能有一些超级简单的东西我不见了。我是WPF成像的新手。



What I have tried:

I've tried reducing the PixelFormat quality, but WPF uses Pbgra32. There may be something super simple I'm missing. I'm new to WPF imaging.

推荐答案

检查接收应用程序通过剪贴板接受哪些图像格式。如果接收应用程序接受PNG图像,您可以从内存中的位图创建PNG图像并将其放在剪贴板上。



但我不在乎尺寸2.5 MB。图像传递到剪贴板后,如果不使用,则可以在应用程序中将其删除。然后,系统使用2.5 MB,只要其他数据被复制到剪贴板,就会释放。如今,系统拥有GB内存的2.5 MB并不多。
Check which image formats are accepted by the receiving application via clipboard. If the receiving application accepts PNG images, you can create a PNG image from the bitmap in memory and put that on the clipboard.

But I would not care about a size of 2.5 MB. Once the image has been passed to the clipboard it can be deleted in your application if not used anmyore. Then you have 2.5 MB used by the system which is released whenever other data are copied to the clipboard. And 2.5 MB are not much nowadays where systems have GB of memory.


这篇关于如何在将bitmapsource发送到剪贴板之前减小它的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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