We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Change Size of a SDT Picture Content Control using C# .NET | Set Image Data & Lock Aspect Ratio | MemoryStream

Hi,
I am using the latest Aspose.Word. I had a Picture Content Control in my word.

After i locate the Content Control, i set a image into the sdt and set the width and height. However ,the image that i set will fit to the content control size. and the the width and height cannot set to the value i want.
For example, the size of the content control in word template is 10px x 10px. After i set the width to 1(did not set the height) from code, it will become 1px x 1px but not 10px x 1px.

here is my word and code:

foreach(StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true)) {
switch(sdt.SdtType) {
case SdtType.Picture:
Shape _shape= (Shape)sdt.GetChild(NodeType.Shape, 0, true);
Stream _stream = new MemoryStream(somePicByteArr);
dml.ImageData.SetImage(_stream);
_shape.Height = _height;
_shape.Width = _width;
}
}

By the way, i tried ConvertUtil.PixelToPoint(). the size did not match what i want and still have the problem above.

My question is how do i solve this problem? or any other way to do that?

here are the Word templates before editing, after editing and the expected result:
Aspose_Word.zip (91.8 KB)

@nickms,

Please try using the following code:

Document doc = new Document(@"E:\Temp\Aspose_Word\before.docx");

foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
{
    if (sdt.SdtType == SdtType.Picture)
    {
        Shape _shape = (Shape)sdt.GetChild(NodeType.Shape, 0, true);

        MemoryStream ms = new MemoryStream();
        new FileStream(@"E:\Temp\Aspose_Word\Aspose.Words.jpg", FileMode.Open).CopyTo(ms);
        byte[] bytes = ms.ToArray();

        _shape.ImageData.SetImage(ms);
        _shape.AspectRatioLocked = false;

        _shape.Height = 36;
        _shape.Width = 144;
    }
}

doc.Save(@"E:\Temp\Aspose_Word\19.8.docx");

Dear @awais.hafeez,

Thank you for your help! That is exactly what i want :
_shape.AspectRatioLocked = false;

Thank you very much!
Nick

@nickms,

Thanks for your feedback. In case you have further inquiries or need any help in future, please let us know.

A post was split to a new topic: Set Custom Width & Height of Image Shape in Word Document using Java