Error saving templates with PNG Images

When I use the following method from your examples to try to make a stencil file with PNG images:

public static Master createMaster(final int masterId, final String name, final java.awt.Image image) throws Exception {
	// set master properties
	Master ms = new Master();
	ms.setID(masterId);
	ms.setName(name);
	ms.setIconSize(1);
	ms.setAlignName(2);
	ms.setMatchByName(0);
	ms.setIconUpdate(1);
	
	ms.setPatternFlags(0);
	ms.setHidden(0);

	// set master's shape properties
	final Shape shape = new Shape();
	ms.getShapes().add(shape);

	final String encodedImage = loadImage(image);
	final double width = 0.5443889263424177;
	final double height = 0.432916947568133;

	shape.setID(5);
	shape.setType(TypeValue.FOREIGN);

	shape.getXForm().getPinX().setValue(0.2221944631712089);
	shape.getXForm().getPinY().setValue(0.1666458473784065);
	shape.getXForm().getWidth().setValue(width);
	shape.getXForm().getHeight().setValue(height);
	shape.getXForm().getLocPinX().getUfe().setF("Width*0.5");
	shape.getXForm().getLocPinY().getUfe().setF("Height*0.5");
	shape.getXForm().getResizeMode().setValue(0);

	shape.getTextXForm().getTxtPinY().getUfe().setF("-TxtHeight/2");
	shape.getTextXForm().getTxtWidth().getUfe().setF("TEXTWIDTH(TheText)");
	shape.getTextXForm().getTxtHeight().getUfe().setF("TEXTHEIGHT(TheText, TxtWidth)");

	// set connection properties
	final Connection connection = new Connection();
	shape.getConnections().add(connection);

	connection.setID(1);
	connection.setNameU("All");
	connection.getX().setValue(0.22);
	connection.getX().getUfe().setF("Width*0.5");
	connection.getY().setValue(0.16);
	connection.getY().getUfe().setF("Height*0.5");
	connection.getDirX().setValue(0);
	connection.getDirY().setValue(0);
	connection.getType().setValue(0);
	connection.getAutoGen().setValue(BOOL.FALSE);
	connection.getPrompt().getUfe().setF("No Formula");

	shape.getForeignData().setForeignType(ForeignType.BITMAP);
	shape.getForeignData().setCompressionType(CompressionType.PNG);
	shape.getForeignData().setValue(encodedImage.getBytes());

	return ms;
}

I get the following exception when i try to save the file:

java.lang.IllegalStateException: Cannot convert image type to string.

The PNG files I am using look correct.

@Thinian

Can you please share the link to this code example as well? We will check and test the scenario in our environment and address it accordingly.

When I run this example it happens:

I tried both:

diagram.save(dataDir + “template_Out.vtx”, SaveFileFormat.VTX);
// diagram.save(dataDir + “template_Out.vssx”, SaveFileFormat.VSSX);

The first has no error, but the image is empty, the second throws the “can not convert” exception.

That’s one of yours.

@Thinian

We are checking it and will get back to you shortly.

Thanks…apparently it takes at least 12 characters to say that…

@Thinian

We have found an issue in the example and we have logged a ticket as DIAGRAMJAVA-51072 in order to get it fixed. The ticket has been associated with this forum thread and you will be notified as soon as it is resolved. Please be patient and spare us some time.

We are sorry for the inconvenience.

The issue is marked as resolved, so what is the solution?

@Thinian

The fix is scheduled to be included in 23.2 version of the API which will be released in February 2023.

Ok, thanks. I can wait

The issues you have found earlier (filed as DIAGRAMJAVA-51072) have been fixed in this update. This message was posted using Bugs notification tool by philip.zhou

I upgraded to this version, and the problem has not gone away. The example that did not work has been removed. Resisting urge to make sarcastic comment here. We are in the process of purchasing a license, hope you fix it soon.

@Thinian

We humbly apologize for the inconvenience you have faced. We are looking into this matter and will get back to your shortly.

@Thinian
The original example has problem, we have updated the entire example, please refresh the page to use the latest example.

Thanks.

I used the following code and it produced a file with a master of the correct name, but the image is not there.
The PNG file is valid.

