Proposal: Add MutateAsync() support. #2465
-
While Adding I would add this feature my self, externally, but I have a proposal PR ready at (https://github.com/SixLabors/ImageSharp/pull/2464/files) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Can you please provide a code example that demonstrates this? (How does upfront loading look like vs how would it look like with |
Beta Was this translation helpful? Give feedback.
-
Sure, two quick examples, with and without async mutate() Example of non async var layerImages = new List<Image>();
using var resultImage = new Image<Abgr32>(1024, 1024, new Abgr32());
try {
foreach( var model in assetLayers ) {
await using var stream = await _httpClient.GetStreamAsync(model.ImageUrl;);
layerImages.Add(await Image.LoadAsync(stream));
}
resultImage.Mutate(x => {
var graphicsOptions = new GraphicsOptions {
AlphaCompositionMode = PixelAlphaCompositionMode.SrcOver,
};
foreach( var layer in layerImages ) {
// MARK: Here would be a great spot to load each asset at a time,
// and merge it with the resultImage, then dispose of the asset
x.DrawImage(layer, new Point(0, 0), graphicsOptions);
}
});
}
finally {
// We obviously need to dispose all images after use
foreach( var x in layerImages ) {
x.Dispose();
}
} Same example, but with proposed using var resultImage = new Image<Abgr32>(1024, 1024, new Abgr32());
await resultImage.MutateAsync( async x => {
var graphicsOptions = new GraphicsOptions {
AlphaCompositionMode = PixelAlphaCompositionMode.SrcOver,
};
foreach( var model in assetLayers ) {
await using var stream = await _httpClient.GetStreamAsync(assetModel.ImageUrl;);
using var layer = await Image.LoadAsync(stream);
x.DrawImage(layer, new Point(0, 0), graphicsOptions);
}
}); |
Beta Was this translation helpful? Give feedback.
could you not just use this pattern instead? just call
Mutate(..)
multiple times.