Why WPicture.Dispose() doesn't dispose the loaded picture in .NET MVC FileFormat Libraries ?
In .NET DocIO, our older versions of Essential® Studio® (before 10.3.0.53) 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. 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 a System.Drawing.Image instance. Kindly dispose of the System.Drawing.Image instance explicitly before attempting to delete the image file in later versions.
Below code snippets 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 file system Image image = Image.FromFile(imagePath); //Sets the image to the PictureWatermark watermark.Picture = image; document.Watermark = watermark; //Dispose 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 file system
Dim image As Image = Image.FromFile(imagePath)
'Sets the image to the PictureWatermark
watermark.Picture = image
document.Watermark = watermark
'Dispose the image
image.Dispose()
'Delete the image file
imageFile.Delete()
Below code snippets demonstrate how to delete the image file after loading it as a picture in Word documents.
C#:
//Gets image file FileInfo imageFile = new FileInfo(imagePath); //Loads image from file system Image image = Image.FromFile(imagePath); //Appends picture to the paragraph IWPicture picture = paragraph.AppendPicture(image); //Dispose the image image.Dispose(); //Delete the image file imageFile.Delete();
VB:
Dim watermark As New PictureWatermark()
'Get image file
Dim imageFile As New FileInfo(imagePath)
'Loads image from file system
Dim image As Image = Image.FromFile(imagePath)
'Appends picture to the paragraph
Dim picture As IWPicture = paragraph.AppendPicture(image)
'Dispose the image
image.Dispose()
'Delete the image file
imageFile.Delete()
Conclusion
I hope you enjoyed learning about why WPicture.Dispose() doesn't dispose of the loaded picture in .NET MVC FileFormat Libraries.
You can refer to our ASP.NET MVC DocIO feature tour page to learn about its other groundbreaking features. You can also explore our ASP.NET MVC DocIO documentation to understand how to present and manipulate data.
For current customers, you can check out our ASP.NET MVC components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our ASP.NET MVC DocIO and other ASP.NET MVC components.
If you have any queries or require clarifications, please let us know in the comments below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!