スキップしてメイン コンテンツに移動

投稿

ラベル(Resize)が付いた投稿を表示しています

Java: Auto Resize Image Canvas (Swing)

I wrote an image canvas class which can be automatically re-size image so that it fits the window. The code itself is pretty simple. package com.dukesoftware.utils.swing.others; import java.awt.Graphics; import java.awt.Image; import javax.swing.JPanel; public class AutoResizeImageCanvas extends JPanel{ private Image img; public void setImage(Image img){ this.img = img; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); final int panelWidth = getWidth(); final int panelHeight = getHeight(); g.fillRect(0, 0, panelWidth, panelHeight); if(img != null){ final int imgWidth = img.getWidth(null); final int imgHeight = img.getHeight(null); final double rW = (double)panelWidth / imgWidth; final double rH = (double)panelHeight / imgHeight; int newWidth; int newHeight; if(rW < rH){

ActionScript 3.0: Resize Image

Main flow part code. package utils.tool { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Loader; import flash.net.URLRequest; import flash.events.Event; import utils.ImageUtils; public class ImageResizer { private var maxW:int, maxH:int; private var smoothing:Boolean; private var saveFunction:Function; public function ImageResizer(maxW:int, maxH:int, smoothing:Boolean = false, type:String="jpg") { this.maxW = maxW; this.maxH = maxH; this.smoothing = smoothing; if (type === "png") { saveFunction = ImageUtils.saveBitmapDataAsPNGAsync; } else if(type === "jpg"){ saveFunction = ImageUtils.saveBitmapDataAsJPEGAsync; } else { throw new Error("Not Supported"); } }

C#: Resize Image

// Default parameters are almost the highest quality setting. public static Bitmap Resize(this Image src, int w, int h, SmoothingMode smoothingMode = SmoothingMode.AntiAlias, InterpolationMode interpolationMode = InterpolationMode.HighQualityBicubic, PixelOffsetMode pixelOffsetMode = PixelOffsetMode.HighQuality) { var newImage = new Bitmap(w, h); using (var gr = Graphics.FromImage(newImage)) { gr.SmoothingMode = smoothingMode; gr.InterpolationMode = interpolationMode; gr.PixelOffsetMode = pixelOffsetMode; gr.DrawImage(src, 0, 0, w, h); } return newImage; } // resize image based on given percentage public static Bitmap ResizedByPercentage(this Image src, double percent) { var rW = (int)Math.Round(src.Width * percent, 0); var rH = (int)Math.Round(src.Height * percent, 0); return src.Resize(rW, rH); } // resize image limited in given w, h parameter public static Bitmap ResizeImageIn(this Image src, int w, i

Java: Resize Image Using Graphics2D

Here is a code for resizing image using Graphics2D. I also put bunch of rendering hints in the example code. Hope it helps for your understanding. private static BufferedImage resize(BufferedImage image, int width, int height) { BufferedImage shrinkImage = new BufferedImage(width,height, image.getType()); Graphics2D g2d = shrinkImage.createGraphics(); // rendering hints g2d.setRenderingHint(KEY_ALPHA_INTERPOLATION, VALUE_ALPHA_INTERPOLATION_QUALITY); g2d.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON); g2d.setRenderingHint(KEY_COLOR_RENDERING, VALUE_COLOR_RENDER_QUALITY); g2d.setRenderingHint(KEY_DITHERING, VALUE_DITHER_ENABLE); g2d.setRenderingHint(KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_ON); g2d.setRenderingHint(KEY_RENDERING, VALUE_RENDER_QUALITY); g2d.setRenderingHint(KEY_INTERPOLATION, VALUE_INTERPOLATION_BILINEAR); g2d.setRenderingHint(KEY_FRACTIONALMETRICS, VALUE_FRACTIONALME