Save component to image

27 views
Skip to first unread message

Bryan Buchanan

unread,
Oct 7, 2016, 10:37:34 PM10/7/16
to CodenameOne Discussions
I want to do the same as this Android code in CN1:
Bitmap mBitmap = Bitmap.createBitmap(mContent.getWidth(), mContent.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(mBitmap);
v
.draw(canvas);                            // draw current view to bitmap
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();   // now have PNG of the component

Shai answered a similar question with: "You can create a mutable image (Image.create(width,height)) then invoke getGraphics on it and paint the component onto it.", which seems reasonable, but I'm not sure what to do to make that happen.

Chen Fishbein

unread,
Oct 8, 2016, 2:09:39 AM10/8/16
to CodenameOne Discussions
Try this:               

                Image buffer = Image.createImage(width, height);
                Graphics g = buffer.getGraphics();
                
               g.translate(-cmp.getAbsoluteX(), -cmp.getAbsoluteY());
                cmp.paintComponent(g);
               g.translate(cmp.getAbsoluteX(), cmp.getAbsoluteY());
              
               ByteArrayOutputStream stream = new ByteArrayOutputStream();
               ImageIO.getImageIO().save(buffer, stream, ImageIO.FORMAT_PNG, 1);
               byte[] byteArray = stream.toByteArray();

Bryan Buchanan

unread,
Oct 8, 2016, 3:40:43 AM10/8/16
to CodenameOne Discussions
Thanks Chen.

On further looking around I noticed the SignatureComponent which Steve had written which has exactly the code I wanted - clever guy that Steve !

Thanks.

Reply all
Reply to author
Forward
0 new messages