tags:

views:

210

answers:

1

I'm using this class swt.graphics.ImageLoader to save a file as a PNG:

ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] { m_imgdat };
loader.save(selected, SWT.IMAGE_PNG);

Yet sometimes the file I get is incomplete. the last line of pixels is black and some programs (photoshop) refuse to open it altogether.
Is there something I'm missing? How can I tell this class to flush the file and close it?

+1  A: 

The principle is correct, but you may try to save it like ImageDialog does:

See its buttonPressed() method:

try
{
    ImageData imageData = new ImageData( new ByteArrayInputStream JavaDoc( this.newImageRawData ) );
    if ( imageData.type != this.requiredImageType )
    {
        ImageLoader imageLoader = new ImageLoader();
        imageLoader.data = new ImageData[] { imageData };
        ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
        imageLoader.save( baos, this.requiredImageType );
        this.newImageRawDataInRequiredFormat = baos.toByteArray();
    }
    else
    {
        this.newImageRawDataInRequiredFormat = this.newImageRawData;
    }
}
catch ( SWTException swte )
{
    this.newImageRawDataInRequiredFormat = null;
}


Note: When it saves an image, it does use the following code:

try
    {
    File JavaDoc file = new File JavaDoc( returnedFileName );
    FileOutputStream JavaDoc out = new FileOutputStream JavaDoc( file );
    out.write( currentImageRawData );
    out.flush();
    out.close();
}
VonC