Table of Contents

Resize, move and copy a picture

A picture already in a document is resized with ResizeImage, moved with MoveImage and copied with CopyImage: one undoable DocumentEdit each, as Word's handles and drags make for a picture in line with text.

The code

using DocWright.Core.Primitives;
using DocWright.Dom;
using DocWright.Dom.Editing;

ImageRef picture = first.Inlines.OfType<ImageRef>().Single();

// Twice the size, proportions kept; the saved drawing says so.
DocumentEdit resize = document.ResizeImage(picture, Emu.FromInches(2), Emu.FromInches(1.5));
Console.WriteLine($"resized to {picture.Width.Value} x {picture.Height.Value} EMU");

// Into the next paragraph, as dragging it does: the same node, its alt text kept.
DocumentEdit move = document.MoveImage(picture, DocumentPosition.StartOf(second));
Console.WriteLine($"moved: before offset {DocumentPosition.Before(picture).Offset} of the second paragraph, alt text \"{picture.Description}\"");

// A copy at the end of that paragraph, as Ctrl+drag does: its own drawing id.
DocumentEdit copy = document.CopyImage(picture, DocumentPosition.EndOf(second));
ImageRef copied = second.Inlines.OfType<ImageRef>().Last();
Console.WriteLine($"copied: drawing ids {picture.DrawingId} and {copied.DrawingId}");

// Each is one undo step.
copy.Undo();
Console.WriteLine($"after undo: {second.Inlines.OfType<ImageRef>().Count()} picture in the second paragraph");

Output

resized to 1828800 x 1371600 EMU
moved: before offset 0 of the second paragraph, alt text "A checked tile"
copied: drawing ids 1 and 2
after undo: 1 picture in the second paragraph
the placement at (1440, 1890) tw draws "A checked tile"
pictures.pdf
A page with a line of text, and below it a two-inch checked picture before the words The next paragraph.

What each edit does

Edit What changes Refused when
ResizeImage(picture, width, height) Width and Height, and the saved drawing's wp:extent and the picture's a:ext, rewritten in place: every other byte of the drawing (effects, crop, alt text) stays the drawing is not one DrawingML picture (VML, a group): InvalidOperationException; a side at or below zero or above ImageEditingExtensions.MaxImageSide (22 in, Word's limit): ArgumentOutOfRangeException
MoveImage(picture, to) the picture node itself goes to the position (given as it is before the move), splitting a run there; a position just before or after it is an empty edit the position is in another package part than the picture (a body picture into a header): ArgumentException, since a relationship belongs to one part
CopyImage(picture, to) a clone goes to the position, with a drawing id of its own; it shares the image part as MoveImage

Setting ImageRef.Width directly changes the layout but not the file: the DOCX writer writes a picture from its preserved drawing markup, which is why the edit rewrites it. DocumentPosition.Before(picture) is where a picture is; it is one offset wide, so deleting it is DeleteRange over that offset and the next. Each edit's ChangedParagraphKeys lets a LayoutSession re-lay out only what moved (Edit text at a position).

Which picture is under a point

With source references on (a LayoutSession has them by default), every ImagePlacement of a document picture carries SourceKey, the picture's NodeId value. A renderer of its own can hit-test the placements and find the picture to resize or move:

using DocWright.Core.Rendering;
using DocWright.Dom;
using DocWright.Fonts;
using DocWright.Layout;

// Which picture does a point of the page show? Lay out with source references on.
var session = new LayoutSession(new FontEngine());
LayoutDocument layout = session.Layout(document);
// DocWright.Dom has an ImagePlacement enum too (inline or anchored): name the layout primitive in full.
DocWright.Core.Rendering.ImagePlacement placement = layout.Pages[0].Primitives
    .OfType<DocWright.Core.Rendering.ImagePlacement>().Single(p => p.SourceKey != 0);
var found = (ImageRef)document.FindNode(DomNodeId.FromValue(placement.SourceKey))!;
Console.WriteLine($"the placement at ({placement.X.Value}, {placement.Y.Value}) tw draws \"{found.Description}\"");

DocWright.Dom also has an ImagePlacement (the enum that says whether a picture is inline or anchored), so a file that uses both namespaces names the layout primitive in full. SourceKey is 0 without source references, for a shape's fill and for a report image; a picture inside a metafile drawn as vector paths has no placement at all.