0

This question has been asked multiples times but none has helped.. I am trying to draw a rectangle on a image and save the image but while save its throwing "A generic error occurred in GDI+"

   Bitmap bm = new Bitmap(path);
   // Draw the rectangle.
   using (Graphics gr = Graphics.FromImage(bm))
   {
        Pen blackPen = new Pen(Color.Red, 103);
        gr.DrawRectangle(blackPen, x1, y1, Width, Height);
        bm.Save(path);
   }

Even when trying to delete the file before saving it throws "the file has been used by another proccess".

Beena Gupta
  • 178
  • 12
  • _This question has been asked multiples times_ right. _but none has helped_ [wrong](https://stackoverflow.com/questions/56943416/how-to-fix-this-error-a-generic-error-occurred-in-gdi/56947586#56947586). – TaW Jun 05 '20 at 06:58
  • @Aless : How would that make a difference? – TaW Jun 05 '20 at 06:59
  • 1
    Doubtful. Overwriting an image file while having a reference to it is a common error.. – TaW Jun 05 '20 at 07:04
  • 1
    @TaW oh I see, **Use this constructor to open images with the following file formats: BMP, GIF, EXIF, JPG, PNG and TIFF. For more information about supported formats, see Types of Bitmaps. The file remains locked until the Bitmap is disposed.** – Alessandro D'Andria Jun 05 '20 at 07:08
  • 1
    Right. But it should be noted, that just about any other way to get at an image will have the same lock. Imo the easiest way is to put the bmp intoa using clause and write a temporary 2nd bitmap.. – TaW Jun 05 '20 at 07:12
  • Even I tried it dispose it and then save it again but even that throws same error – Beena Gupta Jun 05 '20 at 07:13
  • @TaW even I have tried ur way... its throws same GUID error... – Beena Gupta Jun 05 '20 at 08:06
  • _have tried your way_ Update the question to show that new code ! - Also: If this _Even when trying to delete the file before saving it throws "the file has been used by another proccess"._ is true, then you seem to have yet another place that holds that reference. But we can only comment on code we see.. – TaW Jun 05 '20 at 08:17
  • Even I have tried that.. .this also doesnot work... Updated my question as you mentioned.. still getting same error... – Beena Gupta Jun 15 '20 at 12:34

0 Answers0