问题
I have an image applied to a control in Visual Studio (in this case a PictureBox, but it could be a Button or any number of other controls). I would like to edit this image in a 3rd party program, but VS will not let any other program access the file while it is open. Even if I temporarily change the graphic to something else so that the image I want to save is no longer being used, VS will still not relinquish control of the file.
So instead I have to shut down the entire project in order to save the file in the other program, then start it up and reload the project, every time I want to make a change. Needless to say, this is very time-consuming and I would prefer to be able to edit the graphics used in the program while VS is running instead of shutting it down and reloading every time.
(I don't need the project to be running - I wouldn't expect that to be changeable at runtime. But it should at least be possible to swap out graphics without completely shutting down VS...)
回答1:
Question summary:
Allowing the automatic update of Visual Studio form designer interface after a WinForm Control's underlying Image source file has been modified.
Problem reported:
When the Control's Image property is assigned using a Bitmap file, GDI+ locks the file, thus hindering any modification attempt to the source file.
This problem can be overcome loading the images as Project Resources.
- Store your Bitmap files inside the default Project
Resources
folder.
This directory is automatically created as soon as an Image file is added to a Project through the Project Properties → Resources → Images, but it can be created manually. - Assign the image from a Resource Image.
When the Image is assigned this way, the Bitmap file will not be locked. - When the underlying Bitmap file is edited (with an external graphics program or using the built-in editor), Visual Studio will update a Control's Image as soon as it acknowledge the modification.
This usually happens in real-time.
Notes:
- When adding a Bitmap file to VS Resource, the original Bitmap format is preserved.
- Verify that the Persistece
property of the Bitmap resource is set to Linked at compile time
.
Possible problems/drawbacks.
Occasionally, an issue with resource caching may arise: the resource Image, when trying to apply it to a Control's Image property, appears to be stuck to an older/previous version of the Bitmap file.
No change to the Image resource or file seems to correct the problem.
When the the Project is Run, the correct image is shown, but the design-time Image appearance doesn't change (is the "wrong" one).
This is just a resource indexing issue.
It may happen, mostly, if you try to modify a control underlying Image while in debug mode or the Image is removed from the Project Resources and then added again, modified.
To solve the problem:
- Close the affected Solution/Project (and the Visual Studio instance).
- Delete all build file in the
../Bin/Debug
directory (the .exe file, its associated .pdb file and the exe.config file). - Delete all files in the
../Obj/Debug
directory.
These files can be deleted without affecting the application functionality. - Re-open the Solution/Project and Clean/Rebuild.
This will reset the cache and the Design-Time form interface will be updated with the corrent Images.
.
来源:https://stackoverflow.com/questions/51218586/edit-image-used-in-winforms-without-closing-vs