Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What WPF image type should I use to perform multiple transformations

I need to perform multiple operations on an Image, for example, I need to resize the image, perhaps pad it to maintain aspect ratio (and draw a background color), and conditionally stamp with a watermark.

I'm currently using BitmapFrame as the type that I pass between the methods involved.

Can anyone recommend another type which I should use to perform incremental updates on images?

I could possibly create a composition of various images, although I'm not sure which types I should use for this?

like image 954
Khanzor Avatar asked Jun 11 '12 04:06

Khanzor


2 Answers

WriteableBitmap is suitable when you want to make incremental updates of the image. Both BitmapFrame and WriteableBitmap inherit from BitmapSource, and WriteableBitmap can be instantiated using any BitmapSource.

You might also want to have a look at the WriteableBitmapEx library that provides a wealth of efficient WriteableBitmap extension methods for bitmap manipulation. This library is applicable to WPF applications as well as Silverlight, WP7 and Metro.

like image 156
Anders Gustafsson Avatar answered Nov 06 '22 01:11

Anders Gustafsson


Try WriteableBitmap

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap.aspx

Use the WriteableBitmap class to update and render a bitmap on a per-frame basis. This is useful for generating algorithmic content, such as a fractal image, and for data visualization, such as a music visualizer.

For greater control over updates, and for multi-threaded access to the back buffer, use the following workflow.

1. Call the Lock method to reserve the back buffer for updates.

2. Obtain a pointer to the back buffer by accessing the BackBuffer property.

3. Write changes to the back buffer. Other threads may write changes to the back buffer when the WriteableBitmap is locked.

4. Call the AddDirtyRect method to indicate areas that have changed.

5. Call the Unlock method to release the back buffer and allow presentation to the screen.

When updates are sent to the rendering thread, the rendering thread copies the changed rectangles from the back buffer to the front buffer. The rendering system controls this exchange to avoid deadlocks and redraw artifacts, such as "tearing".

like image 36
Sumeet Jindal Avatar answered Nov 06 '22 00:11

Sumeet Jindal