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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:01:37+00:00 2026-06-14T03:01:37+00:00

i have one problem when i convert image to byte[] and reverse: I have

  • 0

i have one problem when i convert image to byte[] and reverse:

I have 2 function convert image to byte[] as follow

public byte[] extractBytes2 (String ImageName) throws IOException {
    File imgPath = new File(ImageName);
    BufferedImage bufferedImage = ImageIO.read(imgPath);
    WritableRaster raster = bufferedImage .getRaster();
    DataBufferByte data   = (DataBufferByte) raster.getDataBuffer();    
    return ( data.getData() );
}

and

public byte[] extractBytes (String ImageName) throws IOException 
{
    Path path = Paths.get(ImageName);
    byte[] data = Files.readAllBytes(path);
    return data;
}

I will have byte[] byteArray

byteArray = extractBytes2("image/pikachu.png");

or

byteArray = extractBytes("image/pikachu.png");

when i convert byte[] to Image i use

Graphics g = panelMain.getGraphics();
    Graphics2D g2D = (Graphics2D) g;
    try {
        InputStream in = new ByteArrayInputStream(byteArray);
        BufferedImage image = ImageIO.read(in);
        g2D.drawImage(image, 0, 0, GiaoDienChinh.this);
        g2D.setPaint(Color.BLACK);
        panelMain.setOpaque(true);
        panelMain.paintComponents(g2D);
    }
    catch ( Exception e ) {           
    }
    finally {       
    }       

but i only draw with byteArray use function “extractBytes” not with “extractBytes2” !!!

Anyone can explain me how i can draw image with byteArray which got from “extractByte2”?

Thanks for all support!

  • 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-14T03:01:39+00:00Added an answer on June 14, 2026 at 3:01 am

    Let’s start with the paint code.

    ImageIO.read(in) is expecting a valid image format that one of it’s pluggable service provides knows how to read and convert to a BufferedImage.

    When you pass the byes from extractBytes, you’re simply passing back an array of bytes that represents the actual image file. I’d be the same as saying Image.read(new File("image/pikachu.png"))

    However, the data buffer returned from your extractBytes2 is returning a internal representation of the image data, which may not be “readable” by ImageIO.

    UPDATED

    A BufferedImage is an accessible buffer of image data, essentially
    pixels, and their RGB colors. The BufferedImage provides a powerful
    way to manipulate the Image data. A BufferedImage object is made up of
    two parts a ColorModel object and a Raster object.

    enter image description here

    Referenced from here

    UPDATED

    I had this wacky idea on the way home of how to convert a BufferedImage to a byte array…

    The basic idea is to use ImageIO.write to write out the BufferedImage to a ByteOutputStream…

    public static byte[] extractBytes2(String ImageName) throws IOException {
        File imgPath = new File(ImageName);
        BufferedImage bufferedImage = ImageIO.read(imgPath);
    
        ByteOutputStream bos = null;
        try {
            bos = new ByteOutputStream();
            ImageIO.write(bufferedImage, "png", bos);
        } finally {
            try {
                bos.close();
            } catch (Exception e) {
            }
        }
    
        return bos == null ? null : bos.getBytes();
    
    }
    

    Here’s my test…

    enter image description here

    public class TestByteImage {
    
        public static void main(String[] args) {
            new TestByteImage();
        }
    
        public static byte[] extractBytes2(String ImageName) throws IOException {
            File imgPath = new File(ImageName);
            BufferedImage bufferedImage = ImageIO.read(imgPath);
    
            ByteOutputStream bos = null;
            try {
                bos = new ByteOutputStream();
                ImageIO.write(bufferedImage, "png", bos);
            } finally {
                try {
                    bos.close();
                } catch (Exception e) {
                }
            }
    
            return bos == null ? null : bos.getBytes();
    
        }
    
        public TestByteImage() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new ImagePane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class ImagePane extends JPanel {
    
            private BufferedImage original;
            private byte[] byteData;
            private BufferedImage fromBytes;
    
            public ImagePane() {
                String name = "/path/to/your/image";
                try {
                    original = ImageIO.read(new File(name));
                    byteData = extractBytes2(name);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
    
                setFont(UIManager.getFont("Label.font").deriveFont(Font.BOLD, 48f));
    
            }
    
            @Override
            public Dimension getPreferredSize() {
                return original == null ? super.getPreferredSize() : new Dimension(original.getWidth() * 2, original.getHeight());
            }
    
            protected void drawText(Graphics2D g2d, String text, int x, int width) {
                BufferedImage img = new BufferedImage(width, getHeight(), BufferedImage.TYPE_INT_ARGB);
                Graphics2D tmpg = img.createGraphics();
                tmpg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                tmpg.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                tmpg.setFont(g2d.getFont());
                tmpg.setColor(Color.RED);
                FontMetrics fm = tmpg.getFontMetrics();
                int xPos = ((width - fm.stringWidth(text)) / 2);
                int yPos = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
                tmpg.drawString(text, xPos, yPos);
                tmpg.dispose();
    
                AffineTransform transform = g2d.getTransform();
                g2d.setTransform(AffineTransform.getRotateInstance(Math.toRadians(-10), x + (x + width) / 2, getHeight() / 2));
                g2d.drawImage(img, x, 0, this);
                g2d.setTransform(transform);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (original != null) {
                    g.drawImage(original, 0, 0, this);
                    drawText((Graphics2D) g, "Original", 0, original.getWidth());
                }
                if (byteData != null && fromBytes == null) {
                    try {
                        fromBytes = ImageIO.read(new ByteInputStream(byteData, byteData.length));
                    } catch (IOException exp) {
                        exp.printStackTrace();
                    }
                }
                if (fromBytes != null) {
                    g.drawImage(fromBytes, getWidth() - fromBytes.getWidth(), 0, this);
                    drawText((Graphics2D) g, "From Bytes", getWidth() - fromBytes.getWidth(), fromBytes.getWidth());
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function to convert from one image representation ( MyImage ) to
I have a problem with Raphael.path2curve() . The function modifies SVG path string so
I have a problem in silverlight which I need to convert this image(from database
I have one problem with parsing *.docx document with OpenXML (C#). So, here's my
I have one problem with Javascript Nodes.I want to find out what button was
i have one problem with handling list,i have three class named as UserInf,userData,userProcess,i created
I have one problem , I want to get some data from XML file
I have one problem, I learn how work with socket and I write programm
i have one problem when i sort the NSMutablearray using date and time wise.
I have one problem with android calendar programming, so If there are 2 months

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.