如何在WinForm中的PictureBox中显示文件系统中的图像 [英] How to display an image from file system in a PictureBox in WinForm
问题描述
我对在WinForms中加载PictureBox中的图像有一点疑问。
我想在 PictureBox中显示来自文件系统的图像文件
在我的表格上,说 form1
。
I have a small doubt regarding loading an image in PictureBox in WinForms.
I want to show an image file from file system in a PictureBox
on my form, say form1
.
我正在使用C#进行Windows应用程序。
I am doing Windows applications using C#.
我想检查文件类型也说是pdf / text / png / gif / jpeg。
是否可以使用C#以编程方式从文件系统中打开文件?
如果有人知道,请提供任何想法或示例代码。
I want to check the file type also say is it pdf/text/png/gif/jpeg.
Is it possible to programmatically open a file from file system using C#?
If anyone knows please give any idea or sample code for doing this.
修改后的代码:我这样做是为了在我的系统中打开文件,但我不知道如何附加文件并附加文件。
Modified Code: I have done like this for opening a file in my system, but I don't know how to attach the file and attach the file.
private void button1_Click(object sender, EventArgs e)
{
string filepath = @"D:\";
openFileDialog1.Filter = "Image Files (*.jpg)|*.jpg|(*.png)|*.png|(*.gif)|*.gif|(*.jpeg)|*.jpeg|";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
try
{
}
}
}
我不知道我要写什么尝试
阻止。任何人都可以帮忙吗?
I don't know what I have to write in try
block. Can anyone help on this?
推荐答案
使用Image.ImageFromFile http://msdn.microsoft.com/en-us/library/system.drawing.image.fromfile.aspx 方法
Use Image.ImageFromFile http://msdn.microsoft.com/en-us/library/system.drawing.image.fromfile.aspx method
图片img = Image.ImageFromFile(openFileDialog1.FileName);
Image img = Image.ImageFromFile(openFileDialog1.FileName);
应该有效。
编辑
如果你打算将它设置为PictureBox,那么看什么在里面完成,使用picturebox
If you're going to set it to PictureBox, and what to see complete inside it, use picturebox
SizeMode 属性。
这篇关于如何在WinForm中的PictureBox中显示文件系统中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!