question

KailashSahu-8768 avatar image
0 Votes"
KailashSahu-8768 asked DanielZhang-MSFT edited

Select Image File in ListView and save exe in c#

I need Image Selection in ListView and Save in Setting in c#.

1:- Add Images in Listview
2:- Save All IMage in a .exe
3:- Run The exe another pc it will run

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DanielZhang-MSFT edited

Hi KailashSahu-8768,
You need to add the image in the file into imageList, and then fill to the listView.
Here is a code example:

  private void Form1_Load(object sender, EventArgs e)
   {
             DirectoryInfo dir = new DirectoryInfo(@"C:\Users\Administrator\Desktop\test");
    
             foreach (FileInfo file in dir.GetFiles())
             {
                 try
                 {
                     this.imageList1.Images.Add(Image.FromFile(file.FullName));
                 }
                 catch
                 {
                     Console.WriteLine("This is not an image file");
                 }
             }
    
             this.listView1.View = View.LargeIcon;
    
             this.imageList1.ImageSize = new Size(32, 32);
    
             this.listView1.LargeImageList = this.imageList1;
    
             //or
    
             //this.listView1.View = View.SmallIcon;
    
             //this.listView1.SmallImageList = this.imageList1;
    
             for (int j = 0; j < this.imageList1.Images.Count; j++)
             {
                 ListViewItem item = new ListViewItem();
                 item.ImageIndex = j;
                 this.listView1.Items.Add(item);
             }
 }

You cannot save all the pictures at once, so you can use the ListView.SelectedIndexChanged event to save the selected picture in the listview to settings.
Setting:
Name:Image1, Type:String, Scope:Application

 private void listView1_SelectedIndexChanged(object sender, EventArgs e)
     {
         var base64 = string.Empty;
         using (MemoryStream ms = new MemoryStream())
         {
             Image image;
             foreach (ListViewItem itm in listView1.SelectedItems)
             {
                 int imgIndex = itm.ImageIndex;
                 if (imgIndex >= 0 && imgIndex < this.imageList1.Images.Count)
                 {
                     image = this.imageList1.Images[imgIndex];
                     image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                     base64 = Convert.ToBase64String(ms.ToArray());
                     Properties.Settings.Default.Image1 = base64;
                     Properties.Settings.Default.Save();
                 }
             }
         }
     }
 }

>>3:- Run The exe another pc it will run
Could you explain in detail?
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

KailashSahu-8768 avatar image
0 Votes"
KailashSahu-8768 answered DanielZhang-MSFT commented
         private void button2_Click(object sender, EventArgs e)
         {
             string str = Environment.GetFolderPath(Environment.SpecialFolder.Personal).ToString();
             try
             {
                 if (this.imageListView1.SelectedItems.Count > 0)
                 {
                     if (!Directory.Exists(str + "\\selectedimages"))
                     {
                         Directory.CreateDirectory(str + "\\selectedimages");
                     }
                     string[] files = Directory.GetFiles(str + "\\selectedimages");
                     foreach (string text in files)
                     {
                         string text2 = text.ToString();
                         if (File.Exists(text2))
                         {
                             File.Delete(text2);
                         }
                     }
                     ResizeOpts resizeOpts = new ResizeOpts();
                     resizeOpts.ShowDialog(this);
                     int percentage = IResizer.Percentage;
                     if (percentage != 0)
                     {
                         IResizer resizer = new IResizer();
                         this.progressBar2.Minimum = 1;
                         this.progressBar2.Maximum = this.imageListView1.SelectedItems.Count;
                         Rectangle clientRectangle = this.progressBar2.Parent.ClientRectangle;
                         this.progressBar2.Left = (clientRectangle.Width - this.progressBar2.Width) / 2;
                         this.progressBar2.Top = (clientRectangle.Height - this.progressBar2.Height) / 2;
                         this.progressBar2.Step = 1;
                         this.saveFileDialog1.Title = "Save EXE File";
                         this.saveFileDialog1.CheckPathExists = true;
                         this.saveFileDialog1.DefaultExt = "exe";
                         this.saveFileDialog1.Filter = "EXE Files (*.exe)|*.exe|All Files (*.*)|*.*";
                         this.saveFileDialog1.FilterIndex = 1;
                         this.saveFileDialog1.RestoreDirectory = true;
                         if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                         {
                             string fileName = this.saveFileDialog1.FileName;
                             base.Enabled = false;
                             this.panel2.Visible = false;
                             this.imageListView1.Visible = false;
                             this.progressBar2.Visible = true;
                             for (int j = 0; j < this.imageListView1.SelectedItems.Count; j++)
                             {
                                 string text3 = string.Concat(new object[]
                                 {
                                     "Processing Photo ",
                                     this.progressBar2.Value,
                                     " From ",
                                     this.progressBar2.Maximum
                                 });
                                 using (Graphics graphics = this.progressBar2.CreateGraphics())
                                 {
                                     graphics.DrawString(text3, SystemFonts.DefaultFont, Brushes.Black, new PointF((float)(this.progressBar2.Width / 2) - graphics.MeasureString(text3.ToString(), SystemFonts.DefaultFont).Width / 2f, (float)(this.progressBar2.Height - 15)));
                                 }
                                 string text2 = Path.GetFileName(this.imageListView1.SelectedItems[j].FileName);
                                 string fileName2 = this.imageListView1.SelectedItems[j].FileName;
                                 string target = str + "\\selectedimages\\" + text2;
                                 Image image = Image.FromFile(fileName2);
                                 resizer.MaxX = image.Height * percentage / 100;
                                 resizer.MaxY = image.Width * percentage / 100;
                                 resizer.Resize(fileName2, target);
                                 image.Dispose();
                                 this.progressBar2.PerformStep();
                             }
                             this.progressBar2.Visible = false;
                             string directoryName = Path.GetDirectoryName(fileName);
                             string path = directoryName + "\\ResourceManager.dll";
                             if (LstFld.CompileCSharpCode(".\\Class1.cs", fileName))
                             {
                                 if (!File.Exists(path))
                                 {
                                     File.Copy(".\\ResourceManager.dll", directoryName + "\\ResourceManager.dll");
                                 }
                                 MessageBox.Show("All Photos are Locked in Exe File");
                             }
                             base.Enabled = true;
                         }
                     }
                 }
             }
             catch (Exception ex)
             {
                 MessageBox.Show("Error Generating EXE File : " + ex.ToString() + " Please Try again");
             }
         }
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Check It , How to Save .exe File in c#

0 Votes 0 ·

Hi @KailashSahu-8768,
According to the code you provided, what specific error did you encounter?
And Shawn Miller's answer maybe helpful to you.
Best Regards,
Daniel Zhang


0 Votes 0 ·