i have picture box in my form.i need to import many picture in to tha picture box. pictures should be in thumbnail type.when load pictures load in to box either auto resize(view) or add auto scroll bar in to picture box will be possible.if cannot load multi pictures in to one picture box please tell me is there go any other possible way to load many pictures in to a form.i mean may be use panel will be possible or not?????
if any body can help me.....im stuck wil my school project...... thx u every body.....
if any body can help me.....im stuck wil my school project...... thx u every body.....
Len La SalaPosted Feb 13, 2009, 7:07 PM
Here is some code which will do the job for you, the code shows just one way you can do it, the rest is up to you.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication3
{
public partial class Form1 : Form
{
//array of picture boxes
PictureBox[] p = new PictureBox[0];
int last=0; //index to last pcture box added to array
int row = 0; //current row of picture boxes
int rowCount = 0; //number of rows of picture boxes
int rowTop = 5; //picture box top for current row
int rowLeft = 5; //picture box left for current row
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for(int i=0; i<28; i++)
{
Array.Resize(ref p, p.Length +1);
p[p.Length-1] = new PictureBox();
p[p.Length-1].Text = (p.Length-1).ToString();
p[p.Length-1].BorderStyle = BorderStyle.Fixed3D;
p[p.Length-1].Width = 63;
p[p.Length-1].Height = 63;
p[p.Length-1].ImageLocation = "your image location";
//now check that next image will not exceed width of forms client area
p[p.Length-1].Left = rowLeft;
if((p[p.Length-1].Left + p[p.Length-1].Width + 5) > (this.ClientRectangle.Width - p[p.Length-1].Width))
{
row ++;
//assuming that all picture boxes will be the same height incement the value
//of rowTop to height of picture box on first row + 5 pixcell spacing
rowTop += p[0].Height +5;
//reset left to left pos of fist picture box on current row
rowLeft =5;
p[p.Length-1].Left = rowLeft;
}
else
{
if(rowCount == 0) //1st picture box on first row
{
p[p.Length-1].Left = 5;
p[p.Length-1].Top = rowTop;
}
else
{
//add width of last picture box + 5 pxcells to rowLeft
p[p.Length-1].Left = p[last].Left + p[last].Width + 5;
rowLeft += p[p.Length-1].Width +5;
}
p[p.Length-1].Top = rowTop;
rowCount++; //increment row count
}
p[p.Length-1].Location = new Point(p[p.Length-1].Left, rowTop);
this.Controls.Add(p[p.Length-1]);
last = p.Length-1;
}
}
}
}
asela WijesooriyaPosted Feb 18, 2009, 10:40 PM
actually i managed to do that by my self.....
i hope for others will helpful this......so i posted what i have done.....
i count the open diolog box slected files and pass that valu in to array size. so array will creat according to the number of files that user select....
here is the codes....
public partial class Form10 : Form
{
public Form10()
{
InitializeComponent();
}
private void Form10_Load(object sender, EventArgs e)
{
ImageList imageList = new ImageList();
imageList.ImageSize = new Size(108, 108);
imageList.ColorDepth = ColorDepth.Depth32Bit;
int i = 0;
int x = 0;
int count;
string[] files = dlg.FileNames;
count = files.length;
x = count;
string[] pathes = new string[x];
while(i != x)
{
foreach(stirng file in files)
{
pathes[ i ] = file;
i++
}
dlg.reset( );
}
foreach (string path in pathes)
{
Image img1 = Image.FromFile(path);
imageList.Images.Add(getThumbnaiImage(imageList.ImageSize.Width, img1));
}
for (int j = 0; j < pathes.Length; j++)
{
this.listView1.Items.Add("Image\n aaaaaa\r\n bbbbbbbbbbbbbbbbbbbbb ");
this.listView1.Items[j].ImageIndex = j;
}
this.listView1.View = View.LargeIcon;
this.listView1.LargeImageList = imageList;
}
private Image getThumbnaiImage(int width, Image img)
{
Image thumb = new Bitmap(width, width);
Image tmp = null;
//If the original image is small than the Thumbnail size, just draw in the center
if (img.Width < width && img.Height < width)
{
using (Graphics g = Graphics.FromImage(thumb))
{
int xoffset = (int)((width - img.Width) / 2);
int yoffset = (int)((width - img.Height) / 2);
g.DrawImage(img, xoffset, yoffset, img.Width, img.Height);
}
}
else //Otherwise we have to get the thumbnail for drawing
{
Image.GetThumbnailImageAbort myCallback = new
Image.GetThumbnailImageAbort(ThumbnailCallback);
if (img.Width == img.Height)
{
thumb = img.GetThumbnailImage(
width, width,
myCallback, IntPtr.Zero);
}
else
{
int k = 0;
int xoffset = 0;
int yoffset = 0;
if (img.Width < img.Height)
{
k = (int)(width * img.Width / img.Height);
tmp = img.GetThumbnailImage(k, width, myCallback, IntPtr.Zero);
xoffset = (int)((width - k) / 2);
}
if (img.Width > img.Height)
{
k = (int)(width * img.Height / img.Width);
tmp = img.GetThumbnailImage(width, k, myCallback, IntPtr.Zero);
yoffset = (int)((width - k) / 2);
}
using (Graphics g = Graphics.FromImage(thumb))
{
g.DrawImage(tmp, xoffset, yoffset, tmp.Width, tmp.Height);
}
}
}
using (Graphics g = Graphics.FromImage(thumb))
{
g.DrawRectangle(Pens.Green, 0, 0, thumb.Width - 1, thumb.Height - 1);
}
return thumb;
}
public bool ThumbnailCallback()
{
return true;
}
}
NOTE :- its working on the very fist time....u can load pictures as many as u can on ur own any loaction's but when u load again on second time the previous pictures will disapired and appier new picture on top.......
if some one can slove that.....just be kind to reply to this......
thx a lot.......u all.....
asela WijesooriyaPosted Feb 16, 2009, 9:41 PM
hi,
NEED HELP AGAIN.......
i need to know how can i give open dialog box file path to here...
string[] pathes = new string[] { to here};
if i put like this
string[] pathes = new string[] { @"c:\pic\h.jpg"};
i can load picture but its static.when user wants to load many pictues in to an array using open load box, if i could give the open dialog box path to there i can load dynamicaly. isn't it????
i mean by open dialog box path is......some thing like when we use pictureBox....
EG: - pictureBox1.Image = image.fromFile(dlg.filname);
is there got way to give path like this.....to array......???
pl if anyone can help me.....thx a lot u all.....
asela WijesooriyaPosted Feb 16, 2009, 9:37 PM
hi...
thx for ur reply......
i found this from msdn......its nice.....just use listView Box.......
public partial class Form10 : Form
{
public Form10()
{
InitializeComponent();
}
private void Form10_Load(object sender, EventArgs e)
{
ImageList imageList = new ImageList();
imageList.ImageSize = new Size(108, 108);
imageList.ColorDepth = ColorDepth.Depth32Bit;
string[] pathes = new string[] { @"c:\pic\h.jpg", @"c:\pic\1.jpg",
@"c:\pic\1.gif",
@"c:\images\1.gif"};
foreach (string path in pathes)
{
Image img1 = Image.FromFile(path);
imageList.Images.Add(getThumbnaiImage(imageList.ImageSize.Width, img1));
}
for (int j = 0; j < pathes.Length; j++)
{
this.listView1.Items.Add("Image\n aaaaaa\r\n bbbbbbbbbbbbbbbbbbbbb ");
this.listView1.Items[j].ImageIndex = j;
}
this.listView1.View = View.LargeIcon;
this.listView1.LargeImageList = imageList;
}
private Image getThumbnaiImage(int width, Image img)
{
Image thumb = new Bitmap(width, width);
Image tmp = null;
//If the original image is small than the Thumbnail size, just draw in the center
if (img.Width < width && img.Height < width)
{
using (Graphics g = Graphics.FromImage(thumb))
{
int xoffset = (int)((width - img.Width) / 2);
int yoffset = (int)((width - img.Height) / 2);
g.DrawImage(img, xoffset, yoffset, img.Width, img.Height);
}
}
else //Otherwise we have to get the thumbnail for drawing
{
Image.GetThumbnailImageAbort myCallback = new
Image.GetThumbnailImageAbort(ThumbnailCallback);
if (img.Width == img.Height)
{
thumb = img.GetThumbnailImage(
width, width,
myCallback, IntPtr.Zero);
}
else
{
int k = 0;
int xoffset = 0;
int yoffset = 0;
if (img.Width < img.Height)
{
k = (int)(width * img.Width / img.Height);
tmp = img.GetThumbnailImage(k, width, myCallback, IntPtr.Zero);
xoffset = (int)((width - k) / 2);
}
if (img.Width > img.Height)
{
k = (int)(width * img.Height / img.Width);
tmp = img.GetThumbnailImage(width, k, myCallback, IntPtr.Zero);
yoffset = (int)((width - k) / 2);
}
using (Graphics g = Graphics.FromImage(thumb))
{
g.DrawImage(tmp, xoffset, yoffset, tmp.Width, tmp.Height);
}
}
}
using (Graphics g = Graphics.FromImage(thumb))
{
g.DrawRectangle(Pens.Green, 0, 0, thumb.Width - 1, thumb.Height - 1);
}
return thumb;
}
public bool ThumbnailCallback()
{
return true;
}
}
realy its nice....can load many static pictures.......using list view box....in thumbnail type.......auto creat scroll bar tooo
Len La SalaPosted Feb 16, 2009, 12:09 AM
there are other ways to do this, you need to explore these.
---------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication3
{
public partial class Form1 : Form
{
//array of picture boxes
PictureBox[] p = new PictureBox[0];
int last=0; //index to last pcture box added to array
int row = 0; //current row of picture boxes
int rowCount = 0; //number of rows of picture boxes
int rowTop = 5; //picture box top for current row
int rowLeft = 5; //picture box left for current row
int maxImages = 10;
string[] fileNames = new string[maxImages-1];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//this is hard coded array of file names, you could get these from a directory
//instead of using an array of hard coded names
fileNames[0] = "file1.gif";
fileNames[1] = "file2.gif";
fileNames[2] = "file3.gif";
fileNames[3] = "file4.gif";
fileNames[4] = "file5.gif";
fileNames[5] = "file6.gif";
fileNames[6] = "file7.gif";
fileNames[7] = "file8.gif";
fileNames[8] = "file9.gif";
fileNames[9] = "file10.gif";
for(int i=0; i
//for height you can do this
if(row == 2) //exit loop,
break;
Array.Resize(ref p, p.Length +1);
p[p.Length-1] = new PictureBox();
p[p.Length-1].Text = (p.Length-1).ToString();
p[p.Length-1].BorderStyle = BorderStyle.Fixed3D;
p[p.Length-1].Width = 60;
p[p.Length-1].Height = 60;
//this will load the image from the image array
p[p.Length-1].ImageLocation = @"C:\Images\" + fileNames[i];
//now check that next image will not exceed width of forms client area
p[p.Length-1].Left = rowLeft;
//to limit the number of boxes on each row, 5 image boxes 63 wide and 5 pixcell space
// = 300 + 25
//you could also check how many boxes on the row
//if(rowCount == 5)
if((p[p.Length-1].Left + p[p.Length-1].Width + 5) >= 325)
{
row ++;
//assuming that all picture boxes will be the same height incement the value
//of rowTop to height of picture box on first row + 5 pixcell spacing
rowTop += p[0].Height +5;
//reset left to left pos of fist picture box on current row
rowLeft =5;
p[p.Length-1].Left = rowLeft;
}
else
{
if(rowCount == 0) //1st picture box on first row
{
p[p.Length-1].Left = 5;
p[p.Length-1].Top = rowTop;
}
else
{
//add width of last picture box + 5 pxcells to rowLeft
p[p.Length-1].Left = p[last].Left + p[last].Width + 5;
rowLeft += p[p.Length-1].Width +5;
}
p[p.Length-1].Top = rowTop;
rowCount++; //increment row count
}
p[p.Length-1].Location = new Point(p[p.Length-1].Left, rowTop);
this.Controls.Add(p[p.Length-1]);
last = p.Length-1;
}
}
}
}
asela WijesooriyaPosted Feb 15, 2009, 9:36 PM
hi,
sorry for delay reply.....& thx a lot for ur reply.
i tried out that codes.it can successfuly creat the picBox array. but it couldn't load picture......to load images, when i put the image location like....
" Image.FromFile(dlg.FileName);"is got error......can u just tell me how should be that......????
and one more i need to limited the length of picBox to the left......bcz its take the application length and creat array according to application length.so how can i limited to 5 picBoxs to the left and load according to application hight to down.....
want to say thx again for ur help.....realy its a big help to me....
thx a lot.....
asela WijesooriyaPosted Feb 13, 2009, 9:16 AM
hi satish.....
thank u very much u to be kind to reply me......bro.... do u know the code in c#....i mean covert this code u sent to c#.....please.....hope u'll reply soon.....thx again.....
Satish MahapatraPosted Feb 12, 2009, 9:36 AM
Hi Asela,
Ii think you can use a datalist.inside it you can use a image control as a template.then set it's imageurl as bind the datafield from database.
like:ImageUrl='<%#bind("Image_Path") %>'
you can set the image size as your wish.