0

我将图像保存在字符串中,然后当我打开它时它总是 300x150

为什么图像被截断?

300x150 从何而来?

代码就是你所看到的。只需 2 个按钮。

第一个将图像保存在“png”中,另一个从“png”中读取图像

                    Button save = new Button("copy");
                    save.addClickHandler(new ClickHandler() {
                        @Override
                        public void onClick(ClickEvent event) {
                            ImageElement imageElement = ImageElement.as(image.getElement());

                            Canvas canvasTmp = Canvas.createIfSupported();
                            Context2d context = canvasTmp.getContext2d();
                            context.drawImage(imageElement, 0.0, 0.0, imageElement.getWidth(), imageElement.getHeight());
                            png = canvasTmp.toDataUrl("image/png");
                        }
                    });

                    Button open = new Button("open");
                    open.addClickHandler(new ClickHandler() {

                        @Override
                        public void onClick(ClickEvent event) {

                            final Image image = new Image(png);
                            vp.add(image);
                            image.addLoadHandler(new LoadHandler() {

                                @Override
                                public void onLoad(LoadEvent event) 
                                {
                                    Window.alert("ok");
                                }
                            });

                            image.addErrorHandler(new ErrorHandler() {

                                @Override
                                public void onError(ErrorEvent event) {
                                    Window.alert("error");
                                }
                            } );

                        }
                    });
4

1 回答 1

1

画布的默认宽度为 300 像素,默认高度为 150 像素。在创建画布之后和绘制图像之前,考虑这样做:

int width = imageElement.getWidth()
int height = imageElement.getHeight()
canvasTmp.setWidth(width + "px");
canvasTmp.setHeight(height + "px");
canvasTmp.setCoordinateSpaceWidth(width);
canvasTmp.setCoordinateSpaceHeight(height);
于 2012-07-18T14:58:28.957 回答