public class ExampleImage {

public static void main(String[] args) throws Exception {
	// create a new template
	File flagsDir = new File("../src/main/resources/svg/flags");
	Diagram diagram = new Diagram();
	// add master
	diagram.getMasters().add(createMaster(101, "Regular", "./build/output/flags/zw.png"));
	// save template
	diagram.save("build/output/template_Out.vtx", SaveFileFormat.VTX);
}

// create master
public static Master createMaster(final int masterId, final String name, String file) throws Exception {
	// set master properties
	Master ms = new Master();
	ms.setID(masterId);
	ms.setName(name);
	ms.setIconSize(1);
	ms.setAlignName(2);
	ms.setMatchByName(0);
	ms.setIconUpdate(1);
	ms.setUniqueID(new k6g("00000000-0000-0000-0000-100000000" + masterId));
	ms.setUniqueID(new k6g("00000000-0000-0000-0000-100000000" + masterId));
	ms.setPatternFlags(0);
	ms.setHidden(0);

	// set master's shape properties
	final Shape shape = new Shape();
	ms.getShapes().add(shape);

	final double width = 0.5443889263424177;
	final double height = 0.432916947568133;

	shape.setID(5);
	shape.setType(TypeValue.FOREIGN);

	shape.getXForm().getPinX().setValue(0.2221944631712089);
	shape.getXForm().getPinY().setValue(0.1666458473784065);
	shape.getXForm().getWidth().setValue(width);
	shape.getXForm().getHeight().setValue(height);
	shape.getXForm().getLocPinX().getUfe().setF("Width*0.5");
	shape.getXForm().getLocPinY().getUfe().setF("Height*0.5");
	shape.getXForm().getResizeMode().setValue(0);

	shape.getTextXForm().getTxtPinY().getUfe().setF("-TxtHeight/2");
	shape.getTextXForm().getTxtWidth().getUfe().setF("TEXTWIDTH(TheText)");
	shape.getTextXForm().getTxtHeight().getUfe().setF("TEXTHEIGHT(TheText, TxtWidth)");

	// set connection properties
	final Connection connection = new Connection();
	shape.getConnections().add(connection);

	connection.setID(1);
	connection.setNameU("All");
	connection.getX().setValue(0.22);
	connection.getX().getUfe().setF("Width*0.5");
	connection.getY().setValue(0.16);
	connection.getY().getUfe().setF("Height*0.5");
	connection.getDirX().setValue(0);
	connection.getDirY().setValue(0);
	connection.getType().setValue(0);
	connection.getAutoGen().setValue(BOOL.FALSE);
	connection.getPrompt().getUfe().setF("No Formula");

	shape.getForeignData().setForeignType(ForeignType.BITMAP);
	shape.getForeignData().setCompressionType(CompressionType.PNG);
	File f = new File(file);
	byte[] fileBytes = new byte[(int) f.length()];
	FileInputStream fis = new FileInputStream(f);
	fis.read(fileBytes);
	fis.close();

	shape.getForeignData().setValue(fileBytes);

	return ms;
}

}

@Thinian

We are checking it and will get back to you shortly.

@Thinian
We are sorry for any inconvenience caused to you.
Please use this sample code to set icon update:
ms.setIconUpdate(BOOL.TRUE);

Add the following code to set the properties of the image.
shape.getForeign().getImgOffsetX().setValue(0);
shape.getForeign().getImgOffsetY().setValue(0);
shape.getForeign().getImgWidth().setValue(width);
shape.getForeign().getImgHeight().setValue(height);

We have updated this example.
Thanks.

Please note that when you manually generate a VTX file using Visio, the image cannot be seen in the Master of the generated VTX file, but it can be seen in the VSSX file.

Ok, so this is almost working. When I use the icons in another document and I set the width and height of the image, it does not scale the image.
image.png (11.1 KB)
All of the images are from the same template, the bottom one is drawn with a larger width and height.
When I make the stencil in the first place, I tried:

	shape.getXForm().getResizeMode().setValue(ResizeModeValue.SCALE_WITH_GROUP);

When I made the stencil by hand, it worked fine.

@Thinian

Is your issue resolved finally? Please let us know in case some assistance is still required.

Can you answer my last question, please. Then its done.

@Thinian

We are checking it and will get back to you shortly.