Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

C#
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.
C#
Clipboard.SetImage(bitmapsource);


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.
Posted
Updated 3-Nov-16 0:03am
v3
Comments
Dave Kreskowiak 2-Nov-16 16:42pm    
It ends up being 25K ON DISK, not in memory. In memory, it's still 2.5MB.
ryanba29 2-Nov-16 16:51pm    
Is there no way to reduce it in memory before sending it to the clip board? I have no use for it in my app again.
Dave Kreskowiak 2-Nov-16 16:59pm    
As a file, sure. But as an image representation, it'll still be 32-bits per pixel for the entire image. If you change the pixel depth and create a new image object, it'll be smaller, but you won't have the original image any more. It'll be of lower quality because the color depth will be lost.
ryanba29 2-Nov-16 17:02pm    
Update. You are correct. My image is reduced to 66k once I paste it where it's needed. If you want to post an answer saying the same I'll accept it.

1 solution

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.
 
Share this answer
 
Comments
ryanba29 3-Nov-16 9:07am    
Thank you for taking the time to respond, but my size concern here is on the clip board or in finial application. Dave Kreskowiak is correct the large size in memory dose not matter because it's reduced to the appropriate size once used off the clip board.
Jochen Arndt 3-Nov-16 9:45am    
No, Dave has not said so. He said that a file can be smaller (when using an appropriate format) or the amount of information (pixel depth) is reduced.

The final application will usually create a bitmap again (e.g. Paint will convert a PNG back to an in memory bitmap which has again the original size where it does not matter if the PNG is copied from the clipboard or loaded from file.

So if you really want to reduce memory usage you must reduce the amount of information (pixel depth and/or resolution).

But I still can't understand your size concerns (especially on the receiving application because you don't have control on it).

For the clipboard there would be a solution still present from the old days of lower memory:
Passing cliboard data as file streams. That is creating a file stream (IStream), writing the bitmap to that, and pass the stream to the clipboard (IDataObject) which owns it and deletes the file when new data is put on the clipboard or requesting the clipboard to be emptied. Then the image is not stored in memory but in a (temporary) file.

But don't ask me how to do that with C#.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900