Why WPicture.Dispose() doesn't dispose the loaded picture in LightSwitch Silverlight FileFormat Libraries ?
In our older versions of Essential® Studio® (before 10.3.0.53), we preserved the System.Drawing.Image instance as a reference in WPicture and PictureWatermark. Therefore, it is necessary to dispose of the System.Drawing.Image instance preserved in the WPicture object before deleting the image file. However, in later versions (from 10.3.0.53), we optimized memory utilization by preserving a single reference of an image for the entire document as image bytes instead of the System.Drawing.Image instance. Kindly dispose of the System.Drawing.Image instance explicitly before trying to delete the image file in later versions.
Below are code snippets that demonstrate how to delete the image file after setting it as a picture watermark in Word documents.
C#:
PictureWatermark watermark = new PictureWatermark();
// Gets the image file
FileInfo imageFile = new FileInfo(imagePath);
// Loads image from the file system
Image image = Image.FromFile(imagePath);
// Sets the image to the PictureWatermark
watermark.Picture = image;
document.Watermark = watermark;
// Dispose of the image
image.Dispose();
// Delete the image file
imageFile.Delete();
VB:
Dim watermark As New PictureWatermark()
' Gets the image file
Dim imageFile As New FileInfo(imagePath)
' Loads image from the file system
Dim image As Image = Image.FromFile(imagePath)
' Sets the image to the PictureWatermark
watermark.Picture = image
document.Watermark = watermark
' Dispose of the image
image.Dispose()
' Delete the image file
imageFile.Delete()
Below code snippets demonstrates how to delete the image file after loading it as picture in Word documents.
C#:
// Gets the image file
FileInfo imageFile = new FileInfo(imagePath);
// Loads image from the file system
Image image = Image.FromFile(imagePath);
// Appends picture to the paragraph
IWPicture picture = paragraph.AppendPicture(image);
// Dispose of the image
image.Dispose();
// Delete the image file
imageFile.Delete();
VB:
Dim watermark As New PictureWatermark()
' Get the image file
Dim imageFile As New FileInfo(imagePath)
' Loads image from the file system
Dim image As Image = Image.FromFile(imagePath)
' Appends picture to the paragraph
Dim picture As IWPicture = paragraph.AppendPicture(image)
' Dispose of the image
image.Dispose()
' Delete the image file
imageFile.Delete()