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+.'

Developer technologies Windows Presentation Foundation
Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. KOZ6.0 6,655 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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.