Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to convert an Image to a byte array?

807588Aug 5 2009 — edited Aug 5 2009
I want to make a screenshot and then convert the image to a byte of arrays so I can send it through a BufferedOutputStream.
try
		{
			robot = new Robot();
			screenshot = robot.createScreenCapture(new Rectangle(500,500));
			imageFile = new File("image.png");

			ImageInputStream iis = ImageIO.createImageInputStream(screenshot);

			byte[] data = new byte[1024];
			byte[] tmp = new byte[0];
			byte[] myArrayImage = new byte[0];
			int len = 0;
			int total = 0;
			while((len = iis.read(data)) != -1 ) // LINE 52 --- EXCEPTION CATCHED HERE
			{
				total += len;
				tmp = myArrayImage;
				myArrayImage = new byte[total];
				System.arraycopy(tmp,0,myArrayImage,0,tmp.length);
				System.arraycopy(data,0,myArrayImage,tmp.length,len);
			}
			ios.close();
I get this exception while running:

Exception in thread "Thread-0" java.lang.NullPointerException
at Server.run(Server.java:52)
at java.lang.Thread.run(Unknown Source)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 2 2009
Added on Aug 5 2009
8 comments
637 views