microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.27k stars 674 forks source link

How to save ImageSource or BitmapImage to stream? #5777

Closed alexdi220 closed 1 year ago

alexdi220 commented 3 years ago

Hi, what is the best way to do it? I have a model with the property of ImageSource and need to convert it to stream (or System.Drawing.Image).

codendone commented 3 years ago

This is not supported scenario.

What are you trying to achieve?

alexdi220 commented 3 years ago

Hi, @codendone. I'm a little surprised. I have a ViewModel with the property of type ImageSource. I have no source of the image. It can be any - file, byte[], DB and etc. So I want to process that property, save\copy it, or convert it to System.Drawing.Image for internal logic. For WPF it's a base scenario.

codendone commented 3 years ago

You could potentially use RenderTargetBitmap to snapshot an element containing the target ImageSource, but that would only snapshot the pixels being rendered for the element at its current size.

Something higher resolution would require you to separately hold on to the Uri/Stream/whatever you have as the source.

alexdi220 commented 3 years ago

Thank you @codendone. Do you have any plans to support it in the future? Or it's a new approach to work with images when you need to have access to the source of the image (instead of WPF where you can transform ImageSource to a stream)?

codendone commented 3 years ago

This scenario is not our backlog, and I'm not aware of previous requests for it. But if you think this is important support we should add, such as if you think we should specifically mirror WPF's BitmapSource.CopyPixels() APIs, please log a Proposal which can be used for voting and tracking.

alexdi220 commented 3 years ago

While you answered, I've thought over how WinUI processes images. The main is that the ImageSource is only a container of the image that be displayed via Image or ImageBrush. The developer can't manage this process - loading, rendering. So if you need to work with images you need a real source - file, link, DB. Am I right? That why it's surprised me. In WPF the ImageSource is a base abstraction to work with Image, model\viewmodel\components, all worked with ImageSource. I don't know what is better, but the guess is that WPF developers are the main consumers of the WinUI desktop platform. Proposal https://github.com/microsoft/microsoft-ui-xaml/issues/5851

codendone commented 3 years ago

I think ImageSource in WinUI isn't significantly different from WPF, it is just more limited in scope due to the lack of CopyPixels() and other classes/functions from WPF which enable conversion and manipulation. In WinUI ImageSource is a simple render-only concept closer to a SolidColorBrush: you can provide the "color" via a Uri or stream and then you put it where you want it to render.

The SoftwareBitmap type has been recommended for some image manipulations, and then can be used via SoftwareBitmapSource. But I think this is not the scenario you're interested in.

Thanks for logging the proposal!

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.