I have been working in VB.NET for about a year now (upgrading old VB6 projects, ugh), and a lot of my work involves placing controls on background images to simulate an HMI (Human-Machine Interface). A huge part of this is editing the background of the form (as well as other Image controls). My issue with VS is that when I set an image to a control/form, VS locks the image file. When I need to adjust my image, after test fitting it on the form, I am forced to restart VS to release the lock and overwrite the image file.
I've searched Google and found a mention of an issue like this in VS2005, so it has been a problem for a while now. I wasn't able to find a setting to change in VS and there really wasn't much help from the internet either, so I'll post my solution here.
I hope this helps somebody.
The solution was pretty simple (in my case) and I got the idea from this blog :
Even at design time VS executes some of the form/control code when you load the control/form (ie, OnLoad, Paint
, etc), so I created an simple override to the Image property to alter how the image was loaded. Obviously I had a class derived from Windows.Forms
to work with:
Public Class SGBase
Inherits System.Windows.Forms.Form
' Irrelevent code...
' ...
Public Overrides Property BackgroundImage As Image
Get
Return MyBase.BackgroundImage
End Get
Set(value As Image)
If MyBase.BackgroundImage IsNot Nothing Then MyBase.BackgroundImage.Dispose()
MyBase.BackgroundImage = New Bitmap(value)
End Using
End Set
End Property
End Class
The underlying issue is that when you load an image into a form/control, the VS IDE locks the file and sets it to the BackgroundImage
reference. My solution utilizes the Using
statement to prevent file lock and I make sure to properly dispose of and create a new instance. VS runs this code at design time every time you refresh/change the form.
VOILA!
Another simple way would be to choose 'Project Resource File' in the 'select resource' dialogue instead of choosing local resource. this way VS creates a duplicate of your file in the 'Resources' folder and u can open it from that folder with other software. Photoshop for example. Another advantage of using this method is that u can ship all the required resources with your project source for later edits.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.