Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Canvas : save to jpg
Canvas : save to jpg [message #232654] Wed, 04 April 2007 09:14 Go to next message
Eclipse UserFriend
Originally posted by: ben.tiscali.fr

Hi guys,

First, sorry, if i'm not in the good newsgroup but i didn't find any
draw2D group.
And sorry for my english too :)

So here's comes the problem.
I use a Canvas and a LightweightSystem to draw some figures (I create a
class subclassed from Figure).
And everything work well. But I need to export the canvas into a jpeg ,
png or bmp file, and i didn't manage to do it. I fond some source code
which allow to make a screenshot, but if the windows is minimized, the
screenshot takes whe windows desktop (for example).

Who can help me please ?


ps : is there a way to convert a swt Graphics to a classical java Graphics
or Graphics2D ?

Thanks
Re: Canvas : save to jpg [message #232664 is a reply to message #232654] Wed, 04 April 2007 09:37 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: michele.l.evinco.it

the follow code convert a BufferedImage to ImageData, i think it's
possible to do the inverse :


public ImageData convertToSWT(BufferedImage bufferedImage) {
try{
if (bufferedImage.getColorModel() instanceof DirectColorModel) {

DirectColorModel colorModel = (DirectColorModel)
bufferedImage.getColorModel();
PaletteData palette = new
PaletteData(colorModel.getRedMask(),colorModel.getGreenMask( ),
colorModel.getBlueMask());
ImageData data = new
ImageData(bufferedImage.getWidth(),bufferedImage.getHeight() ,
colorModel.getPixelSize(),palette);
WritableRaster raster = bufferedImage.getRaster();

int[] pixelArray = new int[4];
for (int y = 0; y < data.height; y++) {
for (int x = 0; x < data.width; x++) {
try{
pixelArray = raster.getPixel(x, y, pixelArray);
int pixel = palette.getPixel(new
RGB(pixelArray[0],pixelArray[1], pixelArray[2]));
data.setPixel(x, y, pixel);
}catch(ArrayIndexOutOfBoundsException aiobe){
aiobe.printStackTrace();
}
}
}

return data;

} else if (bufferedImage.getColorModel() instanceof IndexColorModel) {

IndexColorModel colorModel = (IndexColorModel)
bufferedImage.getColorModel();
int size = colorModel.getMapSize();

byte[] reds = new byte[size];
byte[] greens = new byte[size];
byte[] blues = new byte[size];

colorModel.getReds(reds);
colorModel.getGreens(greens);
colorModel.getBlues(blues);
RGB[] rgbs = new RGB[size];

for (int i = 0; i < rgbs.length; i++) {
rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF,
blues[i] & 0xFF);
}

PaletteData palette = new PaletteData(rgbs);
ImageData data = new ImageData(bufferedImage.getWidth(),
bufferedImage.getHeight(), colorModel.getPixelSize(), palette);
data.transparentPixel = colorModel.getTransparentPixel();
WritableRaster raster = bufferedImage.getRaster();

int[] pixelArray = new int[1];

for (int y = 0; y < data.height; y++) {
for (int x = 0; x < data.width; x++) {
raster.getPixel(x, y, pixelArray);
data.setPixel(x, y, pixelArray[0]);
}
}
return data;
}

return null;

}catch(Exception e){
e.printStackTrace();
return null;
}

}


ben wrote:
> Hi guys,
>
> First, sorry, if i'm not in the good newsgroup but i didn't find any
> draw2D group. And sorry for my english too :)
>
> So here's comes the problem. I use a Canvas and a LightweightSystem
> to draw some figures (I create a class subclassed from Figure). And
> everything work well. But I need to export the canvas into a jpeg ,
> png or bmp file, and i didn't manage to do it. I fond some source
> code which allow to make a screenshot, but if the windows is
> minimized, the screenshot takes whe windows desktop (for example).
>
> Who can help me please ?
>
>
> ps : is there a way to convert a swt Graphics to a classical java
> Graphics or Graphics2D ?
>
> Thanks
>
Re: Canvas : save to jpg [message #232775 is a reply to message #232654] Sat, 07 April 2007 18:39 Go to previous message
Thomas Maier is currently offline Thomas MaierFriend
Messages: 117
Registered: July 2009
Senior Member
Hi Ben,

you might want to have a look at the Image Export Plug In at

http://www.se.eecs.uni-kassel.de/~thm/Projects/ImageExport/i ndex.html

HTH, Thomas.


ben schrieb:
> Hi guys,
>
> First, sorry, if i'm not in the good newsgroup but i didn't find any
> draw2D group.
> And sorry for my english too :)
>
> So here's comes the problem.
> I use a Canvas and a LightweightSystem to draw some figures (I create
> a class subclassed from Figure).
> And everything work well. But I need to export the canvas into a jpeg
> , png or bmp file, and i didn't manage to do it. I fond some source
> code which allow to make a screenshot, but if the windows is
> minimized, the screenshot takes whe windows desktop (for example).
>
> Who can help me please ?
>
>
> ps : is there a way to convert a swt Graphics to a classical java
> Graphics or Graphics2D ?
>
> Thanks
>
Previous Topic:How to make a multipageGraphical editor ?
Next Topic:XYLayoutEditPolicy - Extend to Select Additional EditParts
Goto Forum:
  


Current Time: Mon Oct 25 18:08:15 GMT 2021

Powered by FUDForum. Page generated in 0.01427 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top