Problem Trying to Rewrite a File Png Images

Marino Liranzo 81 Reputation points
2023-11-11T00:28:23.3166667+00:00

Hello forum friend,

I have this little problem that although there are many questions on the subject, none of them fit my particular problem. The problem is the following.

I have a graph made with wpfPlot.plot.SaveFig(), this graph is generated in a first process and the file is created correctly, in another process the graph must be overwritten with different data; then an error message 'A generic error occurred in GDI+.'

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,700 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,521 questions
{count} votes

2 answers

Sort by: Most helpful
  1. KOZ6.0 6,210 Reputation points
    2023-11-11T02:48:17.89+00:00

    Image files may be locked depending on how they are displayed. In this example, the CacheOption property of BitmapImage is set to BitmapCacheOption.OnLoad. This caches the image in memory when it's loaded, preventing the original file from being locked.

    BitmapImage bitmap = new BitmapImage();
    bitmap.BeginInit();
    bitmap.UriSource = new Uri(imagePath, UriKind.RelativeOrAbsolute);
    bitmap.CacheOption = BitmapCacheOption.OnLoad;
    bitmap.EndInit();
    imageControl.Source = bitmap;
    

  2. Marino Liranzo 81 Reputation points
    2023-11-12T14:28:35.9766667+00:00

    I have tried using the 'File.Delete(path)' method before using wpfPlot.SaveFig(path); however, the file is not deleted.

    0 comments No comments