Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
i have read in a file using the getimage method but now wish to store each pixels RGB value in an array, i have tried using PixelGrabber and grabPixels but am getting errors with these methods. Can anyone see where i'm going wrong or suggest another method for my problem?
help would be appreciated!
tonyimport java.awt.*;
import java.awt.event.*;
import java.awt.image.PixelGrabber.*;public class LoadImage extends Frame {
private Image image;public LoadImage(String fileName) {
int w, h;
image = Toolkit.getDefaultToolkit().getImage(fileName);
MediaTracker mediaTracker = new MediaTracker(this);
mediaTracker.addImage(image, 0);
try
{
mediaTracker.waitForID(0);
}
catch (InterruptedException ie)
{
System.err.println(ie);
System.exit(1);
}
int width = image.getWidth(null);
int height = image.getHeight(null);
//////////////////////////////////////
// errors occuring in this block-->
int[] pixels = new int[width * height];
PixelGrabber myPixelGrabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width);
myPixelGrabber.grabPixels();
////////////////////////////////////////////////
}
public static void main(String[] args) {
new LoadImage(args[0]);
}
}

Okay I haven't done Java in like ages, but I think the problem may be above the area your looking at, when you get the height and width you use:
int width = image.getWidth(null);
but I believe this should be:
int width = image.getWidth(this);
(for both of them of course).
Check our this guys example of the code (link: http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20336901.html
He's doing the same thing (it right around halfway down the page, just search for pixelgrabber). Otherwise you could simply print out the width and height before you use them and make sure they are not zero.

thanks for your help, the link was great help...i was just stupid and forgot to import one of the correct libraries...
: )

![]() |
dynamic allocation
|
VB.Net and Graphing Help?
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |