Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 686929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:04:29+00:00 2026-05-14T02:04:29+00:00

I’m having a problem with image scaling. When I use the following code to

  • 0

I’m having a problem with image scaling. When I use the following code to scale an image it ends up with a line either at the bottom or on the right side of the image.

double scale = 1;
if (scaleHeight >= scaleWidth) {
    scale = scaleWidth;
} else {
    scale = scaleHeight;
}
AffineTransform af = new AffineTransform();
af.scale(scale, scale);

AffineTransformOp operation = new AffineTransformOp(af, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage bufferedThumb = operation.filter(img, null);

The original image

enter image description here

The scaled image

enter image description here

Does anyone know why the line appears?

Thanks!

EDIT:

Added the complete method code:

public static final int SPINNER_MAX_WIDTH = 105;
public static final int SPINNER_MAX_HEIGHT = 70;

public void scaleImage(BufferedImage img, int maxWidth, int maxHeight, String fileName) {
    double scaleWidth = 1;
    double scaleHeight = 1;

    if (maxHeight != NOT_SET) {
        if (img.getHeight() > maxHeight) {
            scaleHeight = (double) maxHeight / (double) img.getHeight();
        }
    }

    if (maxWidth != NOT_SET) {
        if (img.getWidth() > maxWidth) {
            scaleWidth = (double) maxWidth / (double) img.getWidth();
        }
    }

    double scale = 1;

    if (scaleHeight >= scaleWidth) {
        scale = scaleWidth;
    } else {
        scale = scaleHeight;
    }

    AffineTransform af = new AffineTransform();
    af.scale(scale, scale);

    AffineTransformOp operation = new AffineTransformOp(af, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    BufferedImage bufferedThumb = operation.filter(img, null);

    if (bufferedThumb != null) {
        File imageFile = new File(fileName);
        String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
        try {
            ImageIO.write(bufferedThumb, fileType, imageFile);
        } catch (IOException e) {
            logger.error("Failed to save scaled image: " + fileName + "\n" + e.getMessage());
        }
    }
}

The maxWidth and maxHeight parameters in the method call is set to the SPINNER_MAX_* constants.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:04:29+00:00Added an answer on May 14, 2026 at 2:04 am

    Can you give us the rest of the code – how you manipulate the bufferedThumb, because if you just save it to to a file it should be fine.

    ImageIO.write(bufferedThumb, "PNG", new File("img.png"));
    

    What java version do you use?

    EDITED:

    What you can try is to construct the final image explicitly like this:

    BufferedImage bufferedThumb = new BufferedImage(maxWidth, maxHeight, BufferedImage.TYPE_INT_ARGB);
    operation.filter(img, bufferedThumb);
    

    to make sure what color mode is being used.

    I think your issue may be related to this bug:
    https://bugs.java.com/bugdatabase/view_bug?bug_id=6725106

    Another thing is to maybe use a differnt interpolation type like:

    AffineTransformOp.TYPE_BILINEAR
    

    For more information have a look at:
    http://www.dpreview.com/learn/?/key=interpolation

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 391k
  • Answers 391k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer BOOL CALLBACK EnumChildProc ( HWND hwndChild, LPARAM lParam) { //… May 15, 2026 at 1:17 am
  • Editorial Team
    Editorial Team added an answer Why not have a dev server that each dev connects… May 15, 2026 at 1:17 am
  • Editorial Team
    Editorial Team added an answer You have to assign an eventhandler on the datagrid, and… May 15, 2026 at 1:17 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.