0

I'm using Tim Jones's fork of the DynamicImage library on a background task in C#.

dynamic-image

Everything works great, with the exception of isolating the layer effects. In the example below the filters from "firstLayer" are also applied to "secondLayer". I need the second layer to be on top but only anchored to the bottom. Both images are PNGs, only "secondLayer" has a transparency. How can I ensure that filters do not affect other layers?

Composition composition = new Composition();


//Create Layer 1 --------------
var firstLayer = LayerBuilder.Image.SourceBytes(firstImageBytes);

//Rotate The Image
firstLayer.WithFilter(FilterBuilder.Rotate.To(180));

//Crop the image
firstLayer.WithFilter(FilterBuilder.Crop.X(50).Y(50));


//Create Layer 2 -----------
var secondLayer = LayerBuilder.Image.SourceBytes(secondImageBytes);


//Add both layers to the composition ---------------
composition.Layers.Add(firstLayer.ToLayer());
composition.Layers.Add(secondLayer.ToLayer());


// Anchor image 2 to the bottom
composition.Layers[1].Anchor = AnchorStyles.BottomCenter;

//Generate The Image
GeneratedImage generatedImage = composition.GenerateImage();


//I then convert the generatedImage to a byte[] and save it to Blob storage.

I've tried many variations on the above code and the filters seem to always affect layers they are not applied to. I am certain that I am missing an important concept. Any thoughts or suggestions?

INNVTV
  • 3,155
  • 7
  • 37
  • 71
  • Just as a side note: It seems that filters such as Tint & Blur stay within their respective layers. Crop, Rotation & Resize seem to either bleed across multiple layers, or create unwanted effects on other layers. – INNVTV Sep 11 '14 at 00:24
  • If filters do affect layers they are not applied to, then that would indeed be a big bug. So far I haven't been able to reproduce it. I've tried something very similar to your code snippet. Can you upload the incorrect generated image? So far I don't really understand what the problem is. – Tim Jones Sep 11 '14 at 15:12
  • Incidentally, I think this line: `composition.Layers[1].Anchor = AnchorStyles.BottomCenter;` will crash, because no layers have been added to the composition at that point. You could just call the fluent `.Anchor(AnchorStyles.BottomCenter)` method on the layer builder directly. – Tim Jones Sep 11 '14 at 15:14
  • I think it has to do with both images being at different sizes. Here is a Zip containing approximations of both images: http://1drv.ms/1pTYQzk – INNVTV Sep 11 '14 at 20:19
  • I updated the anchor styling so that it happens after the layer is added to the composition. This was an error in my example code, and not in the actual source. – INNVTV Sep 11 '14 at 20:20
  • So it seems that layer2 gets stretched to fit the size of layer1. It may not be affected by the filters at all. I guess I need to manage the Layer or Composition sizes in some way to maintain the integrity and aspect ratio of each image/layer? I'm going to dig into this further, but is there is a proper way to manage this I have not found it in any of the docs. Thank you in advance Tim! – INNVTV Sep 11 '14 at 20:23
  • Again, I still can't find a problem. Using your two layer images, I get exactly the image I would expect: http://postimg.org/image/3puvh9v4b/f40c54bd/ What looks wrong to you here? – Tim Jones Sep 12 '14 at 15:18
  • To explain more: the first layer is being rotated by 180 degrees and then cropped (since you've only specified the X and Y, the crop Width and Height will default to 200). Then the second layer is anchored to the BottomCenter. Anchoring implies that the size of the layer being anchored doesn't participate in determining the size of the overall image. So the second layer is essentially anchored to the bottom center of the first layer (after the first layer's filters have been applied). – Tim Jones Sep 12 '14 at 15:22
  • Thanks for looking at this. I'll have another go at it in a few hours and update this thread with my results. – INNVTV Sep 12 '14 at 15:46
  • Thanks Tim, I was able to see how this example does indeed work as you stated. I am however getting some inconsistent behavior when I reverse the order that I add layers to my compositions and apply certain filters in certain ways. This may be due to the fact that I am using transparent PNGs or that I am missing an important concept in how this library works. I reached out to you at your email address to see about setting up a paid screen share. I could then update this thread with my findings. – INNVTV Sep 12 '14 at 22:00

0 Answers0