我想使用(打开文件对话框和
)在四个图片框中同时显示四个图像菜单条)在c#中,我用这段代码打印图片,但不正确
private void fileToolStripMenuItem_Click(object sender, EventArgs e)
{
string str = null;
string str2 = null;
Bitmap img,img2;
int n=1;
OpenFileDialog opendialog1 = new OpenFileDialog();
opendialog1.InitialDirectory = "D:\frames";
opendialog1.Filter = "Image File|*.bmp;";
opendialog1.Title = " Open Image file";
if (opendialog1.ShowDialog() == DialogResult.OK)
{
img = new Bitmap(opendialog1.FileName);
pictureBox1.Image = img;
str = opendialog1.FileName;
string name = (n++).ToString().PadLeft(4, '0');
img2 = new Bitmap("D:\frames"+name+".bmp");
pictureBox2.Image = img2;
str2 = opendialog1.FileName;
name = (n++).ToString().PadLeft(4, '0');
img2 = new Bitmap("D:\frames" + name + ".bmp");
pictureBox3.Image = img2;
str2 = opendialog1.FileName;
name = (n++).ToString().PadLeft(4, '0');
img2 = new Bitmap("D:\frames" + name + ".bmp");
pictureBox4.Image = img2;
str2 = opendialog1.FileName;
}
我需要一个方法来显示四个图像在四个图片框在同一时间
您的变量名为name, str, str2, img2和n都是多余的,你想要实现的。
试试这个:
private void fileToolStripMenuItem_Click(object sender, EventArgs e)
{
Bitmap img;
OpenFileDialog opendialog1 = new OpenFileDialog();
opendialog1.InitialDirectory = "D:\frames";
opendialog1.Filter = "Image File|*.bmp;";
opendialog1.Title = " Open Image file";
if (opendialog1.ShowDialog() == DialogResult.OK)
{
img = new Bitmap(opendialog1.FileName);
pictureBox1.Image = img;
img = new Bitmap("D:\frames\0001.bmp");
pictureBox2.Image = img;
img = new Bitmap("D:\frames\0002.bmp");
pictureBox3.Image = img;
img = new Bitmap("D:\frames\0003.bmp");
pictureBox4.Image = img;
}
}
您甚至可以去掉img变量,直接分配图片框图像:
pictureBox1.Image = new Bitmap(opendialog1.FileName);
pictureBox2.Image = new Bitmap("D:\frames\0001.bmp");
pictureBox3.Image = new Bitmap("D:\frames\0002.bmp");
pictureBox4.Image = new Bitmap("D:\frames\0003.bmp");