display image from listview to picturebox - c#

I want to display image from list to picturebox.
My image is displaying in the picturebox but the problem is it shows the size the of the image in the picturebox that is defined in the list. Can anyone tell me how to enlarge my image size?
here is my piece of code:
private void listView_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListViewItem itm in listView.SelectedItems)
{
int imgIndex = itm.ImageIndex;
if (imgIndex >= 0 && imgIndex < this.documents.Images.Count)
{
// this.documents.Images[imgIndex].Width = 417;
pictureBox.Image = this.documents.Images[imgIndex];
}
}
}
and this is how I am getting images from the database:
ImageList documents = new ImageList();
if (documents.Images.Count < 1)
{
MessageBox.Show("No Documents Found.");
}
else
{
// pictureBox.Image = documents.Images[1];
this.listView.View = View.LargeIcon;
documents.ImageSize = new Size(256, 256);
listView.LargeImageList = documents;
listView.Items.Clear();
for (int j = 0; j < documents.Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
this.listView.Items.Add(item);
}
}

PictureBox has SizeMode property that is used for manipulating image size:
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
More about that on MSDN

-Create a new imagelist (imagelist1)**
-Add images to your imagelist
-Create a new listview (listview1)
-Create a picturebox (picturebox1)
-Create a new button (button1)
-Create another button (button2)**
-Import images from imagelist1 to listview1
private void button1_Click(object sender, EventArgs e)
{
listView1.Scrollable = true;
listView1.View = View.LargeIcon;
imageList1.ImageSize = new Size(100, 100);
listView1.LargeImageList = imagelist1;
for (int i = 0; i < imagelist1.Images.Count; ++i)
{
string s = imagelist1.Images.Keys[i].ToString();
ListViewItem lstItem = new ListViewItem();
lstItem.ImageIndex = i;
lstItem.Text = s;
listView1.Items.Add(lstItem);
}
}
- Set the selected image into your picture box from listview
private void button2_Click(object sender, EventArgs e)
{
if (this != null && listView1.SelectedItems.Count > 0)
{
ListViewItem lvi = listView1.SelectedItems[0];
string imagekeyname = lvi.Text;
if (this.pictureBox1.Image != null)
{
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;
}
//set the selected image into your picturebox
this.pictureBox1.Image = imagelist1.Images[imagekeyname];
}
}

Related

add multiple images from openfiledilog to imagelist and display them in the listview

hello friends i am new to c# i am in a project and i want help
i want to add multiple images from openfiledilog to imagelist and display them in the listview.
its adding the images but showing the same picture
and here goes my code.. please help me
int b = 0;![enter image description here][1]
private void Form1_Load(object sender, EventArgs e)
{
var ofd = new OpenFileDialog();
ofd.Multiselect = true;
ofd.ShowDialog();
for (int z = 1; z <= ofd.FileNames.Length ; z++)
{
Image img = Image.FromFile(ofd.FileName);
string a = b.ToString();
imageList1.Images.Add(a, img);
var listViewItem = listView1.Items.Add("1");
listViewItem.ImageKey = a;
b++;
}
}
You need to iterate over the FileNames array instead of using the FileName property.
int b = 0;
private void Form1_Load(object sender, EventArgs e)
{
var ofd = new OpenFileDialog();
ofd.Multiselect = true;
ofd.ShowDialog();
for (int z = 0; z < ofd.FileNames.Length ; z++)
{
Image img = Image.FromFile(ofd.FileNames[z]);
string a = b.ToString();
imageList1.Images.Add(a, img);
var listViewItem = listView1.Items.Add("1");
listViewItem.ImageKey = a;
b++;
}
}
or
int b = 0;
private void Form1_Load(object sender, EventArgs e)
{
var ofd = new OpenFileDialog();
ofd.Multiselect = true;
ofd.ShowDialog();
foreach (string fileName in ofd.FileNames)
{
Image img = Image.FromFile(fileName);
string a = b.ToString();
imageList1.Images.Add(a, img);
var listViewItem = listView1.Items.Add("1");
listViewItem.ImageKey = a;
b++;
}
}

How to make a grid of colours that can be selected?

