Magick.net combine 2 images with a blur background

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Magick.net combine 2 images with a blur background

Post by stealthrt »

Hey all I have this image here that's 489x508:

Image

and the box where I need to put that image is sized 400x300:

Image

So I resize the image to 0, 300 so the height matches the box height (which is not correct):

Image

Then I try to add the second image on top of that one (again, did not come out correct):

Image

But what I am wanting to do is fill in the exposed background area(s) that are just a plan color (which is white in this example) with the original image with it blurred like so:

Image

What would be the code below need modified in order to do that using the Magick.net C#?

My code:

Code: Select all

    stream = getPic.OpenRead(new Uri(@"C:\Users\David\Pictures\YnTf9.png"));
    Bitmap bitmap1 = new Bitmap(stream);
    stream = getPic.OpenRead(new Uri(@"C:\Users\David\Pictures\YnTf9.png"));
    Bitmap bitmap2 = new Bitmap(stream);
    
    using (MagickImageCollection images = new MagickImageCollection())
    {
         // Add the first image
         MagickImage first = new MagickImage(bitmap1);
         first.Crop(400, 300);
         first.Blur(0, 10);
         images.Add(first);
    
         // Add the second image
         MagickImage second = new MagickImage(bitmap2);
         second.Crop(0, 300, Gravity.Center);
         images.Add(second);
    
         // Create a mosaic from both images
         using (IMagickImage result = images.Mosaic())
         {
             // Save the result
             result.Resize(400, 300);
             result.Write(@"C:\Users\David\Pictures\TEST.png");
         }
    }
    
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Magick.net combine 2 images with a blur background

Post by fmw42 »

In command line, I would do the following:


# line 1: read the input
# line 2: copy the input, blur and crop the middle to 400x300
# line 3: copy the input, and resize to 300x300
# line 4: delete the input
# line 5: composite the resized image over the blurred and cropped image in the middle
# line 6: write the output

Code: Select all

convert YnTf9.png \
\( -clone 0 -blur 0x5 -gravity center -crop 400x300+0+0 +repage \) \
\( -clone 0 -resize 300x300 \) \
-delete 0 \
-gravity center -compose over -composite \
result.png
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Re: Magick.net combine 2 images with a blur background

Post by stealthrt »

Fred: thanks for that but why are you resizing it to 300x300? I would not know what to resize it too in my original code so that’s one thing I won’t be able to do automatically like I am looking to do? The height is the only thing I will know to do and that’s why I used 0, 300 in my code.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Magick.net combine 2 images with a blur background

Post by fmw42 »

I resized it that way, so it looked like your result. Perhaps you can explain better what you want to do.

This does the same for this image. Perhaps this is what you want. It resizes only according to the height and does the width so as to preserve aspect ratio.

Code: Select all

convert YnTf9.png \
\( -clone 0 -blur 0x5 -gravity center -crop 400x300+0+0 +repage \) \
\( -clone 0 -resize x300 \) \
-delete 0 \
-gravity center -compose over -composite \
result.png
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Re: Magick.net combine 2 images with a blur background

Post by stealthrt »

Yes your last post is exactly how i needed it! Thanks!

Now to translate this command line into C#...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Magick.net combine 2 images with a blur background

Post by fmw42 »

Post Reply