Saving Diagram as Image is clipped if negative co-ordinates. How to solve this? [message #533437] |
Thu, 13 May 2010 20:58 |
h1055071 Messages: 335 Registered: July 2009 |
Senior Member |
|
|
The problem is that if any of the diagram's child figures have a
negative x,y, location (for example, x = -100, y = -100) this portion is
not saved to the image. It seems that the figure only saves to image
from origin (0, 0).
I'm using a ScalableFreeformRootEditPart, and a FreeformLayer with
FreeformLayout for my diagram's main edit part.
This is how I save the diagram as an image:
ScalableFreeformRootEditPart rootEditPart =
(ScalableFreeformRootEditPart)diagramViewer.getRootEditPart( );
IFigure figure = rootEditPart.getLayer(LayerConstants.PRINTABLE_LAYERS);
Rectangle rectangle = figure.getBounds();
Image image = new Image(Display.getDefault(), rectangle.width,
rectangle.height);
GC gc = new GC(image);
SWTGraphics graphics = new SWTGraphics(gc);
figure.paint(graphics);
ImageLoader loader = new ImageLoader();
loader.data = new ImageData[]{ image.getImageData() };
loader.save(file, SWT.IMAGE_PNG);
Any ideas? Thanks.
Phil
|
|
|
|
|
Re: Saving Diagram as Image is clipped if negative co-ordinates. How to solve this? [message #533508 is a reply to message #533507] |
Fri, 14 May 2010 10:39 |
h1055071 Messages: 335 Registered: July 2009 |
Senior Member |
|
|
On 14/05/2010 11:21, Phillipus wrote:
> On 14/05/2010 10:25, vijay wrote:
>> This is not possible,
>>
>> because if the image is clipped then the figure on the canvas also
>> should be clipped.
>>
>> because both are being drawn using GC,only difference being canvas or
>> image.
>>
>> can you provide a snippet to proove your point.
>
> Hi, thanks for help!
>
> I can't really provide more of a "snippet" than the following that I
> have implemented:
>
> My Editor extends GraphicalEditorWithFlyoutPalette.
>
> My Graphical Viewer's Root EditPart is a ScalableFreeformRootEditPart:
>
> ScalableFreeformRootEditPart rootEditPart = new
> ScalableFreeformRootEditPart();
> getGraphicalViewer().setRootEditPart(rootEditPart);
>
> My domain model's main EditPart extends AbstractGraphicalEditPart and
> creates a main layer as follows:
>
> protected IFigure createFigure() {
> FreeformLayer figure = new FreeformLayer();
> figure.setLayoutManager(new FreeformLayout());
> return figure;
> }
>
> This ensures that I can place child figures anywhere on the diagram
> including in negative co-ordinates. If I do this, the scroll bars allow
> me to see all of the diagram.
>
> My "Save as Image" action is basically:
>
> ScalableFreeformRootEditPart rootEditPart =
> (ScalableFreeformRootEditPart)graphicalViewer.getRootEditPar t();
>
> IFigure figure = rootEditPart.getLayer(LayerConstants.PRINTABLE_LAYERS);
>
> Rectangle rectangle = figure.getBounds();
>
> Image image = new Image(Display.getDefault(), rectangle.width,
> rectangle.height);
>
> GC gc = new GC(image);
> SWTGraphics graphics = new SWTGraphics(gc);
> figure.paint(graphics);
>
> ImageLoader loader = new ImageLoader();
> loader.data = new ImageData[] { image.getImageData() };
> loader.save(file, SWT.IMAGE_PNG);
I found the answer. I need to add the following line:
graphics.translate(rectangle.x * -1, rectangle.y * -1);
So it is now:
ScalableFreeformRootEditPart rootEditPart =
(ScalableFreeformRootEditPart)graphicalViewer.getRootEditPar t();
IFigure figure = rootEditPart.getLayer(LayerConstants.PRINTABLE_LAYERS);
Rectangle rectangle = figure.getBounds();
Image image = new Image(Display.getDefault(), rectangle.width,
rectangle.height);
GC gc = new GC(image);
SWTGraphics graphics = new SWTGraphics(gc);
graphics.translate(rectangle.x * -1, rectangle.y * -1);
figure.paint(graphics);
ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] { image.getImageData() };
loader.save(file, SWT.IMAGE_PNG);
|
|
|
Powered by
FUDForum. Page generated in 0.04434 seconds