Hi RPA,
Refer below example.
Design
Add ComboBox and ImageList control to the Form.
Namespaces
C#
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Net;
VB.Net
Imports System.Drawing
Imports System.Globalization
Imports System.IO
Imports System.Net
Code
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string[] img;
private void Form1_Load(object sender, EventArgs e)
{
string[] ct = new string[] {"Bangladesh", "Brazil", "China", "Congo",
"Egypt", "Ethiopia", "France", "Germany",
"India", "Indonesia", "Iran", "Iraq", "Italy",
"Japan", "Korea", "Mexico", "Myanmar", "Nigeria",
"Pakistan", "Philippines", "Russian Federation",
"South Africa", "Spain", "Thailand", "Turkey",
"United Kingdom", "United States", "Viet Nam"};
for (int i = 0; i < ct.Length; i++)
{
if (!string.IsNullOrEmpty(GetCountryCode(ct[i])))
{
imageList1.Images.Add(DownloadImage("http://www.geognos.com/api/en/countries/flag/" + GetCountryCode(ct[i]) + ".png"));
}
}
img = new string[imageList1.Images.Count];
for (int i = 0; i < imageList1.Images.Count; i++)
{
img[i] = ct[i];
}
comboBox1.Items.AddRange(img);
comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.Width = imageList1.ImageSize.Width + 120;
comboBox1.MaxDropDownItems = 4;
}
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index != -1)
{
e.Graphics.DrawImage(imageList1.Images[e.Index], e.Bounds.Left, e.Bounds.Top, 50, 50);
e.Graphics.DrawString(img[e.Index], e.Font, new SolidBrush(Color.Red), 55, e.Bounds.Top + 2);
}
}
private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = imageList1.ImageSize.Height + 50;
e.ItemWidth = imageList1.ImageSize.Width + 50;
}
public string GetCountryCode(string country)
{
string countryCode = "";
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo culture in cultures)
{
RegionInfo region = new RegionInfo(culture.LCID);
if (region.EnglishName.ToUpper().Contains(country.ToUpper()))
{
countryCode = region.TwoLetterISORegionName;
}
}
return countryCode;
}
public Image DownloadImage(string fromUrl)
{
using (WebClient webClient = new WebClient())
{
using (Stream stream = webClient.OpenRead(fromUrl))
{
return Image.FromStream(stream);
}
}
}
}
VB.Net
Public Class Form1
Public Sub New()
InitializeComponent()
End Sub
Private img As String()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim ct As String() = New String() {"Bangladesh", "Brazil", "China", "Congo",
"Egypt", "Ethiopia", "France", "Germany",
"India", "Indonesia", "Iran", "Iraq", "Italy",
"Japan", "Korea", "Mexico", "Myanmar", "Nigeria",
"Pakistan", "Philippines", "Russian Federation",
"South Africa", "Spain", "Thailand", "Turkey",
"United Kingdom", "United States", "Viet Nam"}
For i As Integer = 0 To ct.Length - 1 Step 1
If Not String.IsNullOrEmpty(GetCountryCode(ct(i))) Then
imageList1.Images.Add(DownloadImage("http://www.geognos.com/api/en/countries/flag/" & GetCountryCode(ct(i)) & ".png"))
End If
Next
img = New String(imageList1.Images.Count - 1) {}
For i As Integer = 0 To imageList1.Images.Count - 1 Step 1
img(i) = ct(i)
Next
comboBox1.Items.AddRange(img)
comboBox1.DrawMode = DrawMode.OwnerDrawVariable
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList
comboBox1.Width = imageList1.ImageSize.Width + 120
comboBox1.MaxDropDownItems = 4
End Sub
Private Sub comboBox1_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles comboBox1.DrawItem
If e.Index <> -1 Then
e.Graphics.DrawImage(imageList1.Images(e.Index), e.Bounds.Left, e.Bounds.Top, 50, 50)
e.Graphics.DrawString(img(e.Index), e.Font, New SolidBrush(Color.Red), 55, e.Bounds.Top + 2)
End If
End Sub
Private Sub comboBox1_MeasureItem(ByVal sender As Object, ByVal e As MeasureItemEventArgs) Handles comboBox1.MeasureItem
e.ItemHeight = imageList1.ImageSize.Height + 50
e.ItemWidth = imageList1.ImageSize.Width + 50
End Sub
Public Function GetCountryCode(ByVal country As String) As String
Dim countryCode As String = ""
Dim cultures As CultureInfo() = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
For Each culture As CultureInfo In cultures
Dim region As RegionInfo = New RegionInfo(culture.LCID)
If region.EnglishName.ToUpper().Contains(country.ToUpper()) Then
countryCode = region.TwoLetterISORegionName
End If
Next
Return countryCode
End Function
Public Function DownloadImage(ByVal fromUrl As String) As Image
Using webClient As WebClient = New WebClient()
Using stream As Stream = webClient.OpenRead(fromUrl)
Return Image.FromStream(stream)
End Using
End Using
End Function
End Class
Screenshot