问题描述
|
我需要使用打开文件对话框以窗口形式打开位图图像(我将从驱动器中加载它)。图像应适合图片框。这是我尝试过的一些代码,但是出错了!
private void button1_Click(object sender,EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = \"Open Image\";
dlg.Filter = \"bmp files (*.bmp)|*.bmp\";
if (dlg.ShowDialog() == DialogResult.OK)
{
PictureBox PictureBox1 = new PictureBox();
PictureBox1.Image(dlg.FileName);
}
dlg.dispose();
}
解决方法
您必须使用构造函数重载(从磁盘上的文件加载图像)来创建
Bitmap
类的实例。现在编写代码时,您正在尝试使用PictureBox.Image
属性,就好像它是一种方法一样。
更改代码,使其看起来像这样(还利用using
语句来确保正确处理,而不是手动调用Dispose
方法):
private void button1_Click(object sender,EventArgs e)
{
// Wrap the creation of the OpenFileDialog instance in a using statement,// rather than manually calling the Dispose method to ensure proper disposal
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = \"Open Image\";
dlg.Filter = \"bmp files (*.bmp)|*.bmp\";
if (dlg.ShowDialog() == DialogResult.OK)
{
PictureBox PictureBox1 = new PictureBox();
// Create a new Bitmap object from the picture file on disk,// and assign that to the PictureBox.Image property
PictureBox1.Image = new Bitmap(dlg.FileName);
}
}
}
当然,这不会在窗体上的任何地方显示图像,因为您创建的图片框控件尚未添加到窗体中。您需要使用Add
方法将刚刚创建的新图片框控件添加到表单的Controls
集合中。请注意此处添加到上述代码的行:
private void button1_Click(object sender,EventArgs e)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = \"Open Image\";
dlg.Filter = \"bmp files (*.bmp)|*.bmp\";
if (dlg.ShowDialog() == DialogResult.OK)
{
PictureBox PictureBox1 = new PictureBox();
PictureBox1.Image = new Bitmap(dlg.FileName);
// Add the new control to its parent\'s controls collection
this.Controls.Add(PictureBox1);
}
}
}
,工作良好。
尝试这个,
private void addImageButton_Click(object sender,EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
//For any other formats
of.Filter = \"Image Files (*.bmp;*.jpg;*.jpeg,*.png)|*.BMP;*.JPG;*.JPEG;*.PNG\";
if (of.ShowDialog() == DialogResult.OK)
{
pictureBox1.ImageLocation = of.FileName;
}
}
,您应该尝试:
以形式直观地创建图片框(更容易)
将图片框的“ 10”属性设置为“ 11”(如果您希望图像填写表格)
将Picturebox的SizeMode
设置为StretchImage
最后:
private void button1_Click(object sender,EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = \"Open Image\";
dlg.Filter = \"bmp files (*.bmp)|*.bmp\";
if (dlg.ShowDialog() == DialogResult.OK)
{
PictureBox1.Image = Image.FromFile(dlg.Filename);
}
dlg.Dispose();
}
,private void button1_Click(object sender,EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if (open.ShowDialog() == DialogResult.OK)
pictureBox1.Image = Bitmap.FromFile(open.FileName);
}
,您也可以这样尝试PictureBox1.Image = Image.FromFile(\"<your ImagePath>\" or <Dialog box result>);
,PictureBox.Image是属性,而不是方法。您可以这样设置:
PictureBox1.Image = System.Drawing.Image.FromFile(dlg.FileName);
,您可以尝试以下方法:
private void button1_Click(object sender,EventArgs e)
{
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = \"Select file to be upload\";
fDialog.Filter = \"All Files|*.*\";
// fDialog.Filter = \"PDF Files|*.pdf\";
if (fDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = fDialog.FileName.ToString();
}
}
,这很简单。只需添加:
PictureBox1.BackgroundImageLayout = ImageLayout.Zoom;