Quantcast
Channel: Convert "String" to "System.Drawing.Image" in Visual C#
Viewing all articles
Browse latest Browse all 5

Convert "String" to "System.Drawing.Image" in Visual C#

$
0
0

Hi ce2006121,

Welcome to MSDN Forum.

Please try the following code sample to randomly select an image from ImageList for PictureBox control.

Prerequisites: Drag ImageList1, PictureBox1 and Button1 onto Form1.

using System.Drawing;

 

   publicpartialclassForm1 : Form

   {

 

       privatevoid button1_Click(object sender,EventArgs e)

       {

           Image img1 = Image.FromFile(@"C:\1.jpg");

           Image img2 = Image.FromFile(@"C:\2.jpg");

           imageList1.Images.AddRange(new Image[] { img1, img2 });

           Random rnd = newRandom();

           Image choice = imageList1.Images[rnd.Next(0, imageList1.Images.Count)];

           pictureBox1.Image = choice;

       }

 

   }

 


Martin Xie [MSFT]
MSDN Community Support | Feedback to us

Viewing all articles
Browse latest Browse all 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>