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

Add border to image

Sep 20, 2013 at 9:48 PM
What is the proper technique to add a border to an image. I've wrestled with this for days, and no matter what I try, MagikNet messes with my image. I end up with it either skewed, distorted, converted to CMYK, or some other funky conversion.

My situation is this:
A user uploads a picture to my website, but it must be certain dimensions (to fit in a frame).
If they upload one that is not right, I simply want to add white border, then center the picture within the white border. The border require vertical, horizontal, or both centering. This would seem it should be very easy to do. But no matter what I do, the resulting image is garbled in some way.

Right now I have it close. But it keeps converting it to CMYK. Then if I convert it back to RGB, the colors get hosed up.

I would post my code here, but the efforts I have gone through to make this work have become comical, and the code is ridiculous. But the 30 second version, is that I create a white image of the correct size, and drop my uploaded image into it with a 'composite' function. That should work, but the colors always get messed up when I do it.

I would rather have someone just say 'here is how you add a white border, and center the image within the border'.
Sep 20, 2013 at 11:34 PM
You could try to following:
using (MagickImage image = new MagickImage(@"input.png"))
  image.Crop(100, 100, Gravity.Center);
  image.BorderColor = Color.Purple;
Why do you need the CMYK conversion?
Sep 21, 2013 at 3:20 AM
Damn, I'd bet my bottom dollar I had done that, and it skewed the image. But I just tried it and it did do exactly as advertised. I had done that as my first attempt, and it skewed (at least something skewed) so I got to thinking that the border command did not work correctly. Unfortunately I don't have that code anymore, so I don't know why it skewed on me originally. But it does work properly. Thanks for your help.