动态加载图像 [英] Load Image Dynamically
问题描述
朋友,
我正在开发Windows应用程序,我需要在图片框中动态更改图像.我已经添加了一个名为ImagesNew的自定义文件夹.在该文件夹中我有2张图像,单击按钮需要更改图像.我该怎么办那.所有建议都是可以理解的..
Hi Friends,
I am Developing a windows application,where i need to change image dynamically in the picture box.i have added a custom folder named ImagesNew.in that folder i have 2 images,on button click i need to change the image.How do i do that.All your Suggestions are appreciable..
推荐答案
您需要在按钮单击处理程序中设置PictureBox的ImageLocation属性:
You need to set the ImageLocation property of the PictureBox in your button click handler:
protected void Button_Click(object sender, EventArgs e)
{
this.PictureBox1.ImageLocation = "PATH TO IMAGE HERE";
}
http://msdn.microsoft.com/zh-cn/library/system.windows.forms.picturebox%28v = vs.100%29.aspx [
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox%28v=vs.100%29.aspx[^]
假设您正在使用一个PictureBox控件来显示图像,在按钮单击事件处理程序中,您可以将Image属性更新为所需的图像.
Assuming you are using a PictureBox control to display the image, in your button click event handler you would update the Image property to the desired image.
好吧,像这样开始,在窗体加载或其他事件上获取所有图像给定目录中的文件:
Well, start like this, on form load or other event get all the files in given dir:
// Get files from directory (with specified extension)
string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.bmp");
在图片框中设置数组的第一项,在页面中设置当前项的索引.
set the first item of the array in the picture box and the current item index in the page.
int currentItem = 0;
int maxItems;
...
maxItems = filePaths.Length;
然后将图片设置在图片框中
then set the image in picture box
Image image = Image.FromFile(filePaths[maxItems]);
pictureBox1.Image = image;
pictureBox1.Height = image.Height;
pictureBox1.Width = image.Width;
每次点击设置下一张图片:
Set the next image on every click:
Image image = Image.FromFile(filePaths[maxItems]);
pictureBox1.Image = image;
pictureBox1.Height = image.Height;
pictureBox1.Width = image.Width;
maxItems++;
这是一个粗略的解释,需要您尝试一下.
干杯
This is a rough explanation, it is on you to experiment a bit.
Cheers
这篇关于动态加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!