Files
docs-desktop/dotnet-desktop-guide/net/winforms/controls/how-to-add-a-picture-to-a-control.md
T
Andy De GeorgeandGenevieve Warren 184e6c9a0c Initial publish of WinForms for .NET 5. (#96)
* Migrate inprogress winforms contnet from docs

* Fix preview note

* Fix vb code

* Minor fixes to keyboard articles

* Minor adjustment to overview

* Test redirects for 4.0 -> 5.0

* adjust links

* Add mouse input section winforms (#81)

* Update projects to .NET 5

* Update keyboard desc

* Finish mouse events

* Add remaining mouse articles;code

* finish mouse input

* minor fixes

* Remove branch restriction (#82)

* Update build-validation.yml (#84)

* Update build-validation.yml (#85)

* Fix vb proj

* Add WinForms tutorial (#91)

* redirect between overview

* New create an app tutorial

* Fix markdown

* Fix preserve view setting

* Add forms articles (#93)

* Add forms articles

* Fix warnings

* Winforms publish (#95)

* Prep TOC for publish

* Updated date

* Automatic how-to topic type

* Update desc/headers

* Fix links

* Fix build errors

* Add preview note

* Update toc landing page

* Convert old style project files

* update download links

* Apply suggestions from code review

Co-authored-by: Genevieve Warren <[email protected]>

* Adjust number key

* Update dotnet-desktop-guide/net/winforms/overview/index.md

* Try toc position task via bookmark

* Improve images

* Remove sentence

* File redirects

Co-authored-by: Genevieve Warren <[email protected]>
2020-10-28 10:44:16 -07:00

56 lines
2.6 KiB
Markdown

---
title: Display an image on a control
description: Learn how to display an image on a Windows Form control. Many controls, such as the PictureBox, can display an image.
ms.date: 10/26/2020
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "Button control [Windows Forms], images"
- "Windows Forms controls, images"
- "controls [Windows Forms], images"
- "images [Windows Forms], Windows Forms contr ols"
- "examples [Windows Forms], controls"
---
# How to display an image on a control (Windows Forms .NET)
Several Windows Forms controls can display images. These images can be icons that clarify the purpose of the control, such as a diskette icon on a button denoting the Save command. Alternatively, the icons can be background images to give the control the appearance and behavior you want.
[!INCLUDE [desktop guide under construction](../../includes/desktop-guide-preview-note.md)]
## Designer
In the **Properties** window of Visual Studio, select the **Image** or **BackgroundImage** property of the control, and then select the ellipsis (![Ellipsis button in Visual Studio](../media/visual-studio-ellipsis-button.png)) to display the **Select Resource** dialog box and then select the image you want to display.
:::image type="content" source="media/how-to-add-a-picture-to-a-control/properties-image.png" alt-text="Properties dialog with image property selected":::
## Programmatic
Set the control's `Image` or `BackgroundImage` property to an object of type <xref:System.Drawing.Image>. Generally, you'll be loading the image from a file by using the <xref:System.Drawing.Image.FromFile%2A> method.
In the following code example, the path set for the location of the image is the **My Pictures** folder. Most computers running the Windows operating system include this directory. This also enables users with minimal system access levels to run the application safely. The following code example requires that you already have a form with a <xref:System.Windows.Forms.PictureBox> control added.
```csharp
// Replace the image named below with your own icon.
// Note the escape character used (@) when specifying the path.
pictureBox1.Image = Image.FromFile
(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.MyPictures)
+ @"\Image.gif");
```
```vb
' Replace the image named below with your own icon.
PictureBox1.Image = Image.FromFile _
(System.Environment.GetFolderPath _
(System.Environment.SpecialFolder.MyPictures) _
& "\Image.gif")
```
## See also
- <xref:System.Drawing.Image.FromFile%2A>
- <xref:System.Drawing.Image>
- <xref:System.Windows.Forms.Control.BackgroundImage%2A>