11using Syncfusion . Presentation ;
22using Syncfusion . PresentationRenderer ;
33using Syncfusion . Pdf ;
4+ using System . Drawing ;
5+ using System . Drawing . Imaging ;
46using Image = System . Drawing . Image ;
57using ImageFormat = System . Drawing . Imaging . ImageFormat ;
68
@@ -83,31 +85,28 @@ public static void IterateShapes(IShapes shapes)
8385 {
8486 case SlideItemType . Picture :
8587 IPicture picture = shape as IPicture ;
86-
87- Image image = Image . FromStream ( new MemoryStream ( picture . ImageData ) ) ;
88- if ( image . RawFormat . Equals ( ImageFormat . Emf ) )
88+ if ( picture . ImageData != null )
8989 {
90- double height = picture . Height ;
91- double width = picture . Width ;
92- using ( FileStream imgFile = new FileStream ( "Output.png" , FileMode . OpenOrCreate , FileAccess . ReadWrite ) )
90+ Image image = Image . FromStream ( new MemoryStream ( picture . ImageData ) ) ;
91+ if ( image . RawFormat . Equals ( ImageFormat . Emf ) )
9392 {
94- image . Save ( imgFile , ImageFormat . Png ) ;
93+ // Store original properties
94+ double left = picture . Left ;
95+ double top = picture . Top ;
96+ double width = picture . Width ;
97+ double height = picture . Height ;
98+ // Convert EMF to PNG stream
99+ MemoryStream stream ;
100+ stream = ConvertEmfToPng ( picture . ImageData ) ;
101+
102+ // Add replacement picture
103+ IPicture newPicture = shapes . AddPicture ( stream , left , top , width , height ) ;
104+ newPicture . Left = left ;
105+ newPicture . Top = top ;
106+ // Remove original EMF picture
107+ shapes . Remove ( picture ) ;
95108 image . Dispose ( ) ;
96109 }
97-
98- using ( FileStream imageStream = new FileStream ( @"Output.png" , FileMode . Open , FileAccess . ReadWrite ) )
99- {
100- //Creates instance for memory stream
101- using ( MemoryStream memoryStream = new MemoryStream ( ) )
102- {
103- //Copies stream to memoryStream.
104- imageStream . CopyTo ( memoryStream ) ;
105- //Replaces the existing image with new image.
106- picture . ImageData = memoryStream . ToArray ( ) ;
107- picture . Height = height ;
108- picture . Width = width ;
109- }
110- }
111110 }
112111 break ;
113112 case SlideItemType . GroupShape :
@@ -126,23 +125,36 @@ public static void IterateShapes(IShapes shapes)
126125 Image fillImage = Image . FromStream ( new MemoryStream ( pictureFill . ImageBytes ) ) ;
127126 if ( fillImage . RawFormat . Equals ( ImageFormat . Emf ) )
128127 {
129- using ( FileStream imgFile = new FileStream ( "Output.png" , FileMode . OpenOrCreate , FileAccess . ReadWrite ) )
130- {
131- fillImage . Save ( imgFile , ImageFormat . Png ) ;
132- fillImage . Dispose ( ) ;
133- }
134-
135- using ( FileStream imageStream = new FileStream ( @"Output.png" , FileMode . Open , FileAccess . ReadWrite ) )
136- {
137- //Creates instance for memory stream
138- using ( MemoryStream memoryStream = new MemoryStream ( ) )
139- {
140- //Copies stream to memoryStream.
141- imageStream . CopyTo ( memoryStream ) ;
142- //Replaces the existing image with new image.
143- pictureFill . ImageBytes = memoryStream . ToArray ( ) ;
144- }
145- }
128+ byte [ ] imageBytes = shape . Fill . PictureFill . ImageBytes ;
129+
130+ using Image image = Image . FromStream ( new MemoryStream ( imageBytes ) ) ;
131+
132+ // Process only EMF images
133+ if ( image . RawFormat . Guid != ImageFormat . Emf . Guid )
134+ return ;
135+
136+ // Convert EMF -> PNG
137+ using MemoryStream pngStream = ConvertEmfToPng ( imageBytes ) ;
138+
139+ // Save existing properties
140+ double left = shape . Left ;
141+ double top = shape . Top ;
142+ double width = shape . Width ;
143+ double height = shape . Height ;
144+
145+ // Create replacement shape
146+ IShape newShape = shapes . AddShape ( shape . AutoShapeType , left , top , width , height ) ;
147+
148+ // Apply PNG picture fill
149+ newShape . Fill . FillType = FillType . Picture ;
150+ newShape . Fill . PictureFill . ImageBytes = pngStream . ToArray ( ) ;
151+
152+ // Copy rotation
153+ newShape . Rotation = shape . Rotation ;
154+
155+ // Remove original shape
156+ shapes . Remove ( shape ) ;
157+
146158 }
147159 }
148160 break ;
@@ -157,38 +169,82 @@ public static void IterateShapes(IShapes shapes)
157169 double left = oleObject . Left ;
158170 double top = oleObject . Top ;
159171
160- FileStream imgFile = new FileStream ( "Output.png" , FileMode . OpenOrCreate , FileAccess . ReadWrite ) ;
161- oleImage . Save ( imgFile , ImageFormat . Png ) ;
162-
172+ // Convert preview image to PNG stream
173+ MemoryStream pngStream = new MemoryStream ( ) ;
174+ oleImage . Save ( pngStream , ImageFormat . Png ) ;
175+ pngStream . Position = 0 ;
163176
164- //Gets the data of Ole object.
165- byte [ ] array = oleObject . ObjectData ;
166- //Convert Ole object data into memory system
167- MemoryStream objectStream = new MemoryStream ( array ) ;
177+ // Get OLE data
178+ byte [ ] objectData = oleObject . ObjectData ;
179+ MemoryStream objectStream = new MemoryStream ( objectData ) ;
168180
169- //Gets the ProgID of OLE Object
181+ // Get ProgID
170182 string progID = oleObject . ProgID ;
171183
172- //Removed the existing OLE object.
184+ // Remove the existing OLE object
173185 shapes . Remove ( oleObject ) ;
174186
175- //Add an new OLE object to the slide with PNG format image.
176- IOleObject replacedOleObject = shapes . AddOleObject ( imgFile , progID , objectStream ) ;
177- //Set size and position of the OLE object
187+ // Add a new OLE object with PNG preview image
188+ IOleObject replacedOleObject = shapes . AddOleObject ( pngStream , progID , objectStream ) ;
189+
190+ // Restore position and size
178191 replacedOleObject . Left = left ;
179192 replacedOleObject . Top = top ;
180193 replacedOleObject . Width = width ;
181194 replacedOleObject . Height = height ;
182195
183- //Dispose all the instance.
184- imgFile . Dispose ( ) ;
196+ // Dispose
185197 objectStream . Dispose ( ) ;
198+ pngStream . Dispose ( ) ;
186199 oleImage . Dispose ( ) ;
187200 }
188201
189202 break ;
190203 }
191204 }
192205 }
206+ /// <summary>
207+ /// Convert EMF to PNG
208+ /// </summary>
209+ private static MemoryStream ConvertEmfToPng ( byte [ ] emfBytes )
210+ {
211+ using MemoryStream emfStream = new MemoryStream ( emfBytes ) ;
212+ using Metafile metafile = new Metafile ( emfStream ) ;
213+
214+ GraphicsUnit pageUnit = GraphicsUnit . Pixel ;
215+ RectangleF bounds = metafile . GetBounds ( ref pageUnit ) ;
216+
217+ int width = ( int ) Math . Ceiling ( bounds . Width ) ;
218+ int height = ( int ) Math . Ceiling ( bounds . Height ) ;
219+
220+ MemoryStream pngStream = new MemoryStream ( ) ;
221+
222+ using ( Bitmap bitmap = new Bitmap ( width , height ) )
223+ {
224+ bitmap . SetResolution ( 300 , 300 ) ;
225+
226+ using ( Graphics graphics = Graphics . FromImage ( bitmap ) )
227+ {
228+ graphics . Clear ( Color . Transparent ) ;
229+
230+ graphics . SmoothingMode =
231+ System . Drawing . Drawing2D . SmoothingMode . HighQuality ;
232+ graphics . InterpolationMode =
233+ System . Drawing . Drawing2D . InterpolationMode . HighQualityBicubic ;
234+ graphics . PixelOffsetMode =
235+ System . Drawing . Drawing2D . PixelOffsetMode . HighQuality ;
236+
237+ graphics . DrawImage (
238+ metafile ,
239+ new Rectangle ( 0 , 0 , width , height ) ) ;
240+ }
241+
242+ bitmap . Save ( pngStream , ImageFormat . Png ) ;
243+ }
244+
245+ pngStream . Position = 0 ;
246+ return pngStream ;
247+
248+ }
193249 }
194250}
0 commit comments