Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将gwt画布生成的图像保存到计算机硬盘?_Gwt_Canvas - Fatal编程技术网

如何将gwt画布生成的图像保存到计算机硬盘?

如何将gwt画布生成的图像保存到计算机硬盘?,gwt,canvas,Gwt,Canvas,下面是在GWT中从画布获取图像的代码 testCanvas.setWidth(144 + "px"); testCanvas.setHeight(144 + "px"); testCanvas.setCoordinateSpaceWidth(144); testCanvas.setCoordinateSpaceHeight(144); context = testCanvas.getContext2d(); context.fillRect(1,1,114,114); imagelo

下面是在GWT中从画布获取图像的代码

testCanvas.setWidth(144 + "px");
testCanvas.setHeight(144 + "px");
testCanvas.setCoordinateSpaceWidth(144);
testCanvas.setCoordinateSpaceHeight(144);
context = testCanvas.getContext2d();
context.fillRect(1,1,114,114);      
imagelocation=testCanvas.toDataUrl();
Image image=new Image(imagelocation);

现在我有了这个图像,我想把这个图像保存在我电脑的某个位置。我该怎么做呢?

我认为您必须将数据发送到服务器并让用户从该服务器下载数据,或者嵌入一些可以直接访问硬盘的闪存或Java对象。在大多数浏览器中,JavaScript(包括GWT)不允许将文件写入硬盘


如果您的应用程序只需要访问图像,您可以使用文件以外的某种形式的本地存储,尽管您的用户无法从其他应用程序访问该存储。

您可以使用canvas.toDataUrl(“image/png”)保存图像

// Add screen capture button
Button bt = new Button();
bt.setText("Capture");
RootPanel.get().add(bt);
bt.addClickHandler(new ClickHandler() {

    @Override
    public void onClick(ClickEvent event) {
        Window.open(canvas.toDataUrl("image/png"), "_blank", "");
    }
});

您的意思是要触发该图像的文件下载对话框?