求补全c#代码:我想也要实现单击按钮后从电脑里选择一张图片,然后通过picturebox这个控件显示出来。
代码:privatevoidbutton1_Click(objectsender,EventArgse){stringImgPath;StreammyStream;Ope...
代码: private void button1_Click(object sender, EventArgs e)
{
string ImgPath;
Stream myStream;
OpenFileDialog openfiledialog = new OpenFileDialog();//初始化openfiledialog对象
openfiledialog.InitialDirectory = "c:";//设置初始目录
openfiledialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
if (openfiledialog.ShowDialog() == DialogResult.OK)
{
if ((myStream = openfiledialog.OpenFile()) != null)
{
// Insert code to read the stream here.
myStream.Close();
}
}
剩下的怎么写? 展开
{
string ImgPath;
Stream myStream;
OpenFileDialog openfiledialog = new OpenFileDialog();//初始化openfiledialog对象
openfiledialog.InitialDirectory = "c:";//设置初始目录
openfiledialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
if (openfiledialog.ShowDialog() == DialogResult.OK)
{
if ((myStream = openfiledialog.OpenFile()) != null)
{
// Insert code to read the stream here.
myStream.Close();
}
}
剩下的怎么写? 展开
2个回答
展开全部
没这麼复杂!看下我的代码:
private void button1_Click(object sender, EventArgs e)
{
string ImgPath = "";
OpenFileDialog openfiledialog = new OpenFileDialog();//初始化openfiledialog对象
openfiledialog.InitialDirectory = "c:";//设置初始目录
openfiledialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
if (openfiledialog.ShowDialog() == DialogResult.OK)
{
ImgPath = openfiledialog.FileName;
if (File.Exists(ImgPath))
{
pictureBox1.ImageLocation = ImgPath;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
string ImgPath = "";
OpenFileDialog openfiledialog = new OpenFileDialog();//初始化openfiledialog对象
openfiledialog.InitialDirectory = "c:";//设置初始目录
openfiledialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
if (openfiledialog.ShowDialog() == DialogResult.OK)
{
ImgPath = openfiledialog.FileName;
if (File.Exists(ImgPath))
{
pictureBox1.ImageLocation = ImgPath;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询