Change format of image(WPicture) in Syncfusion DocIo .NetCore

Viewed 353

I have an .net core 2 application. I use syncfusion, the docIO libraries for working with word documents. I have a word document and i want to change format of all images inside the document to .png

I have found the WPicture objects iterating over the paragraphs:

 if (paragraphItem is WPicture)
 {
   var wpicture = paragraphItem as WPicture;
   var imageBytes = wpicture.ImageBytes;

 }

How can i change the format of WPicture object?

1 Answers

Essential DocIO doesn’t have a direct API to change image format. Since System.Drawing namespace is not available in ASP.NET Core platform you need to use any one of the alternative image processing library (as mentioned in MSDN) to change the image format of the picture.

Here, the below example code used the CoreCompat helper library to change the image format:

   WPicture picture = item as WPicture;
  //Load the DocIO WPicture image bytes into CoreCompat Image instance.
  Image image = Image.FromStream(new MemoryStream(picture.ImageBytes));
  //Check image format, if format is other than png then convert the image as png format.
  if (!image.RawFormat.Equals(ImageFormat.Png))
  {
      MemoryStream imageStream = new MemoryStream();
      image.Save(imageStream, ImageFormat.Png);
      //Load the png format image into DocIO WPicture instance.
      picture.LoadImage(imageStream);
      imageStream.Dispose();
   }
   //Resize the picture width and height.
   picture.Width = 400;
   picture.Height = 400;
Related