How do I make a kind of grid with colours that can be selected and be saved to a field on selection? Like in the chat options in Twitch.
Windows Form provides the ColorDialog
ColorDialog colorDialog = new ColorDialog();
colorDialog.ShowDialog();
The selected Color could be called with:
colorDialog.Color
All you need is TableLayoutPanel and panel for each cell of it:
public partial class MainForm : Form
{
private Color selected_color;
private List<Color> colors;
public MainForm()
{
InitializeComponent();
colors = new List<Color>();
colors.Add(Color.Red);
colors.Add(Color.Green);
colors.Add(Color.Blue);
colors.Add(Color.Yellow);
colors.Add(Color.Teal);
colors.Add(Color.RosyBrown);
colors.Add(Color.Lime);
colors.Add(Color.Gray);
tableLayoutPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble;
for (byte i = 0; i < tableLayoutPanel.Controls.Count; i++)
{
Panel p = tableLayoutPanel.Controls[i] as Panel;
p.BackColor = colors[i];
p.Click += panel_click;
}
}
private void panel_click(object sender, EventArgs e)
{
Panel p = sender as Panel;
selected_color = p.BackColor;
lbl_color.Text = selected_color.ToString();
lbl_color.ForeColor = selected_color;
}
private void btn_showMoreColours_Click(object sender, EventArgs e)
{
Panel[] panels = new Panel[4];
for (byte i = 0; i < panels.Length; i++)
{
panels[i] = new Panel();
panels[i].Dock = DockStyle.Fill;
panels[i].Location = new System.Drawing.Point(3, 3);
panels[i].Name = "panel" + (i + 4);
panels[i].Size = new System.Drawing.Size(123, 100);
panels[i].BackColor = colors[i + 4];
panels[i].Click += panel_click;
tableLayoutPanel.Controls.Add(panels[i]);
}
Size = new Size(Size.Width, Size.Height * 2);
}
}
After clicking a cell you will get Color in field selected_color.
EDIT:
A have added show more colours button as well. It will expand like shown below:
Source code here

LongListSelector and Image_MouseLeftButtonDown issue

How can I manage that the event LongListSelector_SelectionChanged run first, then Image_MouseLeftButtonDown run after that
int count = 0;
Image LastImage = null, curImage = null;
BitmapImage bi1 = new BitmapImage();
BitmapImage bi2 = new BitmapImage();
int itemIndex;
void getImageLink()
{
for (int i = 0; i < CImageControl.lstImage.Count; i++)
s.Add(CImageControl.lstImage[i].ImageLink);
}
private void lstView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var myItem = ((LongListSelector)sender).SelectedItem as CImage;
itemIndex = ((LongListSelector)sender).ItemsSource.IndexOf(myItem);
//MessageBox.Show(myIndex.ToString());
}
then this will run
private void Image_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
string lastImageSource, lastImageAltSource, curImageSource, curImageAltSource;
count++;
if (count % 2 != 0)
{
LastImage = (Image)sender;
lastImageSource = s[itemIndex];
lastImageAltSource = lastImage.Source.ToString();
}
else
{
curImage = (Image)sender;
curImageSource = s[itemIndex];
curImageAltSource = curImage.Source.ToString();
}
}
ImageAltSouce is the display of the image, and I wanted to replace with ImageSource. But I need itemIndex to find index of images in Longlistselector before I can change any source. Since the event Image_MouseLeftButtonDown occur first, so I cant do anything

Clear PictureBox - c#

