Image Resize For Larger Images, But Padding For Smaller Images
I need to find a .Net solution for ASP.Net where images larger than a given size are scaled down, b开发者_开发百科ut images smaller will be centered in the newer sized image on a white background. Any thoughts or help is appreciated.
ImageResizer supports this:
<img src="image.jpg?width=500&height=300&mode=pad&scale=canvas" />
You can also adjust the background color with &bgcolor=color|hex
, or use &format=png
to get it in transparency instead.
ImageMagick (available as Free and Open Source Software) can easily do what you're describing with command-line options. It sounds like you want a .NET library that will accomplish the same thing. I don't know any off the top of my head, but I bet if you Google for .NET image manipulation libraries, you will find some, and then you will be able to accomplish what you need to.
The second example (Example #2 - Scale to a fixed size) in this post http://www.codeproject.com/Articles/2941/Resizing-a-Photographic-image-with-GDI-for-NET show how to do what you (and me) need with .net System.Drawing.
I found also the command to achieve the result with ImageMagick:
convert input.png -resize 150x150 -background white -compose Copy -gravity center -extent 150x150 -quality 92 output3.png
but I found that ImageMagickNet, the wrapper of ImageMagick for .Net, is not updated and there is no documentation about some commands; I discover how to resize and extent an image, but I can't find how to set background color, gravity and other configurations. CodePlex page has some discussion but very few answers (http://imagemagick.codeplex.com/)
ImageMagickNET.MagickNet.InitializeMagick();
ImageMagickNET.Image img = new ImageMagickNET.Image("c:\\images\\input.png");
ImageMagickNET.Geometry geometry = new ImageMagickNET.Geometry("150x150");
img.Resize(geometry);
img.Compose(CompositeOperator.CopyCompositeOp);
img.Extent(geometry);
img.Write("c:\\images\output.png");
There are some trick to make run ImageMagickNET, but you can find them easily on codeplex or stackoverflow. So my advice is: go with System.Drawing!
精彩评论