Add Image to Grid - WPF - c#

I am wondering how I could set an Image to "grid_Main", if I have created all my images in a loop (See Code).
Code:
private void MoleImageMaker()
{
NumofImages = TUtils.GetIniInt(Moleini, "NumPictures", "pictures", 8);
String MoleImageFunction = TUtils.GetIniFileString(Moleini, "ImagePath", "PictureFile", Root + "mole2.png");
for (int i = 0; i < NumofImages; i++)
{
Image mole = new Image();
mole.Source = new BitmapImage(new Uri(MoleImageFunction));
mole.Name = "Mole" + i;
}
}

Something like this
for (int i = 0; i < NumofImages; i++)
{
Image mole = new Image();
mole.Source = new BitmapImage(new Uri(MoleImageFunction));
mole.Name = "Mole" + i;
Grid.SetColumn(mole, i);
grid_Main.Children.Add(mole);
}

Related

c# how to make matching cards stay turned on grid? (memory game)

I have a problem with my matching/memory game that i'm making for a school project.
I want two of the same cards to stay after they're clicked, and when two different cards are clicked they will return to the backside image of the card.
I'm a total beginner so if someone could explain this in a really easy way to me, that would help me a lot. Currently i'm stuck on this.
This is my code thus far for loading the images from a list and placing them on the grid:
private void AddImage()
{
List<ImageSource> images = GetImagesList();
Random random = new Random();
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
Image ImageOnBacksideOfCard = new Image();
ImageOnBacksideOfCard.Source = new BitmapImage(new Uri("project/achterkant.png", UriKind.Relative));
//Ruimte tussen kaartjes
Thickness margin = ImageOnBacksideOfCard.Margin;
margin.Top = 10;
ImageOnBacksideOfCard.Margin = margin;
ImageOnBacksideOfCard.MouseDown += new MouseButtonEventHandler(CardClick);
//Randomize
positie1 = random.Next(images.Count);
ImageOnBacksideOfCard.Tag = images[positie1];
images.RemoveAt(positie1);
Grid.SetColumn(ImageOnBacksideOfCard, col);
Grid.SetRow(ImageOnBacksideOfCard, row);
grid.Children.Add(ImageOnBacksideOfCard);
}
}
//Load pictures
private List<ImageSource> GetImagesList()
{
List<ImageSource> images = new List<ImageSource>();
for (int i = 0; i < 16; i++)
{
int imageNr = i % 8 + 1;
ImageSource source = new BitmapImage(new Uri("project/" + imageNr + ".jpg", UriKind.Relative));
images.Add(source);
}
return images;
}
//Cards turn on click
private void CardClick(object sender, MouseButtonEventArgs e)
{
Image card = (Image)sender;
ImageSource front = (ImageSource)card.Tag;
card.Source = front;
}
//Reset
public void Reset()
{
grid.Children.Clear();
AddImage();
}

Intersecting rectangles from array

private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < pala.cantLetras; i++)
{ guiones[i] = new Label();
guiones[i].Text = "_";
guiones[i].Font = new Font("Berlin Sans FB Demi", 25);
guiones[i].Size = new Size(18, 37);
guiones[i].Name = "guion" + i;
guiones[i].ForeColor = Color.Black;
guiones[i].BackColor = Color.Transparent;
guiones[i].Location = new Point(x, 341);
this.Controls.Add(guiones[i]);
recguion[i] = guiones[i].Bounds;
recguion[i] = new Rectangle();
//recguion[i].Location = new Point(x, 341);
x = x + 50;
}
for (int i = 0; i < pala.cantLetras; i++)
{
labels[i] = new Label();
labels[i].Size = new Size(25, 55);
labels[i].Name = "label" + i;
labels[i].Text = pala.palabra[i].ToString();
labels[i].Font = new Font("Berlin Sans FB Demi", 20);
labels[i].ForeColor = Color.Black;
labels[i].BackColor = Color.Transparent;
labels[i].Location = new Point(y, 165);
this.Controls.Add(labels[i]);
posRandom[i] = y;
reclabel[i] = labels[i].Bounds;
reclabel[i] = new Rectangle();
// reclabel[i].Location = new Point(y, 165);
y = y + 40;
}
}
I need to know when reclabel[] intersects the recguion[] corresponding to that number.
Ex: reclabel[1] Intersects recguion[1] but only that one, if it intersects another it has to say that it's wrong.
The rectangles have inside(or that's what I a'm trying) labels[] and guiones[]
This is what I have tryied but it doesnt work.
private void intersecta()
{
int cont = 0;
for (int i = 0; i < pala.cantLetras; i++)
{
for (int j = 0; j < pala.cantLetras; j++)
{
if (i==j)
{
Rectangle intersect = Rectangle.Intersect(reclabel[i], recguion[j]);
if (intersect != Rectangle.Empty)
{
MessageBox.Show("Intersection!");
cont++;
}
}
if (cont != 0)
{
i = pala.cantLetras - 1;
j = pala.cantLetras - 1;
}
}
}
}
Thank you!
There is no need for a nested loop. Just loop through one array and check both rectangles at that index with .IntersectsWith. My apologies if there are any syntax errors, I don't have access to Visual Studio at the moment.
For(int i = 0; i < Array1.Length; i++)
{
if(Array1[i].IntersectsWith(Array2[i]))
{
//Intersected
}
}
But also, as Andrew pointed out, you have a serious problem here:
reclabel[i] = new Rectangle();
You are just overwriting all your data with a new instance (of a different type!).