I'd like to modify this PictureBox Array Project.
i want to put a reset button than will clear all the PictureBox Array it created
more likely the form will be empty again as like from the beginning.
this is some of it's code;
// Function to add PictureBox Controls
private void AddControls(int cNumber)
{
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
}
// When call this function you determine number of controls
}
private void ImagesInFolder()
{
FileInfo FInfo;
// Fill the array (imgName) with all images in any folder
imgName = Directory.GetFiles(Application.StartupPath + #"\Images");
// How many Picture files in this folder
NumOfFiles = imgName.Length;
imgExtension = new string[NumOfFiles];
for (int i = 0; i < NumOfFiles; i++)
{
FInfo = new FileInfo(imgName[i]);
imgExtension[i] = FInfo.Extension; // We need to know the Extension
//
}
}
private void ShowFolderImages()
{
int Xpos = 8;
int Ypos = 8;
Image img;
Image.GetThumbnailImageAbort myCallback =
new Image.GetThumbnailImageAbort(ThumbnailCallback);
MyProgress.Visible = true;
MyProgress.Minimum = 1;
MyProgress.Maximum = NumOfFiles;
MyProgress.Value = 1;
MyProgress.Step = 1;
string[] Ext = new string [] {".GIF", ".JPG", ".BMP", ".PNG"};
AddControls(NumOfFiles);
for (int i = 0; i < NumOfFiles; i++)
{
switch (imgExtension[i].ToUpper())
{
case ".JPG":
case ".BMP":
case ".GIF":
case ".PNG":
img = Image.FromFile(imgName[i]); // or img = new Bitmap(imgName[i]);
imgArray[i].Image = img.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero);
img = null;
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;
// Fill the (Tag) with name and full path of image
imgArray[i].Tag = imgName[i];
imgArray[i].Click += new System.EventHandler(ClickImage);
this.BackPanel.Controls.Add(imgArray[i]);
Xpos = Xpos + 72; // width of image + 8
Application.DoEvents();
MyProgress.PerformStep();
break;
}
}
MyProgress.Visible = false;
}
private bool ThumbnailCallback()
{
return false;
}
private void btnLoad_Click(object sender, System.EventArgs e)
{
ImagesInFolder(); // Load images
ShowFolderImages(); // Show images on PictureBox array
this.Text = "Click wanted image";
}
how can i do that?
sorry i don't have any code for the reset button yet.
i don't know what to do, i am new to c#.
You can just set the image to null:
private void Clear()
{
foreach (var pictureBox in imgArray)
{
pictureBox.Image = null;
pictureBox.Invalidate();
}
}
I will follow this steps to be sure everything will be fred :
private void btnReset_Click(object sender, System.EventArgs e)
{
for(int x = this.BackPanel.Controls.Count - 1; x >= 0; x--)
{
if(this.BackPanel.Controls[x].GetType() == typeof(PictureBox))
this.BackPanel.Controls.Remove(x);
}
for(int x = 0; x < imgArray.Length; x++)
{
imgArray[x].Image = null;
imgArray[x] = null;
}
}
Assuming there are no other child controls in this.Backpanel (the container control that is actually displaying your images), this will probably work:
private void ClearImages() {
this.BackPanel.Controls.Clear();
imgArray = null;
}
Good Luck!
If you are drawing on a pictureBox and you want to clear it:
Graphics g = Graphics.FromImage(this.pictureBox1.Image);
g.Clear(this.pictureBox1.BackColor);
After that you can draw again on the control.
I hope this can help to someone.

add picturebox in windowsform c#

I tried to add picturebox in windowsform on load event-handler but the images didn't appear in the form after loading
attachimage is a picturebox I added it from toolbox ( not by c# )
private void ViewCmap_Load(object sender, EventArgs e)
{
for (int i = 0; i < ConceptProperties.ConceptsMap.Count; i++)
{
conceptattchboxlist.Add(new PictureBox());
conceptattchboxlist[i].Visible = true;
if (ConceptProperties.ConceptsMap[i].Attachments.Count > 0)
{
PictureBox new_attach_box = new PictureBox();
new_attach_box.Image = attachimage.Image;
new_attach_box.Width = attachimage.Width;
new_attach_box.Height = attachimage.Height;
new_attach_box.BackgroundImageLayout = ImageLayout.Stretch;
new_attach_box.SizeMode = PictureBoxSizeMode.StretchImage;
new_attach_box.Location = new Point(ConceptProperties.ConceptsMap[i].Coords[0] + (ConceptProperties.ConceptsMap[i].Coords[2]), ConceptProperties.ConceptsMap[i].Coords[1] + (ConceptProperties.ConceptsMap[i].Coords[3]));
conceptattchboxlist[i] = new_attach_box;
}
}
for (int i = 0; i < ConceptProperties.ConnectionMap.Count; i++)
{
connectionattchboxlist.Add(new PictureBox());
connectionattchboxlist[i].Visible = true;
if (ConceptProperties.ConnectionMap[i].Attachments.Count > 0)
{
PictureBox new_attach_box = new PictureBox();
new_attach_box.Image = attachimage.Image;
new_attach_box.Width = attachimage.Width;
new_attach_box.Height = attachimage.Height;
new_attach_box.BackgroundImageLayout = ImageLayout.Stretch;
new_attach_box.SizeMode = PictureBoxSizeMode.StretchImage;
new_attach_box.Location = new Point(ConceptProperties.ConceptsMap[i].Coords[0] + (ConceptProperties.ConceptsMap[i].Coords[2]), ConceptProperties.ConceptsMap[i].Coords[1] + (ConceptProperties.ConceptsMap[i].Coords[3]));
new_attach_box.Show();
connectionattchboxlist[i] = new_attach_box;
}
}
}
To add a pictureBox or any control use:
PictureBox pic = new Picturebox();
this.Controls.Add(pic);

Categories

Resources