2016-11-17 7 views
-1

Also habe ich eine Schaltfläche und 4 Pictureboxes und wenn ich auf den Button Ich möchte auf der ersten Bildbox 1 Bild hinzufügen, und wenn ich die Taste das zweite Mal klicken möchte ich die picturebox2 = picturebox1 machen und die PictureBox1 = das neue Bild und so weiter, bis 4 das ist, was ich bisher tat, aber es funktioniert nicht, es zeigt mir, auf den alle 4 PictureBox das gleiche Bild:Anzeige 4 Bild 1 1

namespace ImageUploadAndCameraUse 
{ 
    public partial class Form1 : Form 
    { 
     Image File; 
     Image File2; 
     Image File3; 
     Image File4; 

     bool button1Click = true; 
     bool button1Click2 = true; 
     bool button1Click3 = true; 
     bool button1Click4 = true; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog f = new OpenFileDialog(); 
      f.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png"; 

      bool IsNullOrEmpty1 = false; 
      bool IsNullOrEmpty2 = false; 
      bool IsNullOrEmpty3 = false; 

      if (f.ShowDialog() == DialogResult.OK) 
      { 
       if (button1Click) 
       { 
        File = Image.FromFile(f.FileName); 
        pictureBox1.Image = File; 

        IsNullOrEmpty1 = true; 
        button1Click = false; 
       } 
       if (IsNullOrEmpty1 && button1Click2) 
       { 
        File2 = Image.FromFile(f.FileName); 
        pictureBox2.Image = pictureBox1.Image; 
        pictureBox1.Image = File2; 

        IsNullOrEmpty2 = true; 
        button1Click2 = false; 
       } 
       if (IsNullOrEmpty2 && button1Click3) 
       { 
        File3 = Image.FromFile(f.FileName); 
        pictureBox3.Image = File3; 
        IsNullOrEmpty3 = true; 
        button1Click3 = false; 
       } 
       if (IsNullOrEmpty3 && button1Click4) 
       { 
        File4 = Image.FromFile(f.FileName); 
        pictureBox4.Image = File4; 
        button1Click4 = false; 
       } 
      } 
     } 
    } 
} 

und auch wenn Sie wissen: Wie kann ich dieses Programm verwenden, um die Gerätekamera zu verwenden, um ein Foto zu machen, wenn Sie nichts in irgendeiner Bildbox/dem Ordner haben, den ich erstellen werde, um alle diese Fotos zu speichern.

Antwort

0

Try-Klon oder instanziiert neues Bitmap für PictureBox:

File2 = Image.FromFile(f.FileName); 
pictureBox2.Image = new Bitmap(pictureBox1.Image); // or pictureBox1.Image.Clone() 
pictureBox1.Image = File2; 

Sie kopieren Referenz, so dass Sie immer das gleiche Bild zeigen.

** EDIT

Versuchen Sie algorith ändern, weil es ein bisschen kompliziert ist:

private List<Image> _images = new List<Image>(); 
private PictureBox[] _pictureBoxes = new [] { pictureBox1, pictureBox2, pictureBox3, pictureBox4 }; 

private void button1_Click(object sender, EventArgs e) { 
    var fileName = GetFileName(); 

    if (string.IsNullOrEmpty(fileName)) return; 

    var image = Bitmap.FromFile(fileName); 
    _images.Insert(0, image); // I don't control items in list, you can remove items when reach count grater than 4 

    for(var i = 0; i < Math.Min(_images.Count, 4); i++) { 
     // _pictureBoxes[i].Image = null; // I'm not sure if this is necessary 
     _pictureBoxes[i].Image = _images[i]; // set image as we store it in list 
    } 
} 

private string GetFileName() { 
    var form = new OpenFileDialog(); 
    form.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png"; 

    return form.ShowDialog() == DialogResult.OK ? form.FileName : ""; 
} 
+0

habe gerade noch dasselbe Bild auf allen Pictureboxen versucht, aber kann ich keinen bestimmten Klick auf Button machen, wie wenn ich das zuerst mache, wenn ich einen zweiten Klick mache und so weiter? –

1

Sie können dies nur eine Schleife durch Ihre PictureBox zu erreichen:

int boxIndex = 0; 

private void button1_Click(object sender, EventArgs e) { 
    OpenFileDialog f = new OpenFileDialog(); 
    f.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png"; 
    if (f.ShowDialog() == DialogResult.OK) { 
    PictureBox[] boxes = new PictureBox[] { pictureBox1, pictureBox2, pictureBox3, pictureBox4 }; 
    if (boxIndex + 1 > boxes.Length) { 
     foreach (PictureBox pb in boxes) { 
     pb.Image = null; 
     } 
     boxIndex = 0; 
    } 
    for (int i = boxIndex; i > 0; --i) { 
     boxes[i].Image = boxes[i - 1].Image; 
    } 
    boxes[0].Image = Image.FromFile(f.FileName); 
    boxIndex++; 
    } 
} 
0

Nevermind gelöst I es selbst mit einem Schalter so:

private void button1_Click(object sender, EventArgs e) 
     { 
      ++NumberOfClick; 
      switch (NumberOfClick) 
      { 
       case 1: 
        OpenFileDialog f = new OpenFileDialog(); 
        f.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png"; 

        if (f.ShowDialog() == DialogResult.OK) 
        { 
         File = Image.FromFile(f.FileName); 
         pictureBox1.Image = File; 
        } 

        break; 
       case 2: 
        OpenFileDialog f2 = new OpenFileDialog(); 
        f2.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png"; 

        if (f2.ShowDialog() == DialogResult.OK) 
        { 
         File = Image.FromFile(f2.FileName); 
         pictureBox2.Image = pictureBox1.Image; 
         pictureBox1.Image = File; 
        } 
        break; 
       case 3: 
        OpenFileDialog f3 = new OpenFileDialog(); 
        f3.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png"; 

        if (f3.ShowDialog() == DialogResult.OK) 
        { 
         File = Image.FromFile(f3.FileName); 
         pictureBox3.Image = pictureBox2.Image; 
         pictureBox2.Image = pictureBox1.Image; 
         pictureBox1.Image = File; 
        } 
        break; 
       case 4: 
        OpenFileDialog f4 = new OpenFileDialog(); 
        f4.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png"; 

        if (f4.ShowDialog() == DialogResult.OK) 
        { 
         File = Image.FromFile(f4.FileName); 
         pictureBox4.Image = pictureBox3.Image; 
         pictureBox3.Image = pictureBox2.Image; 
         pictureBox2.Image = pictureBox1.Image; 
         pictureBox1.Image = File; 
        } 

        break; 
       default: 
        // other clicks 
        // . . . 
        break; 

      } 
     }