Why i need to wait so long.....
-
HI all, i'm trying to populate a panel with an array of picture but when i debug the program is so slow to download the picture in the panel. i have this code snippet :
public partial class ArtLabel : Form
{
private System.Windows.Forms.PictureBox[] imgArray; //Declaring array of PictureBox
public static string ImageToShow;
private int NumOfFiles;
private string[] imgExtension;public ArtLabel() { InitializeComponent(); panel1.VerticalScroll.Visible = true; panel1.VerticalScroll.Enabled = true; // panel1.VerticalScroll.Maximum = 1000; panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum; } private void ArtLabel\_Load(object sender, EventArgs e) { GetPicture4(@"D:\\\\Music\\\\"); } private bool ThumbnailCallback() { return false; } private void ARR(int cNumber, string exc) { int Xpos = 8; int Ypos = 8; Image img; Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback); imgArray = new System.Windows.Forms.PictureBox\[cNumber\]; // assign number array for (int i = 0; i < cNumber; i++) { imgArray\[i\] = new System.Windows.Forms.PictureBox(); // Initialize one variable if (Xpos > 432) // six images in a line { Xpos = 8; // leave eight pixels at Left Ypos = Ypos + 72; // height of image + 8 } imgArray\[i\].Left = Xpos; imgArray\[i\].Top = Ypos; imgArray\[i\].Width = 64; imgArray\[i\].Height = 64; imgArray\[i\].Visible = true; imgArray\[i\].SizeMode = PictureBoxSizeMode.StretchImage; img = Image.FromFile(exc); imgArray\[i\].Tag = exc; imgArray\[i\].Click += new System.EventHandler(ClickImage); imgArray\[i\].Image = img.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero); panel1.Controls.Add(imgArray\[i\]); Application.DoEvents(); Xpos = Xpos + 72; } } private List<stri
ascotravel wrote:
Do you have any advice where i wrong in my code?
yes. 1. it is better to use Image.FromStream() rather than Image.FromFile() since the latter keeps the file locked. This is however not affecting performance AFAIK. 2. it is much better to dispose of all objects you don't need any longer provided their class offers a public Dispose() method. Image class does, so don't just abandon
img
by assigning a new image to it, first do aif(img!null) img.Dispose();
That will reduce your memory consumption, reducing the work of the garbage collector. 3. the basic idea of thumbnail images is you don't calculate them over and over; e.g. have your program create thumbnail files (with a modified filename), and look for them when loading. That will avoid the file load and the computation. For small images, avoid compressed formats such as JPEG; use BMP or GIF instead. BTW: although some image formats support a built-in thumbnail, these are not always present. Furthermore the quality of GetThumbnailImage() is rumored to be not good. :) -
HI all, i'm trying to populate a panel with an array of picture but when i debug the program is so slow to download the picture in the panel. i have this code snippet :
public partial class ArtLabel : Form
{
private System.Windows.Forms.PictureBox[] imgArray; //Declaring array of PictureBox
public static string ImageToShow;
private int NumOfFiles;
private string[] imgExtension;public ArtLabel() { InitializeComponent(); panel1.VerticalScroll.Visible = true; panel1.VerticalScroll.Enabled = true; // panel1.VerticalScroll.Maximum = 1000; panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum; } private void ArtLabel\_Load(object sender, EventArgs e) { GetPicture4(@"D:\\\\Music\\\\"); } private bool ThumbnailCallback() { return false; } private void ARR(int cNumber, string exc) { int Xpos = 8; int Ypos = 8; Image img; Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback); imgArray = new System.Windows.Forms.PictureBox\[cNumber\]; // assign number array for (int i = 0; i < cNumber; i++) { imgArray\[i\] = new System.Windows.Forms.PictureBox(); // Initialize one variable if (Xpos > 432) // six images in a line { Xpos = 8; // leave eight pixels at Left Ypos = Ypos + 72; // height of image + 8 } imgArray\[i\].Left = Xpos; imgArray\[i\].Top = Ypos; imgArray\[i\].Width = 64; imgArray\[i\].Height = 64; imgArray\[i\].Visible = true; imgArray\[i\].SizeMode = PictureBoxSizeMode.StretchImage; img = Image.FromFile(exc); imgArray\[i\].Tag = exc; imgArray\[i\].Click += new System.EventHandler(ClickImage); imgArray\[i\].Image = img.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero); panel1.Controls.Add(imgArray\[i\]); Application.DoEvents(); Xpos = Xpos + 72; } } private List<stri
Replace your ARR method with this:
private void ARR(int cNumber, string exc)
{
int Xpos = 8;
int Ypos = 8;
Image img;
Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
imgArray = new System.Windows.Forms.PictureBox[cNumber]; // assign number array
imgArray[cNumber] = new System.Windows.Forms.PictureBox(); // Initialize one variable
if (Xpos > 432) // six images in a line
{
Xpos = 8; // leave eight pixels at Left
Ypos = Ypos + 72; // height of image + 8
}imgArray\[cNumber\].Left = Xpos; imgArray\[cNumber\].Top = Ypos; imgArray\[cNumber\].Width = 64; imgArray\[cNumber\].Height = 64; imgArray\[cNumber\].Visible = true; imgArray\[cNumber\].SizeMode = PictureBoxSizeMode.StretchImage; img = Image.FromFile(exc); imgArray\[cNumber\].Tag = exc; imgArray\[cNumber\].Click += new System.EventHandler(ClickImage); imgArray\[cNumber\].Image = img.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero); panel1.Controls.Add(imgArray\[cNumber\]); Xpos = Xpos + 72; }
I've removed the loop, and the Application.DoEvents call. You were effectively creating the same PictureBoxes many times over
-
Replace your ARR method with this:
private void ARR(int cNumber, string exc)
{
int Xpos = 8;
int Ypos = 8;
Image img;
Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
imgArray = new System.Windows.Forms.PictureBox[cNumber]; // assign number array
imgArray[cNumber] = new System.Windows.Forms.PictureBox(); // Initialize one variable
if (Xpos > 432) // six images in a line
{
Xpos = 8; // leave eight pixels at Left
Ypos = Ypos + 72; // height of image + 8
}imgArray\[cNumber\].Left = Xpos; imgArray\[cNumber\].Top = Ypos; imgArray\[cNumber\].Width = 64; imgArray\[cNumber\].Height = 64; imgArray\[cNumber\].Visible = true; imgArray\[cNumber\].SizeMode = PictureBoxSizeMode.StretchImage; img = Image.FromFile(exc); imgArray\[cNumber\].Tag = exc; imgArray\[cNumber\].Click += new System.EventHandler(ClickImage); imgArray\[cNumber\].Image = img.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero); panel1.Controls.Add(imgArray\[cNumber\]); Xpos = Xpos + 72; }
I've removed the loop, and the Application.DoEvents call. You were effectively creating the same PictureBoxes many times over
Hi Thanks to reply me so fast, for Luc i will follow your advice as i'm a novice i need wise advice..Thanks :) @Computafreak - Thanks to post the code but not luck . When i debug the code in this line code : <pre> imgArray[cNumber] = new System.Windows.Forms.PictureBox(); appear this error:"Index was outside the bounds of the array.". I'm trying to work out it if i can i will post my solution. Thanks a lot for your help. Nice Regards :)
-
Hi Thanks to reply me so fast, for Luc i will follow your advice as i'm a novice i need wise advice..Thanks :) @Computafreak - Thanks to post the code but not luck . When i debug the code in this line code : <pre> imgArray[cNumber] = new System.Windows.Forms.PictureBox(); appear this error:"Index was outside the bounds of the array.". I'm trying to work out it if i can i will post my solution. Thanks a lot for your help. Nice Regards :)
Hi, If you remove the for loop from the ARR method you have to remove
imgArray = new System.Windows.Forms.PictureBox[cNumber];
there as well, and allocate the array at the caller. :) -
Hi, If you remove the for loop from the ARR method you have to remove
imgArray = new System.Windows.Forms.PictureBox[cNumber];
there as well, and allocate the array at the caller. :)Hi Luc, sorry i'm shame for my poor knowledge but i removed the loop For and i followed your advices but i receive always the same error . What i do of wrong??? :^) EDIT : if i remove <code>imgArray = new System.Windows.Forms.PictureBox[cNumber]; and after i debug in this code line
imgArray[cNumber] = new System.Windows.Forms.PictureBox();
i receive this error "Object reference not set to an instance of an object." :(
modified on Saturday, April 11, 2009 2:22 PM
-
HI all, i'm trying to populate a panel with an array of picture but when i debug the program is so slow to download the picture in the panel. i have this code snippet :
public partial class ArtLabel : Form
{
private System.Windows.Forms.PictureBox[] imgArray; //Declaring array of PictureBox
public static string ImageToShow;
private int NumOfFiles;
private string[] imgExtension;public ArtLabel() { InitializeComponent(); panel1.VerticalScroll.Visible = true; panel1.VerticalScroll.Enabled = true; // panel1.VerticalScroll.Maximum = 1000; panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum; } private void ArtLabel\_Load(object sender, EventArgs e) { GetPicture4(@"D:\\\\Music\\\\"); } private bool ThumbnailCallback() { return false; } private void ARR(int cNumber, string exc) { int Xpos = 8; int Ypos = 8; Image img; Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback); imgArray = new System.Windows.Forms.PictureBox\[cNumber\]; // assign number array for (int i = 0; i < cNumber; i++) { imgArray\[i\] = new System.Windows.Forms.PictureBox(); // Initialize one variable if (Xpos > 432) // six images in a line { Xpos = 8; // leave eight pixels at Left Ypos = Ypos + 72; // height of image + 8 } imgArray\[i\].Left = Xpos; imgArray\[i\].Top = Ypos; imgArray\[i\].Width = 64; imgArray\[i\].Height = 64; imgArray\[i\].Visible = true; imgArray\[i\].SizeMode = PictureBoxSizeMode.StretchImage; img = Image.FromFile(exc); imgArray\[i\].Tag = exc; imgArray\[i\].Click += new System.EventHandler(ClickImage); imgArray\[i\].Image = img.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero); panel1.Controls.Add(imgArray\[i\]); Application.DoEvents(); Xpos = Xpos + 72; } } private List<stri
Hi, you need a
imgArray = new System.Windows.Forms.PictureBox[NumOfFiles];
inside GetPicture4() and none of it inside ARR(). And I strongly suggest you buy a tutorial book on C# and study it. It is the only efficient way to make real progress. :) -
Hi, you need a
imgArray = new System.Windows.Forms.PictureBox[NumOfFiles];
inside GetPicture4() and none of it inside ARR(). And I strongly suggest you buy a tutorial book on C# and study it. It is the only efficient way to make real progress. :)I tried this code snippet:
private List<string> GetPicture4(string Folder)
{
imgArray = new System.Windows.Forms.PictureBox[NumOfFiles]; // here i add the imaArray
DirectoryInfo dir = new DirectoryInfo(Folder);
List<string> str = new List<string>();
FileInfo[] files = dir.GetFiles("*.jpg", SearchOption.AllDirectories);
NumOfFiles = files.Count();
imgExtension = new string[NumOfFiles];for (int i = 0; i < NumOfFiles; i++) { ARR(i, files\[i\].FullName); str.Add(files\[i\].FullName); } return str; }
and after i remode this line
imgArray = new System.Windows.Forms.PictureBox[NumOfFiles]; // here i add the imaArray
from the Arr Method but i receive always this error "Index was outside the bounds of the array." :doh: Really sorry maybe i misundertood your advice if yes please be patient i try my best. Thanks a lot P.S. i will buy a book as soon as possible,i need improve my English too :)
-
Hi Thanks to reply me so fast, for Luc i will follow your advice as i'm a novice i need wise advice..Thanks :) @Computafreak - Thanks to post the code but not luck . When i debug the code in this line code : <pre> imgArray[cNumber] = new System.Windows.Forms.PictureBox(); appear this error:"Index was outside the bounds of the array.". I'm trying to work out it if i can i will post my solution. Thanks a lot for your help. Nice Regards :)
-
I tried this code snippet:
private List<string> GetPicture4(string Folder)
{
imgArray = new System.Windows.Forms.PictureBox[NumOfFiles]; // here i add the imaArray
DirectoryInfo dir = new DirectoryInfo(Folder);
List<string> str = new List<string>();
FileInfo[] files = dir.GetFiles("*.jpg", SearchOption.AllDirectories);
NumOfFiles = files.Count();
imgExtension = new string[NumOfFiles];for (int i = 0; i < NumOfFiles; i++) { ARR(i, files\[i\].FullName); str.Add(files\[i\].FullName); } return str; }
and after i remode this line
imgArray = new System.Windows.Forms.PictureBox[NumOfFiles]; // here i add the imaArray
from the Arr Method but i receive always this error "Index was outside the bounds of the array." :doh: Really sorry maybe i misundertood your advice if yes please be patient i try my best. Thanks a lot P.S. i will buy a book as soon as possible,i need improve my English too :)
ascotravel wrote:
imgArray = new System.Windows.Forms.PictureBox[NumOfFiles];
What do you think is the value of
NumOfFiles
at this point? :| -
ascotravel wrote:
imgArray = new System.Windows.Forms.PictureBox[NumOfFiles];
What do you think is the value of
NumOfFiles
at this point? :|Okay Luc , I work out it after read an article about array ,i removed the method ARR() and i organize the code in the GetPicture4() creating the right loop and removing the messy code wrote down in past. Thanks for your patience and sorry for my stupidity :-O Have a good work. Nice Regards