.NET7を利用して、ソフトウェアを作成しています。
イメージファイルをResources.resxに追加したのですが、
Properties.Resources.から呼び出そうとしたら、定義がありませんとエラーが出ました。
追加したら勝手に定義がされるものだと思っていたのですが、Resourcesを見ると確かに定義がありませんでした。
手動で勝手に定義を追加してもよいのでしょうか?
また、なぜ定義が自動的に行われなかったのでしょうか?
試したこと
1.VisualStudio2022で開発中のC#.NETにて、Resources.resxを開く。
2.デスクトップから対象の画像(pic1)をD&Dで追加する。
3.Form1にあるImageBoxで表示するために以下のコードを追加した。
public Form1() { InitializeComponent(); this.imageBox.Image = Properties.Resources.pic1 }
4.「'Resources'に'pic1'の定義がありません。」と表示される。
以下、csprojファイル
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net7.0-windows</TargetFramework> <OutputType>WinExe</OutputType> <PublishUrl>publish\</PublishUrl> <Install>true</Install> <InstallFrom>Disk</InstallFrom> <UpdateEnabled>false</UpdateEnabled> <UpdateMode>Foreground</UpdateMode> <UpdateInterval>7</UpdateInterval> <UpdateIntervalUnits>Days</UpdateIntervalUnits> <UpdatePeriodically>false</UpdatePeriodically> <UpdateRequired>false</UpdateRequired> <MapFileExtensions>true</MapFileExtensions> <ApplicationRevision>0</ApplicationRevision> <ApplicationVersion>1.0.0.%2a</ApplicationVersion> <IsWebBootstrapper>false</IsWebBootstrapper> <UseApplicationTrust>false</UseApplicationTrust> <BootstrapperEnabled>true</BootstrapperEnabled> <GenerateAssemblyInfo>false</GenerateAssemblyInfo> <UseWindowsForms>true</UseWindowsForms> <ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets> </PropertyGroup> <PropertyGroup> <TargetZone>LocalIntranet</TargetZone> </PropertyGroup> <PropertyGroup> <GenerateManifests>true</GenerateManifests> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> <OutputPath>bin\x64\Debug\ImageRenderApp</OutputPath> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> <OutputPath>bin\x64\Release\</OutputPath> </PropertyGroup> <PropertyGroup> <ApplicationManifest>Properties\app.manifest</ApplicationManifest> </PropertyGroup> <PropertyGroup> <ApplicationIcon>Resources\myIcon.ico</ApplicationIcon> </PropertyGroup> <ItemGroup> <BootstrapperPackage Include=".NETFramework,Version=v4.7.2"> <Visible>False</Visible> <ProductName>Microsoft .NET Framework 4.7.2 %28x86 および x64%29</ProductName> <Install>true</Install> </BootstrapperPackage> <BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1"> <Visible>False</Visible> <ProductName>.NET Framework 3.5 SP1</ProductName> <Install>false</Install> </BootstrapperPackage> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> <PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" /> <PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.4.421302"> <PrivateAssets>all</PrivateAssets> </PackageReference> <PackageReference Include="Microsoft.Windows.Compatibility" Version="7.0.1" /> </ItemGroup> <ItemGroup> <Compile Update="Properties\Settings.Designer.cs"> <DesignTimeSharedInput>True</DesignTimeSharedInput> <AutoGen>True</AutoGen> <DependentUpon>Settings.settings</DependentUpon> </Compile> </ItemGroup> <ItemGroup> <None Update="Properties\Settings.settings"> <Generator>SettingsSingleFileGenerator</Generator> <LastGenOutput>Settings.Designer.cs</LastGenOutput> </None> </ItemGroup> </Project>