本文介绍了在C#.net表单应用程序中动态生成图库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在使用Image gallery。我在其中动态创建了图片框和标签,用于提供图片名称。现在将所有图片加载到面板中的图片框后,问题在于排列图片框,只有四个连续。当我向下拖动面板并再次调用button1_Click事件时,排列图片框后,图像的位置被从它们中删除。图像加载到不同的位置意味着它正在覆盖。请帮我解决。 图片链接,您可以从中获得确切的想法 http://postimg.org/image / 5tm5gzj87 / http://postimg.org/image/4sridmuuj/ 这是我的代码。 请帮帮我.. 代码: public partial class Form1:Form { int photo_Count,a; PictureBox [] pb; private string [] imgName; public static string ImageToShow ; string _directoryName = @ ./快照; 图片im; Bitmap bm; // System.IO.Directory mydir; System.IO.DirectoryInfo dirinfo; public Form1() { InitializeComponent(); panel1.Top =( this .ClientSize.Height - panel1.Height)/ 2 ; panel1.Left =( this .ClientSize.Width - panel1.Width)/ 2 ; } private void AddControls( int Count) { pb = new System.Windows.Forms.PictureBox [Count]; // 分配号码数组 for ( int i = 0 ; i < 计数; i ++) { pb [i] = new System.Windows.Forms.PictureBox(); // 初始化一个变量 } // 调用此函数时,您可以确定控件的数量 } // 点击图片的事件 private void ClickImage( Object sender,System.EventArgs e) { // 点击:加载(ImageToShow)图像的(标签) ImageToShow =((System.Windows.Forms.PictureBox)sender).Tag.ToString(); // PictureBox picBox =(PictureBox)(发送者); // picBox.BorderStyle = BorderStyle.Fixed3D; } private 图片GetCopyImage(字符串路径) {a = 1 ; 使用(im = Image.FromFile(path)) { bm = new 位图(im); return bm; } } private void button1_Click( object sender,EventArgs e) { if (Directory.Exists(_directoryName)) { imgName = Directory.GetFiles(Application.StartupPath + @ \Snapshot); dirinfo = new System.IO.DirectoryInfo(_directoryName); photo_Count = dirinfo.GetFiles( * jpg)。Count(); if (photo_Count!= 0 ) { AddControls(photo_Count); int xpos = 5 ; int ypos = 5 ; // MessageBox.Show(目录中已记录图片的总数为: + photo_Count); for ( int i = 0 ; i < photo_Count; ++ i) { Label name = new Label(); // *******************加载跟踪文件夹中的图像********************* // im = GetCopyImage(imgName [i]); pb [i] .Image = im; if (xpos > 920 ) { xpos = 5 ; // 在左边留下八个像素 ypos = ypos + 240 ; // 图片高度+ 220 } pb [i] .Left = xpos; pb [i] .Top = ypos; pb [i] .Size = new 大小( 300 , 220 ); pb [i] .BorderStyle = BorderStyle.None; pb [i] .SizeMode = PictureBoxSizeMode.Zoom; pb [i] .BackgroundImageLayout = ImageLayout.Tile; pb [i] .Cursor = System.Windows.Forms.Cursors.Hand; panel1.Controls.Add(pb [i]); pb [i] .BringToFront(); pb [i] .Tag = imgName [i]; pb [i]。点击+ = new System.EventHandler(ClickImage); var filenNme = Path.GetFileName(imgName [i]); name.Text = filenNme.ToString(); name.Size = new 大小( 300 , 30 ); name.Left = xpos; name.Top = ypos + 220 ; name.Tag = imgName [i]; panel1.Controls.Add(name); xpos = xpos + 305 ; } } else { MessageBox.Show( 目录中没有跟踪图像...... !!!, 信息,MessageBoxButtons.OK,MessageBoxIcon.Information); } } else { if (MessageBox.Show( 跟踪图像没有目录...... !!!, 信息,MessageBoxButtons.YesNo,MessageBoxIcon.Information)== DialogResult.Yes); { System.IO.Directory.CreateDirectory(_directoryName); } } } private void btnDELETE_Click( object sender,EventArgs e) { File.Delete(ImageToShow); // button1.PerformClick(); MessageBox.Show( 图像被成功删除... !!!, 信息,MessageBoxButtons.OK,MessageBoxIcon.Information); } 解决方案 嗨Dhruv, 从您在问题中显示的代码中我假设您正在尝试各种原型。如果你想这样做更好地实现自定义控件或至少一个组合用户控件(在Label和picturebox之外)。当然,你需要一种列表来呈现/选择单个图片。 我会实现一个自定义控件(OwnerDrawn)并通过绘制图像摆脱无用的PictureBox / Label控件/自定义控件的绘图处理程序中的文本(这应该可以提高性能),您可以更轻松地控制DoubleBuffering之类的东西。 替代方案可以只是使用准备好的控件完成您在代码中显示的内容 - ListView ,其属性查看设置为 View.Tile 您目前的方法有任何具体问题吗? 顺便说一句。对于这种应用程序,我强烈建议使用WPF而不是Windows.Forms作为演示技术。 (但那是另一个故事) 只是我的2美分(或者你用的是什么货币;-) 亲切的问候Johannes 试试这个, PictureBox picToDelete; 修改此功能。 私有 void ClickImage( Object sender,System.EventArgs e) { // On点击:加载(ImageToShow)带图像的(Tag) ImageToShow =((System.Windows.Forms.PictureBox)sender).Tag.ToString(); PictureBox picToDelete =(PictureBox)(发送者); } private void btnDELETE_Click( object sender,EventArgs e) { panel1.Controls.Remove(picToDelete); File.Delete(ImageToShow); // button1.PerformClick(); MessageBox.Show(& ; 已成功删除... !!!& quot;,& quot; Information& quot;,MessageBoxButtons.OK,MessageBoxIcon.Information); } < / pre > I am working on Image gallery .In which I have dynamically create picturebox and label for giving image name.Now after getting load all images in to pictureboxes in a panel , issue is in arranging the picture boxes, only four in a row. After arranging the picture boxes when I am drag down the panel and again calling the button1_Click event the place of images getting destructed from them place.The images are loading on different location means it's getting over-write.please help me out.Images links from where you can get exact ideahttp://postimg.org/image/5tm5gzj87/http://postimg.org/image/4sridmuuj/here is my code.please help me out..CODE:public partial class Form1 : Form { int photo_Count, a; PictureBox[] pb; private string[] imgName; public static string ImageToShow; string _directoryName = @"./Snapshot"; Image im; Bitmap bm; // System.IO.Directory mydir; System.IO.DirectoryInfo dirinfo; public Form1() { InitializeComponent(); panel1.Top = (this.ClientSize.Height - panel1.Height) / 2; panel1.Left = (this.ClientSize.Width - panel1.Width) / 2; } private void AddControls(int Count) { pb = new System.Windows.Forms.PictureBox[Count]; // assign number array for (int i = 0; i < Count; i++) { pb[i] = new System.Windows.Forms.PictureBox(); // Initialize one variable } // When call this function you determine number of controls } // The Event to click the image private void ClickImage(Object sender, System.EventArgs e) { // On Click: load (ImageToShow) with (Tag) of the image ImageToShow = ((System.Windows.Forms.PictureBox)sender).Tag.ToString(); //PictureBox picBox = (PictureBox)(sender); //picBox.BorderStyle = BorderStyle.Fixed3D; } private Image GetCopyImage(string path) { a = 1; using (im = Image.FromFile(path)) { bm = new Bitmap(im); return bm; } } private void button1_Click(object sender, EventArgs e) { if (Directory.Exists(_directoryName)) { imgName = Directory.GetFiles(Application.StartupPath + @"\Snapshot"); dirinfo = new System.IO.DirectoryInfo(_directoryName); photo_Count = dirinfo.GetFiles("*jpg").Count(); if (photo_Count != 0) { AddControls(photo_Count); int xpos = 5; int ypos = 5; // MessageBox.Show("Total No of traked images in the directory are : " + photo_Count); for (int i = 0; i < photo_Count; ++i) { Label name = new Label(); //*******************Load tracked Images from the folder*********************// im = GetCopyImage(imgName[i]); pb[i].Image = im; if (xpos > 920) { xpos = 5; // leave eight pixels at Left ypos = ypos + 240; // height of image + 220 } pb[i].Left = xpos; pb[i].Top = ypos; pb[i].Size = new Size(300, 220); pb[i].BorderStyle = BorderStyle.None; pb[i].SizeMode = PictureBoxSizeMode.Zoom; pb[i].BackgroundImageLayout = ImageLayout.Tile; pb[i].Cursor = System.Windows.Forms.Cursors.Hand; panel1.Controls.Add(pb[i]); pb[i].BringToFront(); pb[i].Tag = imgName[i]; pb[i].Click += new System.EventHandler(ClickImage); var filenNme = Path.GetFileName(imgName[i]); name.Text = filenNme.ToString(); name.Size = new Size(300, 30); name.Left = xpos; name.Top = ypos + 220; name.Tag = imgName[i]; panel1.Controls.Add(name); xpos = xpos + 305; } } else { MessageBox.Show("There is no tracked image in directory...!!!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { if (MessageBox.Show("There is no directory for Tracked Images...!!!", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) ; { System.IO.Directory.CreateDirectory(_directoryName); } } } private void btnDELETE_Click(object sender, EventArgs e) { File.Delete(ImageToShow); //button1.PerformClick(); MessageBox.Show("The image is deleted successfully...!!!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } 解决方案 Hi Dhruv,From the code you showed in your question I assume you are experimenting with kind of prototype. If you want to go that way better implement a custom control or at least a composed user control (out of Label and picturebox). Of course then you need a kind of "list" to present/select the individual pictures.I would implement a custom control (OwnerDrawn) and get rid of useless PictureBox/Label controls by just painting the Image/Text in a custom control's paint handler (this should boost perfomance) and you can easier control things like DoubleBuffering. An alternative could be just using a prepared control doing exactly what you showed in your code - a ListView with the property View set to View.TileDo you have any specific problems with your current approach?Btw. for this kind of app I would highly recommend using WPF and not Windows.Forms as presentation technology. (But that's another story)just my 2 cents (or what currency you use ;-)kind regards JohannesTry this,PictureBox picToDelete;modify this function.private void ClickImage(Object sender, System.EventArgs e) { // On Click: load (ImageToShow) with (Tag) of the image ImageToShow = ((System.Windows.Forms.PictureBox)sender).Tag.ToString(); PictureBox picToDelete = (PictureBox)(sender); }private void btnDELETE_Click(object sender, EventArgs e) { panel1.Controls.Remove(picToDelete); File.Delete(ImageToShow); //button1.PerformClick(); MessageBox.Show("The image is deleted successfully...!!!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); }</pre> 这篇关于在C#.net表单应用程序中动态生成图库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-23 18:38