This project has moved. For the latest updates, please go here.

What happens with Copyopacity in version 7

Jul 22, 2016 at 7:41 AM
I am using Magick.net to make some changes on user pictures. To Cut out a specific part of the picture I used composite with the compositoperator.Copyopacity it have been working perfectly. Then I upgrade to version 7 and now I have to user other compositoperators I found that the DstIn is something like copyOpacity and is doing my job. But the problem is that when I apply that command the pixels that are in the mask picture (the auxiliary picture which is black with some transparent area, the transparent are the area that I wanted to cut out) and are not 100% black transfers to the final picture so I have a gray and ghostly lines on the final picture. I thought maybe I have to edit the mask picture and remove all the pixels that are not 100 percent black but the problem is that I have to resize the mask in relation to the user uploaded files.
my questions :
How can cut out a part of the user picture and have a picture as my result that has the cut out part and the rest is transparent ?
the technology and framework that I am using: .Net Mvc c# and Magick.net version 7
Coordinator
Jul 22, 2016 at 8:21 AM
Edited Jul 22, 2016 at 8:21 AM
CopyOpacity has been renamed to CopyAlpha. Can you give that a try?
Jul 22, 2016 at 9:25 AM
Thanks for you quick reply.

I tested CopyAlpha and it is not like copy opacity. CopyAlpha use the white color pixels of the auxilary image to select and cut out the pixels in user picture.

I wrote a code and apply all compositeoperators and then save each result in Hard drive (I uploaded all the result in the folder below) but none of the compositeoperator gave me the same result as copyopacity in version 6. However you can see in the folder of allresult that the white box in copyalpha gave me the required result.

you can see the pictures and all results here
https://drive.google.com/drive/folders/0B_CDxTDJCrVCeU1rQmZyOXdiR00


if I change all the transparent area of all of my masks and replace copyopacity with CopyAlpha the problem will fix but is there anyway to prevent this big change ?
Jul 22, 2016 at 9:28 AM
As you can see test_DstOut is exactly what I need but if you zoom in you can see there is a gray, and ghostly border around the cut area. Is there any way to prevent that ?
Coordinator
Jul 22, 2016 at 10:05 AM
Can you also create a really simple class that I can use to reproduce your results with the pictures you provided? I would prefer it if you would put the source file together with your pictures. Then I will be able to reproduce it much easier.
Jul 22, 2016 at 10:11 AM
this is the class that I run to get all the results.
please do not forget to change the path of the two file to your local drive


class Class1
{
    public void ConvertPictures()
    {
        var cropImagePath = @"d:\temp\BlackWithSquare.png";
        var userSamplePicturePath = @"d:\temp\userSample.jpg";

        using (var userSamplePicture = new MagickImage(userSamplePicturePath))
        {
            using (var blackImage = new MagickImage(cropImagePath))
            {
                blackImage.Resize(userSamplePicture.Width, userSamplePicture.Height);
                foreach (CompositeOperator op in Enum.GetValues(typeof(CompositeOperator)))
                {
                    try
                    {
                        using (var temp = new MagickImage(userSamplePicture))
                        {
                            temp.Composite(blackImage, Gravity.Center, op);
                            temp.Write(@"d:\temp\test_" + op + ".png");
                        }
                    }
                    catch (Exception e)
                    {
                        continue;
                    }
                }
            }
        }
    }

}
Coordinator
Jul 23, 2016 at 1:57 PM
If I understand you corectly you want to copy the alpha channel from one image to the other image. I have just published Magick.NET 7.0.2.600 that has a new overload in CopyPixels that allows you to copy only the alpha channel. I think this will get you the result that you want:
public void ConvertPictures()
{
  var cropImagePath = @"d:\temp\BlackWithSquare.png";
  var userSamplePicturePath = @"d:\temp\userSample.png";

  using (var userSamplePicture = new MagickImage(userSamplePicturePath))
  {
    using (var blackImage = new MagickImage(cropImagePath))
    {
      blackImage.Resize(userSamplePicture.Width, userSamplePicture.Height);
      userSamplePicture.CopyPixels(blackImage, Channels.Alpha);
      userSamplePicture.Write(@"d:\temp\test.png");
    }
  }
}