VB.net or C#, How to load an image into a picturebox during form load + load a picture from a relative directory

T Duck 0 Reputation points
2023-02-21T16:37:54.59+00:00

Hi everybody,

I need some help with 2 things Using either VB.Net or C# please.

#1. How to load an image/picture from form load into either form1 or a picturebox.

#2. How to load an image/picture into a picturebox or form1 from a relative directory.

I used to do this from VB6 using the following code.

MyPath = CurDir()

'This code loads all the small kanji pictures.

Image1(1).Picture = LoadPicture(MyPath + "\Icon_Conversation_Pictures\Step_01\Body\Small_Kan\001.jpg")

Image1(2).Picture = LoadPicture(MyPath + "\Icon_Conversation_Pictures\Step_01\Body\Small_Kan\002.jpg")

Your help will be greatly appreciated.

Thank You Very Much!

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,842 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 120.5K Reputation points
    2023-02-21T20:06:28.2333333+00:00

    To load a picture from file in C#:

    img1.SetValue( Image.SourceProperty, new ImageSourceConverter( ).ConvertFromString( @"C:\MyFiles\SomeImage.png" ) );
    

    where img1 is a picture control:

    <Image x:Name="img1" />
    

    To load it from a subfolder (e.g. "Images") of the executable using relative path:

    img1.SetValue( Image.SourceProperty, new ImageSourceConverter( ).ConvertFromString( System.IO.Path.Combine( System.IO.Path.GetDirectoryName( Assembly.GetEntryAssembly( ).Location ), @"Images\SomeImage.png" ) ) );
    

    There are different approaches.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.