Exception when saving an imagefile

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bitten
    New Member
    • Apr 2022
    • 2

    Exception when saving an imagefile

    Hello.
    I have a problem-saving an image file using c# in Windows Forms App. Environment MS visual and windows 11.

    Clip of code where exception appears.(I clipped what I believe is the involved part).
    Code:
     private void saveImage2()
            {
                if (System.IO.File.Exists(@"D:\bilder\temp.png"))
                      System.IO.File.Delete(@"D:\bilder\temp.png");
               MG.bmp.Save(@"D:\bilder\temp.png", ImageFormat.Png); // exception appears at this line.
                // Dispose of the image files.
              //  MG.bmp.Dispose();
            }
    The MG.bmp is a global variable Bitmap bmp which is located in the internal class.(MG).
    I use it every time I update image content and others.

    Here is the exception text:
    System.NullRefe renceException
    HResult=0x80004 003
    Message=Object reference not set to an instance of an object.
    Source=ark
    StackTrace:
    at ark.Form1.saveI mage2() in C:\Users\xxxxx\ source\repos\bo k\ark\Form1.cs: line 1150

    Can someone kindly help me get the exception devil off my back after 5 hours of knocking my head?
    Thank You.
Working...