Proportional image resize
ratio = MIN( maxWidth / width, maxHeight/ height ); width = ratio * width; height = ratio * height; Make sure all divides are floating-point.
ratio = MIN( maxWidth / width, maxHeight/ height ); width = ratio * width; height = ratio * height; Make sure all divides are floating-point.
If you get your image directly from file, you can use the following code to get size of original file in bytes. var fileLength = new FileInfo(filePath).Length; If you get your image from other source, like getting one bitmap and composing it with other image, like adding watermark you will have to calculate size in … Read more
This method merge two images one in the top of the other you can modify the code to meet for your needs: public static Bitmap MergeTwoImages(Image firstImage, Image secondImage) { if (firstImage == null) { throw new ArgumentNullException(“firstImage”); } if (secondImage == null) { throw new ArgumentNullException(“secondImage”); } int outputImageWidth = firstImage.Width > secondImage.Width ? … Read more
Eventually it will be, using shaders. See the W3C Docs on Filters. At the moment, what is possible for instance is: -webkit-filter: grayscale; /*sepia, hue-rotate, invert….*/ -webkit-filter: brightness(50%); See David Walsh on CSS Filters Stackoverflow: apply a rose tint…: W3C Filter Effects 1.0 Docs – 38.2.5. Other uniform variables: the CSS shaders parameters Update: Adobe … Read more
The Canvas documentation explicitly does not specify a scaling method – in my own tests it did indeed anti-alias the image quite badly in Firefox. The code below copies pixel by pixel from a source image (which must be from the same Origin or from a Data URI) and scales it by the specified factor. … Read more
You can use “160×160#” which will scale and crop to exactly that size, which is unique to paperclip. Otherwise you can use any of the ImageMagick geometry strings, detailed here: ImageMagick Geometry But I’ll quote the one you’re interested in: “160×160!” Width and height emphatically given, original aspect ratio ignored.
BufferedImage in = ImageIO.read(img); BufferedImage newImage = new BufferedImage( in.getWidth(), in.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = newImage.createGraphics(); g.drawImage(in, 0, 0, null); g.dispose();
Find which is smaller: MaxWidth / w or MaxHeight / h Then multiply w and h by that number Explanation: You need to find the scaling factor which makes the image fit. To find the scaling factor, s, for the width, then s must be such that: s * w = MaxWidth. Therefore, the scaling … Read more
Before jsbueno posted his answer I had also tried asking on the #gimp IRC channel. I was directed to this thread on Gimptalk which contains the following code: gimp -n -i -b – <<EOF (let* ( (file’s (cadr (file-glob “*.xcf” 1))) (filename “”) (image 0) (layer 0) ) (while (pair? file’s) (set! image (car (gimp-file-load … Read more
PNG is not a lossy image format, so you would likely need to convert the image into another format– most likely JPEG. JPEG has a settable “quality” factor– you could simply keep reducing the quality factor until you got an image that was small enough. All of this can be done without changing the image … Read more