Displaying an array of images in picturebox?

I'm very new to visual C# I want to display an array of images in a picture box
Here's my code:
string[] list = Directory.GetFiles(#"C:\\pictures", "*.jpg");
Image[] images = new Image[5];
for (int index = 0; index < 5; index++)
{
//HERE IS WHERE IM STUCKED WITH
picturebox[index] = Image.FromFile(list[index]);
}
Edit-1 : This answer has a scope limited to Win-Forms C#.
You need certain assemblies added in your application before using this code.
using System.IO;
using System.Windows.Forms;
Edit ended;
Original Answer
You have to draw all image to one image for displaying them in single picturebox
That is bit complex you can use mutiple pictureboxes
In following code they are created dynamically according to need:
// For confirm visibility of all images set
this.AutoScroll = true;
string[] list = Directory.GetFiles(#"C:\pictures", "*.jpg");
PictureBox[] picturebox= new PictureBox[list.Length];
int y = 0;
for (int index = 0; index < picturebox.Length; index++)
{
this.Controls.Add(picturebox[index]);
// Following three lines set the images(picture boxes) locations
if(index % 3 == 0)
y = y + 150; // 3 images per rows, first image will be at (20,150)
picturebox[index].Location=new Point(index * 120 + 20, y);
picturebox[index ].Size = new Size(100,120);
picturebox[index].Image = Image.FromFile(list[index]);
}
The answer provided throws an object reference exception. Otherwise thanks for the example!
for (int index = 0; index < picturebox.Length; index++)
{
this.Controls.Add(picturebox[index]);
// Following three lines set the images(picture boxes) locations
should be
for (int index = 0; index < picturebox.Length; index++)
{
picturebox[index] = new PictureBox();
this.Controls.Add(picturebox[index]);
// Following three lines set the images(picture boxes) locations
Use picturebox[index].Image = Image.FromFile(list[index]);
//this code help you to work with picturebox in arraye
public partial class Form_Begin : Form
{
PictureBox[] pictureBoxs = new PictureBox[50];
public Form_Begin()
{
InitializeComponent();
pictureBoxs[0] = pictureBox1;
pictureBoxs[1] = pictureBox2;
pictureBoxs[2] = pictureBox3;
pictureBoxs[3] = pictureBox4;}
List<PictureBox> pictureBoxes = new List<PictureBox>();
private void buttonX1_Click(object sender, EventArgs e)
{
for (int i = 0; i <2; i++)
{
pictureBoxs[i].Image =your_name_project.Properties.Resources.Image_1; // Load Image_1 from Resources on property of picturebox
}
for (int i = 2; i < 4; i++)
{
pictureBoxs[i].Image =your_name_project.Properties.Resources.Image_2; // Load Image_12 from Resources on property of picturebox
}
private void picbutton_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
PictureBox[] picture = new PictureBox[5];
int x = 0;
int y = 15;
for (int index = length; index < picture.Length; index++)
{
picture[index] = new PictureBox();
picture[index].Size = new Size(100, 50);
open.Title = "OPen Image";
open.Filter = "JPG Files (*.jpg)|*.jpg|JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|GIF Files (*.gif)|*.gif";
DialogResult result = open.ShowDialog();
if (result == DialogResult.OK)
{
picture[index].BackgroundImage = new Bitmap(open.FileName);
picture[index].SizeMode = PictureBoxSizeMode.AutoSize;
listBox1.Controls.Add(picture[index]);
if ((x % 3 == 0) && (index != 0))
{
y = y + 150; // 3 images per rows, first image will be at (20,150)
x = 0;
}
picture[index].Location = new Point(x * 210 + 20, y);
picture[index].Size = new Size(200, 150);
x++;
}
}
}

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