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 8981399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:19:34+00:00 2026-06-15T20:19:34+00:00

I am trying to resize an Image, the save it as a BufferedImage. If

  • 0

I am trying to resize an Image, the save it as a BufferedImage. If I do not scale the image, I works fine.

With the following code, a filename is passed in and converted to a BufferedImage this works fine Using g.drawImage(img, x, y, null); where img is the BufferedImage

public Sprite(String filename){
    ImageIcon imgIcon = new ImageIcon(filename);
    int width = imgIcon.getIconWidth();
    int height = imgIcon.getIconHeight();
    BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics bg = bimg.getGraphics();
    bg.drawImage(imgIcon.getImage(), 0, 0, null);
    bg.dispose();
    this.sprite = bimg;
}

The following method here does not work, it takes a filename, and a resize width. It resizes it, then converts it to a BufferedImage, but it does not work using g.drawImage(img, x, y, null); again where img is the BufferedImage.

public Sprite(String filename, int width){
    ImageIcon imgIcon = new ImageIcon(filename);
    Image img = imgIcon.getImage();
    float h = (float)img.getHeight(null);
    float w = (float)img.getWidth(null);
    int height = (int)(h * (width / w));
    Image imgScaled = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);

    BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics bg = bimg.getGraphics();
    bg.drawImage(imgScaled, 0, 0, null);
    bg.dispose();
    this.sprite = bimg;
}

So my question is, why isn’t the second block working?

  • 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-06-15T20:19:36+00:00Added an answer on June 15, 2026 at 8:19 pm

    You have a rounding issue…

    Java will return result of a division based on the values you provide it…

    For example…

    int width = 100;
    int w = 5;
    int result = width / w
    // result = 0, but it should be 0.5
    

    Java has done an internal conversion, converting the value back to an int, which simply truncates the decimal values.

    Instead, you need to encourage Java to return the result as decimal value…

    int result = width / (float)w
    // result = 0.5
    

    So, you scale calculation int height = (int)(h * (width / w)) is actually returning 0

    I would use a calculation more along the lines of

    int height = Math.round((h * (width / (float)w)))
    

    I’m sorry, I don’t quite remember the “technical” babble for all of this, but this is the general jest of the idea 😉

    Updated

    ImageIcon uses a background thread to actually load the image pixels, but returns immediately after you call the constructor. This means that the image data may not be available for some time into the future.

    Use ImageIO.read(new File(filename)) instead. This will block until the image data has been read and will return a BufferedImage, which is significantly easier to deal with.

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

Sidebar

Related Questions

Using this code I am trying to resize the selected image and then want
I am trying to resize an image in wordpress using this code if (
Hello everyone I am trying to resize an image of 700kb with imagecreatefromjpeg. This
I trying to resize one image from InputStream, so I use the code in
I'm trying to resize an image using stretchableImageWithLeftCapWidth: it works on the simulator, but
Possible Duplicate: resize image on save Am trying to create a thumbnail in django,
Trying to use the following scripts to upload, resize & save multiple images via
I'm trying to resize an image uploaded through an MVC3 app. The code below
I'm trying to grab an image from a remote location, resize it and save
I'm trying to resize an image at runtime in yii, but does not work.

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.