0

While trying to scale the image drawn on a canvas, I came across Graphics2D.drawImage(originalImage) [Graphics2D object obtained from BufferedImage object].

I used it to draw the image into the graphics created from BufferedImage, this new image could be now be drawn onto a Graphics object of the Panel/Frame to get a zoomed image.

I populated the original image using BufferedImage.setRGB.

So, what is it actually doing? Is it selectively omitting the pixels from the original image?


Similar to this code.

int newImageWidth = imageWidth * zoomLevel;
int newImageHeight = imageHeight * zoomLevel;
BufferedImage resizedImage = new BufferedImage(newImageWidth , newImageHeight, imageType);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, newImageWidth , newImageHeight , null);
g.dispose();

Original Answer for the above code

Tarun Maganti
  • 3,076
  • 2
  • 35
  • 64
  • It's not omitting anything. drawImage() is geared towards copying the pixels of an existing image (and not necessarily loaded as a BufferedImage) and also allows scaling as it is a commonly needed feature. setRGB() just copies the pixels out of an array, without genericity and without a scaling option. setRGB() also forces the source pixels to be RGB and thus the destination image to perform relevant conversion if it's not RGB. drawImage() may be using images of the same color model. – kumesana Mar 12 '19 at 06:54
  • What if I write a smaller image into that scaled BufferedImage graphics2D object same image? – Tarun Maganti Mar 12 '19 at 09:22
  • By default images are drawn at their own size, measured in pixels. If you require them drawn upscaled, the renderer will perform an interpolation and upscale. (Also, you could just try instead of asking) – kumesana Mar 12 '19 at 10:39
  • You should find all your answers in the Java 2D tutorial, see [Java 2D Rendering](https://docs.oracle.com/javase/tutorial/2d/overview/rendering.html) and [Working with Images](https://docs.oracle.com/javase/tutorial/2d/images/index.html) especially. – Harald K Mar 12 '19 at 14:30

0 Answers0