@@ -94,6 +94,14 @@ class ImageExpression(IHtmlImageElement node) : ImageExpressionBase(node)
9494 preferredSize = ImageHeader . KeepAspectRatio ( actualSize , preferredSize ) ;
9595 }
9696
97+ // If size is still empty (e.g., for external linked images), use default dimensions
98+ if ( preferredSize . IsEmpty || preferredSize . Width <= 0 || preferredSize . Height <= 0 )
99+ {
100+ // Use default size for external images or when size cannot be determined
101+ // Default to a reasonable size (similar to how browsers handle images with unknown dimensions)
102+ preferredSize = new Size ( 300 , 200 ) ;
103+ }
104+
97105 long widthInEmus = new Unit ( UnitMetric . Pixel , preferredSize . Width ) . ValueInEmus ;
98106 long heightInEmus = new Unit ( UnitMetric . Pixel , preferredSize . Height ) . ValueInEmus ;
99107
@@ -103,22 +111,26 @@ class ImageExpression(IHtmlImageElement node) : ImageExpressionBase(node)
103111 new wp . Extent ( ) { Cx = widthInEmus , Cy = heightInEmus } ,
104112 new wp . EffectExtent ( ) { LeftEdge = 19050L , TopEdge = 0L , RightEdge = 0L , BottomEdge = 0L } ,
105113 new wp . DocProperties ( ) { Id = drawingObjId , Name = "Picture " + imageObjId , Description = string . Empty } ,
106- new wp . NonVisualGraphicFrameDrawingProperties {
114+ new wp . NonVisualGraphicFrameDrawingProperties
115+ {
107116 GraphicFrameLocks = new a . GraphicFrameLocks ( ) { NoChangeAspect = true }
108117 } ,
109118 new a . Graphic (
110119 new a . GraphicData (
111120 new pic . Picture (
112- new pic . NonVisualPictureProperties {
113- NonVisualDrawingProperties = new pic . NonVisualDrawingProperties ( ) {
121+ new pic . NonVisualPictureProperties
122+ {
123+ NonVisualDrawingProperties = new pic . NonVisualDrawingProperties ( )
124+ {
114125 Id = imageObjId ,
115126 Name = DataUri . IsWellFormed ( src ) ? string . Empty : src ,
116- Description = alt } ,
127+ Description = alt
128+ } ,
117129 NonVisualPictureDrawingProperties = new pic . NonVisualPictureDrawingProperties (
118130 new a . PictureLocks ( ) { NoChangeAspect = true , NoChangeArrowheads = true } )
119131 } ,
120132 new pic . BlipFill (
121- new a . Blip ( ) { Embed = iinfo . ImagePartId } ,
133+ CreateBlip ( iinfo ) ,
122134 new a . SourceRectangle ( ) ,
123135 new a . Stretch (
124136 new a . FillRectangle ( ) ) ) ,
@@ -128,10 +140,14 @@ class ImageExpression(IHtmlImageElement node) : ImageExpressionBase(node)
128140 new a . Extents ( ) { Cx = widthInEmus , Cy = heightInEmus } ) ,
129141 new a . PresetGeometry (
130142 new a . AdjustValueList ( )
131- ) { Preset = a . ShapeTypeValues . Rectangle }
132- ) { BlackWhiteMode = a . BlackWhiteModeValues . Auto } )
133- ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" } )
134- ) { DistanceFromTop = ( UInt32Value ) 0U , DistanceFromBottom = ( UInt32Value ) 0U , DistanceFromLeft = ( UInt32Value ) 0U , DistanceFromRight = ( UInt32Value ) 0U }
143+ )
144+ { Preset = a . ShapeTypeValues . Rectangle }
145+ )
146+ { BlackWhiteMode = a . BlackWhiteModeValues . Auto } )
147+ )
148+ { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" } )
149+ )
150+ { DistanceFromTop = ( UInt32Value ) 0U , DistanceFromBottom = ( UInt32Value ) 0U , DistanceFromLeft = ( UInt32Value ) 0U , DistanceFromRight = ( UInt32Value ) 0U }
135151 ) ;
136152
137153 return img ;
@@ -147,11 +163,28 @@ private static int GetDimension(HtmlAttributeCollection styles, string primarySt
147163
148164 if ( unit . IsValid )
149165 {
150- return unit . Type == UnitMetric . Percent ?
166+ return unit . Type == UnitMetric . Percent ?
151167 ( int ) ( unit . Value * percentageBase / 100 ) :
152168 unit . ValueInPx ;
153169 }
154170
155171 return 0 ;
156172 }
157- }
173+
174+ /// <summary>
175+ /// Creates a Blip element with either an embedded or external image reference.
176+ /// </summary>
177+ private static a . Blip CreateBlip ( HtmlImageInfo iinfo )
178+ {
179+ if ( iinfo . IsExternal )
180+ {
181+ // Use Link property for external images
182+ return new a . Blip ( ) { Link = iinfo . ImagePartId } ;
183+ }
184+ else
185+ {
186+ // Use Embed property for embedded images (default behaviour)
187+ return new a . Blip ( ) { Embed = iinfo . ImagePartId } ;
188+ }
189+ }
190+ }
0 commit comments