pub struct Picture {
pub id: u32,
pub name: String,
pub data: Vec<u8>,
pub format: PictureFormat,
pub offset_emu: (i64, i64),
pub extent_emu: (i64, i64),
pub description: String,
pub shape_properties: Option<ShapeProperties>,
pub external_link: Option<String>,
}Expand description
An embedded image (<p:pic>, CT_Picture).
Unlike an AutoShape, a picture’s position/size is never optional or defaulted:
offset_emu/extent_emu are plain fields (not routed through drawing::Transform2D’s Option
flexibility), since a PresentationML picture’s <a:xfrm> — unlike Excel’s cell-anchored
pictures — is the shape’s only source of position/size; there is no separate anchor to fall
back on, so demanding a concrete value upfront avoids ever having to invent one.
Fields§
§id: u32<p:cNvPr id=".."> — same uniqueness rules as AutoShape::id.
name: String<p:cNvPr name="..">.
data: Vec<u8>The raw, encoded image bytes (e.g. the bytes of a .png file).
format: PictureFormatThe image’s encoding, also used to pick the media part’s extension and content type.
offset_emu: (i64, i64)<a:off x=".." y="..">, in EMUs.
extent_emu: (i64, i64)<a:ext cx=".." cy="..">, in EMUs.
description: StringAlt text / description (p:cNvPr’s descr attribute). May be empty.
shape_properties: Option<ShapeProperties>Additional shape formatting (fill/line/rotation, and a non-default outline geometry — e.g.
an oval crop) for the picture’s own p:spPr, beyond the fixed default rectangle. None
(the default) produces a plain rectangular picture with no extra fill/line, the same fixed
shape word-ooxml/excel-ooxml’s own pictures default to. transform is never consulted
here — offset_emu/extent_emu above are always authoritative for a picture’s
position/size, mirroring word_ooxml::Image::shape_properties’s own posture.
external_link: Option<String><a:blip r:link=".">’s own external target. When set, the writer emits both r:embed
(pointing at data below, embedded in the package as usual) and r:link
(TargetMode="External", this URL/path) on the same <a:blip> — PowerPoint’s own “embedded
preview cache + externally-linked original” dual pattern, so the picture still displays even
when the external source is unreachable. None (the default) omits r:link entirely — a
plain, fully-embedded picture, unchanged. Grounded against a real fixture.
Implementations§
Source§impl Picture
impl Picture
Sourcepub fn new(
id: u32,
name: impl Into<String>,
data: impl Into<Vec<u8>>,
format: PictureFormat,
width_emu: i64,
height_emu: i64,
) -> Picture
pub fn new( id: u32, name: impl Into<String>, data: impl Into<Vec<u8>>, format: PictureFormat, width_emu: i64, height_emu: i64, ) -> Picture
Creates a picture at the given size, positioned at (0, 0) (use Picture::with_offset to
place it elsewhere), with no alt text, no extra shape formatting, and no external link.
Examples found in repository?
20fn main() -> office_toolkit::Result<()> {
21 let path = output_path("pptx_media.pptx");
22 let image_bytes = std::fs::read(image_fixture_path())?;
23
24 let embedded_picture = Picture::new(
25 2,
26 "Embedded picture",
27 image_bytes.clone(),
28 PictureFormat::Png,
29 3_000_000,
30 2_000_000,
31 )
32 .with_offset(838_200, 838_200)
33 .with_description("Project test image");
34
35 let linked_picture = Picture::new(
36 2,
37 "Externally linked picture",
38 image_bytes,
39 PictureFormat::Png,
40 3_000_000,
41 2_000_000,
42 )
43 .with_offset(838_200, 838_200)
44 .with_external_link("https://example.com/original-image.png");
45
46 // Placeholder bytes — a real .pptx would carry a genuine video file
47 // here. This example only demonstrates the model/writer plumbing.
48 let poster_image = vec![0x89, 0x50, 0x4E, 0x47]; // fake, just enough bytes to have *some* data
49 let media = SlideMedia::new(
50 2,
51 "Video clip",
52 vec![0u8; 16],
53 MediaFormat::Mp4,
54 4_000_000,
55 2_250_000,
56 )
57 .with_offset(838_200, 838_200)
58 .with_poster_image(poster_image, PictureFormat::Png);
59
60 let chart = SlideChart::new(
61 2,
62 "Quarterly revenue",
63 sample_chart_space(),
64 6_000_000,
65 3_500_000,
66 )
67 .with_offset(838_200, 838_200);
68
69 let presentation = Presentation::new()
70 .with_slide(Slide::new().with_shape(Shape::Picture(embedded_picture)))
71 .with_slide(Slide::new().with_shape(Shape::Picture(linked_picture)))
72 .with_slide(Slide::new().with_shape(Shape::Media(media)))
73 .with_slide(Slide::new().with_shape(Shape::Chart(Box::new(chart))));
74
75 presentation.save_to_file(&path)?;
76 println!("Wrote {}", path.display());
77 Ok(())
78}Sourcepub fn with_offset(self, x_emu: i64, y_emu: i64) -> Picture
pub fn with_offset(self, x_emu: i64, y_emu: i64) -> Picture
Sets this picture’s position.
Examples found in repository?
20fn main() -> office_toolkit::Result<()> {
21 let path = output_path("pptx_media.pptx");
22 let image_bytes = std::fs::read(image_fixture_path())?;
23
24 let embedded_picture = Picture::new(
25 2,
26 "Embedded picture",
27 image_bytes.clone(),
28 PictureFormat::Png,
29 3_000_000,
30 2_000_000,
31 )
32 .with_offset(838_200, 838_200)
33 .with_description("Project test image");
34
35 let linked_picture = Picture::new(
36 2,
37 "Externally linked picture",
38 image_bytes,
39 PictureFormat::Png,
40 3_000_000,
41 2_000_000,
42 )
43 .with_offset(838_200, 838_200)
44 .with_external_link("https://example.com/original-image.png");
45
46 // Placeholder bytes — a real .pptx would carry a genuine video file
47 // here. This example only demonstrates the model/writer plumbing.
48 let poster_image = vec![0x89, 0x50, 0x4E, 0x47]; // fake, just enough bytes to have *some* data
49 let media = SlideMedia::new(
50 2,
51 "Video clip",
52 vec![0u8; 16],
53 MediaFormat::Mp4,
54 4_000_000,
55 2_250_000,
56 )
57 .with_offset(838_200, 838_200)
58 .with_poster_image(poster_image, PictureFormat::Png);
59
60 let chart = SlideChart::new(
61 2,
62 "Quarterly revenue",
63 sample_chart_space(),
64 6_000_000,
65 3_500_000,
66 )
67 .with_offset(838_200, 838_200);
68
69 let presentation = Presentation::new()
70 .with_slide(Slide::new().with_shape(Shape::Picture(embedded_picture)))
71 .with_slide(Slide::new().with_shape(Shape::Picture(linked_picture)))
72 .with_slide(Slide::new().with_shape(Shape::Media(media)))
73 .with_slide(Slide::new().with_shape(Shape::Chart(Box::new(chart))));
74
75 presentation.save_to_file(&path)?;
76 println!("Wrote {}", path.display());
77 Ok(())
78}Sourcepub fn with_description(self, description: impl Into<String>) -> Picture
pub fn with_description(self, description: impl Into<String>) -> Picture
Sets this picture’s alt text / description.
Examples found in repository?
20fn main() -> office_toolkit::Result<()> {
21 let path = output_path("pptx_media.pptx");
22 let image_bytes = std::fs::read(image_fixture_path())?;
23
24 let embedded_picture = Picture::new(
25 2,
26 "Embedded picture",
27 image_bytes.clone(),
28 PictureFormat::Png,
29 3_000_000,
30 2_000_000,
31 )
32 .with_offset(838_200, 838_200)
33 .with_description("Project test image");
34
35 let linked_picture = Picture::new(
36 2,
37 "Externally linked picture",
38 image_bytes,
39 PictureFormat::Png,
40 3_000_000,
41 2_000_000,
42 )
43 .with_offset(838_200, 838_200)
44 .with_external_link("https://example.com/original-image.png");
45
46 // Placeholder bytes — a real .pptx would carry a genuine video file
47 // here. This example only demonstrates the model/writer plumbing.
48 let poster_image = vec![0x89, 0x50, 0x4E, 0x47]; // fake, just enough bytes to have *some* data
49 let media = SlideMedia::new(
50 2,
51 "Video clip",
52 vec![0u8; 16],
53 MediaFormat::Mp4,
54 4_000_000,
55 2_250_000,
56 )
57 .with_offset(838_200, 838_200)
58 .with_poster_image(poster_image, PictureFormat::Png);
59
60 let chart = SlideChart::new(
61 2,
62 "Quarterly revenue",
63 sample_chart_space(),
64 6_000_000,
65 3_500_000,
66 )
67 .with_offset(838_200, 838_200);
68
69 let presentation = Presentation::new()
70 .with_slide(Slide::new().with_shape(Shape::Picture(embedded_picture)))
71 .with_slide(Slide::new().with_shape(Shape::Picture(linked_picture)))
72 .with_slide(Slide::new().with_shape(Shape::Media(media)))
73 .with_slide(Slide::new().with_shape(Shape::Chart(Box::new(chart))));
74
75 presentation.save_to_file(&path)?;
76 println!("Wrote {}", path.display());
77 Ok(())
78}Sourcepub fn with_shape_properties(self, shape_properties: ShapeProperties) -> Picture
pub fn with_shape_properties(self, shape_properties: ShapeProperties) -> Picture
Sets this picture’s additional shape formatting (fill/line/ geometry), beyond the fixed default rectangle.
Sourcepub fn with_external_link(self, url: impl Into<String>) -> Picture
pub fn with_external_link(self, url: impl Into<String>) -> Picture
Additionally links this picture to an external image (r:link), alongside its own embedded
data — PowerPoint’s own “embedded preview cache + externally-linked original” dual
pattern.
Examples found in repository?
20fn main() -> office_toolkit::Result<()> {
21 let path = output_path("pptx_media.pptx");
22 let image_bytes = std::fs::read(image_fixture_path())?;
23
24 let embedded_picture = Picture::new(
25 2,
26 "Embedded picture",
27 image_bytes.clone(),
28 PictureFormat::Png,
29 3_000_000,
30 2_000_000,
31 )
32 .with_offset(838_200, 838_200)
33 .with_description("Project test image");
34
35 let linked_picture = Picture::new(
36 2,
37 "Externally linked picture",
38 image_bytes,
39 PictureFormat::Png,
40 3_000_000,
41 2_000_000,
42 )
43 .with_offset(838_200, 838_200)
44 .with_external_link("https://example.com/original-image.png");
45
46 // Placeholder bytes — a real .pptx would carry a genuine video file
47 // here. This example only demonstrates the model/writer plumbing.
48 let poster_image = vec![0x89, 0x50, 0x4E, 0x47]; // fake, just enough bytes to have *some* data
49 let media = SlideMedia::new(
50 2,
51 "Video clip",
52 vec![0u8; 16],
53 MediaFormat::Mp4,
54 4_000_000,
55 2_250_000,
56 )
57 .with_offset(838_200, 838_200)
58 .with_poster_image(poster_image, PictureFormat::Png);
59
60 let chart = SlideChart::new(
61 2,
62 "Quarterly revenue",
63 sample_chart_space(),
64 6_000_000,
65 3_500_000,
66 )
67 .with_offset(838_200, 838_200);
68
69 let presentation = Presentation::new()
70 .with_slide(Slide::new().with_shape(Shape::Picture(embedded_picture)))
71 .with_slide(Slide::new().with_shape(Shape::Picture(linked_picture)))
72 .with_slide(Slide::new().with_shape(Shape::Media(media)))
73 .with_slide(Slide::new().with_shape(Shape::Chart(Box::new(chart))));
74
75 presentation.save_to_file(&path)?;
76 println!("Wrote {}", path.display());
77 Ok(())
78}