Crop Image to a bounding box

I am having some issues with making transparent background in an image. My goal is to let uses define a bounding area (in our web application) and then insert an image into a new document bounded by the predefined area. They can scale the image, but only image data within this bounding box will be added to the document.

How I have done this in testing is to load the image, resize it to whatever size they scaled it to, then crop the image to the bounding area. Since they can leave empty space within the area, I then create a new image of the size of the bounding area and draw the cropped image into the new image first clearing the background with Color.Transparent.

Here is the code I am using to do this.

const string filePath = @"resizeSource.jpg"
var img = Image.Load(filePath);
img.ResizeWidthProportionally(161, ResizeType.HighQualityResample);


var raster = img as RasterImage;
raster.Crop(new Rectangle(2, 2, 96, 54));

var newImg = new PngImage(99, 54);
var graphics = new Graphics(newImg);
graphics.Clear(Color.Transparent);
graphics.DrawImage(img, 2, 0, 97, 54);

var options = new PngOptions { ResolutionSettings = new ResolutionSetting(300, 300) };
newImg.Save("test.png", options);

What I am getting, is the properly resized image but there is a black background on the area where it should be transparent. I have tested changing the color to white and it works with white (but its not transparent, its white).resizeSource.jpg (45.0 KB)

Using Aspose.Imaging version=“18.2.0” with Total License.

Not sure what I am doing wrong. Thanks.

@cyrus10101,

I have observed your comments. Can you please share desired and generated result along with environment details so that we can further investigate to help you out.

That is using C# so the .Net Aspose.Imaging nuget package version 18.2.0.

This is the output that it creates
test.png (12.3 KB)

On the left side there is 2px of a black background that shouldnt be there… it should have 2 px of transparent background. The same result occurs if you adjust the image size of newImg (in the above code) to be larger, it would have a black background on the area that does not have image data. The desired result is to have a transparent background in the areas where no image is drawn rather than a black background.

This is another example if you make the canvas larger, much easier to see the black background surrounding the cropped image.

increasedSizeCanvas.png (13.0 KB)

Something like this is the desired (not to scale, i made this manually in an imaging program)

desired.png (14.2 KB)

@cyrus10101,

I suggest you to please try using following sample code on your end.

public static void TestResizeAndTransparent()
{
    String path=@"C:\Imaging Data\TestTransparent\";
    string filePath = path + "resizeSource.jpg";
    var img = Image.Load(filePath);
    img.ResizeWidthProportionally(161, ResizeType.HighQualityResample);


    var raster = img as RasterImage;
    raster.Crop(new Rectangle(2, 2, 96, 54));

    var newImg = new PngImage(99, 54);
    var graphics = new Graphics(newImg);
    graphics.Clear(Color.Transparent);
    graphics.DrawImage(img, 2, 0, 97, 54);

    var options = new PngOptions { ResolutionSettings = new ResolutionSetting(300, 300) };

    Aspose.Imaging.ImageOptions.PngOptions OptionsIM = new Aspose.Imaging.ImageOptions.PngOptions();
    OptionsIM.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;
    //OptionsIM.TransparentColor = new Aspose.Imaging.TransparentColorSetting(Aspose.Imaging.Color.Transparent);
    Aspose.Imaging.ResolutionSetting oResIM = new Aspose.Imaging.ResolutionSetting(Convert.ToDouble(300), Convert.ToDouble(300));
    OptionsIM.ResolutionSettings = oResIM;

    newImg.Save(path + "Savedtest.png", OptionsIM);
}

I have obtained following output on my end using above code.
Savedtest.jpg (5.3 KB)

Ah very nice, those pngoptions did the trick. Thanks.