所以我有一个按钮和4个图片框,当我点击按钮时,我想在第一个图片框上添加1张图片,如果我第二次点击按钮,我想制作picturebox2 = picturebox1和picturebox1 = new图像等等到4 这是我到目前为止所做的,但它没有工作,它向我显示所有4个图片框上的相同图像:
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;
}
}
}
}
}
并且如果您知道:如果您在任何图片框/我将创建的文件夹中没有任何内容存储所有这些照片,我如何使该设备使用设备相机拍照。
答案 0 :(得分:1)
您可以循环使用PictureBox来完成此任务:
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++;
}
}
答案 1 :(得分:0)
尝试克隆或实例化PictureBox的新位图:
File2 = Image.FromFile(f.FileName);
pictureBox2.Image = new Bitmap(pictureBox1.Image); // or pictureBox1.Image.Clone()
pictureBox1.Image = File2;
您复制参考,以便始终显示相同的图片。
**编辑
尝试更改您的算法,因为它有点复杂:
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 : "";
}
答案 2 :(得分:0)
没关系我用这样的开关自己解决了:
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;
}
}