Remote Link Access Protection

I have an image display control similar to the PictureBox control, it takes a bitmap and displays it. The problem is that my control will naively try to perform operations that require it to access this image field, even if the bitmap that was set was deleted. So, let's say I have a form with my control over the display of the image on it. I set the Image property for the image loaded from the file, and then for some reason (no matter why and how) the image is located. Then I do something to cause the image control to be redrawn, and when it tries to access the bitmap that is now located, it throws an InvalidArgumentException left and right. I understand the problem, but not 100% sure of the solution.

My image display control cannot control whether the link to the image will be deleted by the calling code, and there is no public way that I know to check if the object is located. I can imagine two possible solutions:

  • Save a copy of the bitmap in my image control (i.e. clone () of the provided image). Then it does not matter what happens to the original image. I think this is the best solution.
  • Wrap EVERYTHING that I can think of in try / catch statements is a terrible option, I only enable it so that someone else doesn't offer it.

Before implementing option # 1 (the one that seems to make the most sense), I thought I'd ask here if there is best practice for solving this scenario.

+3
2

, ImageViewer # WinForms. , , , , .

- / - , , . , .

+2

- , .

, , , .

0

Source: https://habr.com/ru/post/1773702/


All Articles