Issue with FlowLayoutPanel - c#

I want to have list of PictureBox inside the FlowLayoutPanel.These Picture box with images are added dynamically at run time. Now the problem I have got is, I have to add more than picturebox to panel and when I do that the picture box at the end seems to be missing though it seems like the space for those images are allocated.So how can I overcome this problem.
panel1.Controls.Clear();
Point NewLocation = new System.Drawing.Point(0, 0);
int CurrentImgIndex = 0;
foreach (ScannedImages image in Global.ScannedReportList[0].ImageCollection)
{
panel1.SuspendLayout();
PictureBox pbTemp = new PictureBox();
pbTemp.BackColor = System.Drawing.Color.Transparent;
pbTemp.Location = NewLocation;
pbTemp.Name = "picImage";
pbTemp.Size = new System.Drawing.Size(757*2, 980*2);
pbTemp.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
pbTemp.TabIndex = 7;
pbTemp.TabStop = false;
pbTemp.Tag = CurrentImgIndex + 1;
pbTemp.DragDrop += new System.Windows.Forms.DragEventHandler(this.PbTemp_DragDrop);
pbTemp.DragOver += new System.Windows.Forms.DragEventHandler(this.PbTemp_DragOver);
pbTemp.Click += new EventHandler(this.pbTemp_Click);
try
{
//pm
Image img = Global.ScannedReportList[0].ImageCollection[CurrentImgIndex].GetImageObject();
txtCurrentPage.Text = (CurrentImageIndex + 1).ToString();
//pm image
RotateAngle = Global.ScannedReportList[0].ImageCollection[CurrentImgIndex].RotationAngle;
//drpGroupTool.SelectedIndex = Global.ScannedReportList[0].ImageCollection[CurrentImgIndex].DocumentGroup;
switch (RotateAngle)
{
case 90:
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
break;
case 180:
img.RotateFlip(RotateFlipType.Rotate180FlipNone);
break;
case 270:
img.RotateFlip(RotateFlipType.Rotate270FlipNone);
break;
}
//pm
m_szOriginal = img.Size;
pbTemp.Image = img;
if (Brightness != 0)
pbTemp.Image = AdjustBrightness(new Bitmap(pbTemp.Image), Brightness);
if (Contrast != 0)
pbTemp.Image = AdjustContrast1(new Bitmap(pbTemp.Image), Contrast);
panel1.Controls.Add(pbTemp);
panel1.ResumeLayout();
this.panel1.PerformLayout();
this.panel1.Refresh();
//pm
if (RotateAngle == 90 || RotateAngle == 270)
{
NewLocation.Y += img.Width;
}
else if (RotateAngle == 0 || RotateAngle == 180)
{
NewLocation.Y += img.Height;
}
//
CurrentImgIndex++;
this.Refresh();
index[CurrentImgIndex] = CurrentImgIndex + 1;
}
catch
{
pbTemp.Image = null;
}

Related

How to speed up rendering of an image on picturebox.image and other elements?

There is a program and its form, onto which images are loaded from the network. To change the image, I press button.click. However, there is one moment, namely: after clicking on the button, in theory, the next image should appear instantly (the speed is similar to that of flipping through photos in the "Windows photo viewer")) only with very rare delays). And it appears, but with a delay of one or about one second, and in theory it shouldn't be like this, and there are similar programs of mine, on which it just happens as I need it, without delays! How and is it possible in this code to speed up the rendering / appearance of images on picturebox.image and other elements that appear along with it? Perhaps even in the code there are elements that should not be at all and you can throw them out of the code:
private void ricVision()
{
try
{
if (!this.locker && (this.waitVision.Count > 0))
{
this.textBox2.Focus();
this.locker = true;
string[] strArray = this.waitVision[0];
if (strArray[9].ToString() != "-")
{
this.textBox2.Text = strArray[9].ToString();
}
int num = Convert.ToInt32(strArray[0].ToString());
int num2 = Convert.ToInt32(strArray[1].ToString());
int index = Convert.ToInt32(strArray[6].ToString());
this.visionType = strArray[2].ToString();
this.Registor = strArray[3].ToString();
this.WordsTwo = strArray[4].ToString();
this.label7.Text = this.Registor;
this.label8.Text = strArray[4].ToString();
this.label16.Text = strArray[10].ToString();
{
if (label16.Text.Length > 5)
label16.Text = label16.Text.Substring(0, 5);
};
this.label19.Text = strArray[12].ToString();
this.label21.Text = this.BASE64;
this.label23.Text = strArray[13].ToString();
this.BASE64 = strArray[14].ToString();
DoubleBuffered = true;
if (label7.Text == "False")
{
dataGridView1.Rows[index].Cells[1].Style.BackColor = Color.Empty;
DoubleBuffered = true;
}
else if (label7.Text == "True")
{
dataGridView1.Rows[index].Cells[1].Style.BackColor = Color.Tomato;
DoubleBuffered = true;
}
if (label8.Text == "False")
{
dataGridView1.Rows[index].Cells[3].Style.BackColor = Color.Empty;
DoubleBuffered = true;
}
else if (label8.Text == "True")
{
dataGridView1.Rows[index].Cells[3].Style.BackColor = Color.Tomato;
DoubleBuffered = true;
}
this.label10.Text = this.visionType;
this.pictureBox1.Width = num;
this.pictureBox1.Height = num2;
this.panel2.Visible = true;
{
this.label13.Text = this.dataGridView1.Rows[index].Cells[3].Value.ToString();
if (double.Parse(label13.Text) < 0)
{
panel2.BackColor = Color.GreenYellow;
this.dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.GreenYellow;
if (label8.Text == "True")
{
panel2.BackColor = Color.RoyalBlue;
dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.RoyalBlue;
label13.ForeColor = Color.RoyalBlue;
DoubleBuffered = true;
}
}
else
{
panel2.BackColor = Color.Gainsboro;
if (label8.Text == "False")
{
panel2.BackColor = Color.Gainsboro;
dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.Gold;
label13.ForeColor = Color.Green;
DoubleBuffered = true;
}
}
};
string s = strArray[5].ToString().Substring(0, strArray[5].ToString().Length - 9).Replace(#"\/", "/");
this.visionId = strArray[5].ToString().Substring(strArray[5].ToString().Length - 9);
this.visionThread = Convert.ToInt32(strArray[6]);
this.label12.Text = strArray[7].ToString();
Bitmap bitmap = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
string path = "";
this.pictureBox1.DrawToBitmap(bitmap, this.pictureBox1.ClientRectangle);
this.pictureBox1.Refresh();
DoubleBuffered = true;
this.ResumeLayout(false);
Form1 form = (Form1)Application.OpenForms[0];
this.ActiveControl = textBox2;
TopMost = true;
TopLevel = true;
textBox2.Select();
form.Activate();
form.Focus();
this.showCaptcha();
this.timer1.Enabled = true;
ImageFormat bmp = null;
Graphics gdi = Graphics.FromImage(bitmap);
using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(s)))
{
this.pictureBox1.Image = Image.FromStream(stream);
}
}
}
catch (Exception exception)
{
MessageBox.Show(exception.ToString());
}
}

Putting picturebox on picturebox

I have a problem adding picture boxes using Controls class on another picture box that already exists.
when i load the form i don't see any changes because the "coded" pictures are under the main picture (Arena)
Here is my code:
void drawSpikes()
{
PictureBox[] spikes = new PictureBox[Arena.Height / 25 * 2];
int position = 0;
byte wall = 1;
byte spike_count = 0;
for (int i = 0; i < Arena.Height / 25 * 2; i++)
{
spikes[i] = new PictureBox();
}
foreach (var Spike in spikes)
{
if (spike_count == 18) wall = 2;
Spike.Size = new Size(25, 25);
if (wall == 1)
{
Spike.Location = new Point(21, position);
Spike.BackColor = Color.Yellow;
}
if (wall == 2)
{
Spike.Location = new Point(position, 250);
Spike.BackColor = Color.Yellow;
}
if (position == 450) position = 0;
position += 25;
spike_count += 1;
Controls.Add(Spike);
}
}
How can i fix it ?
sorry for the function disconnection (i am new in stack overflow).
You can use the BringToFront() function after adding the picture box to the controls collection:
Controls.Add(Spike);
Spike.BringToFront();

Change image in picturebox array by button click

I want to generate and change the image in the PictureBox array by button click.
Here is what I have tried so far.
public Form1() {
InitializeComponent();
}
PictureBox[] boxes = new PictureBox[6];
private void GenerateButton1_Click(object sender, EventArgs e) {
for (int i = 0; i < boxes.Length; i++) {
boxes[i] = new PictureBox(); //set the pointer to a new PictureBox instance
if (i == 0) boxes[i].Location = new System.Drawing.Point(3, 3);
if (i == 1) boxes[i].Location = new System.Drawing.Point(221, 3);
if (i == 2) boxes[i].Location = new System.Drawing.Point(439, 3);
if (i == 3) boxes[i].Location = new System.Drawing.Point(3, 210);
if (i == 4) boxes[i].Location = new System.Drawing.Point(221, 210);
if (i == 5) boxes[i].Location = new System.Drawing.Point(439, 210);
boxes[i].Size = new System.Drawing.Size(200, 200);
boxes[i].Image = Image.FromFile(Application.StartupPath + "\\red.PNG"); //for setting its image
}
this.Controls.AddRange(boxes);
}
private void button1_Click(object sender, EventArgs e) {
int i = 3;
int signal = 1;
boxes[i].SizeMode = PictureBoxSizeMode.StretchImage;
if (signal == 0) boxes[i].Image = Image.FromFile(Application.StartupPath + "\\red.PNG");
if (signal == 1) boxes[i].Image = Image.FromFile(Application.StartupPath + "\\green.PNG");
if (signal == 2) boxes[i].Image = Image.FromFile(Application.StartupPath + "\\grey.PNG");
}
}
}
by doing button1 click, the image will change from red.PNG to green.PNG or grey.PNG depending on the condition, however, I have to redo the image Properties declaration, for example, boxes[i].SizeMode = PictureBoxSizeMode.StretchImage;. Otherwise the PictureBox lose its properties.
Is there any simpler way to do this.
Thank you in advance.

winforms refresh multiple panels

I am building a poker game where I need to show multiple poker hands at the same time. I can either show 1, 3, 5, or 10.
I have 1 main hand that will display all the time and I activate different views based on the amount of hands the player wants to play.
I have 2 main panels (main_hand_panel, and extra_hands_panel)
I add all the Panels in a List and when it's time, I call ShowHand on it.
I add the first poker hand to main_hand_panel like this:
Point startPosition = new Point(0, 0);
ComponentResourceManager resources = new ComponentResourceManager(typeof(MainScreen));
var mainPokerHand = new PokerPanel(startPosition, main_hand_panel.Size, offset, cardSize);
mainPokerHand.Initialize(resources);
this.main_hand_panel.Controls.Add(mainPokerHand);
allHands.Add(mainPokerHand);
Then depending on which screen I'm showing I draw and add the additional hands like this (five hands example shown)
public void DrawFivePlay(ComponentResourceManager resources)
{
Point startPosition = new Point(0, 0);
var containerSize = extra_hands_panel.Size;
containerSize.Height = Convert.ToInt32(containerSize.Height / 2);
containerSize.Width = Convert.ToInt32(containerSize.Width / 2);
for (int i = 0; i < 2; i++)
{
startPosition.Y = containerSize.Height * i;
var pokerHand = new PokerPanel(startPosition, containerSize, scale(offset, .30), scale(cardSize, .8));
pokerHand.Initialize(resources);
this.extra_hands_panel.Controls.Add(pokerHand);
allHands.Add(pokerHand);
}
startPosition.X = containerSize.Width;
startPosition.Y = 0;
for (int i = 0; i < 2; i++)
{
startPosition.Y = containerSize.Height * i;
var pokerHand = new PokerPanel(startPosition, containerSize, scale(offset, .30), scale(cardSize, .8));
pokerHand.Initialize(resources);
this.extra_hands_panel.Controls.Add(pokerHand);
allHands.Add(pokerHand);
}
}
When I'm ready to show the hands, I call reveal_click which goes through all the hands in List and displays them.
private void reveal_Click(object sender, EventArgs e)
{
foreach (var hand in allHands)
{
hand.ShowHand();
}
Application.DoEvents();
}
The interesting part is that it doesn't display the main hand, it will display all the others, but the only time it displays the first hand is when the program just started and we are playing only 1 hand. If I show any of the other play option, the first hand will not display anymore.
Here is the PokerPanel code:
namespace TEX_DrawPoker
{
public class PokerPanel : Panel
{
private PictureBox pictureBox1_5;
private PictureBox pictureBox1_1;
private PictureBox pictureBox1_2;
private PictureBox pictureBox1_3;
private PictureBox pictureBox1_4;
Timer drawTimer = new Timer();
int timerTick = 0;
string[] pokerHand;
Size panelSize = new Size(5*229, 275);
Size cardSize = new Size(146, 202);
Point startPosition = new Point(0, 3);
Point firstCardPosition = new Point(0, 0);
Point offset = new Point(160, 0);
public PokerPanel(Point _startPosition,Size _panelSize, Point _offset, Size _cardSize)
{
drawTimer.Tick += new EventHandler(startDisplay);
drawTimer.Interval = 100;
drawTimer.Enabled = false;
panelSize = _panelSize;
cardSize = _cardSize;
offset = _offset;
startPosition = _startPosition;
}
public void startDisplay(object sender, EventArgs e)
{
switch (timerTick)
{
case 0:
this.pictureBox1_1.Image = (Image)(new Bitmap(Image.FromFile(".\\img\\cards\\" + pokerHand[timerTick] + ".png"), cardSize));
break;
case 1:
this.pictureBox1_2.Image = (Image)(new Bitmap(Image.FromFile(".\\img\\cards\\" + pokerHand[timerTick] + ".png"), cardSize));
break;
case 2:
this.pictureBox1_3.Image = (Image)(new Bitmap(Image.FromFile(".\\img\\cards\\" + pokerHand[timerTick] + ".png"), cardSize));
break;
case 3:
this.pictureBox1_4.Image = (Image)(new Bitmap(Image.FromFile(".\\img\\cards\\" + pokerHand[timerTick] + ".png"), cardSize));
break;
case 4:
this.pictureBox1_5.Image = (Image)(new Bitmap(Image.FromFile(".\\img\\cards\\" + pokerHand[timerTick] + ".png"), cardSize));
break;
}
if (timerTick >= 4)
{
drawTimer.Stop();
timerTick = 0;
}
else
{
timerTick++;
}
}
public void ShowHand()
{
pokerHand = Deck.shuffle();
drawTimer.Start();
}
public void Reset()
{
Image back = Image.FromFile(".\\img\\cards\\back.png");
this.pictureBox1_1.Image = (Image)(new Bitmap(back, cardSize));
this.pictureBox1_2.Image = (Image)(new Bitmap(back, cardSize));
this.pictureBox1_3.Image = (Image)(new Bitmap(back, cardSize));
this.pictureBox1_4.Image = (Image)(new Bitmap(back, cardSize));
this.pictureBox1_5.Image = (Image)(new Bitmap(back, cardSize));
}
public void Initialize(ComponentResourceManager resources)
{
Image back = Image.FromFile(".\\img\\cards\\back.png");
this.pictureBox1_1 = new System.Windows.Forms.PictureBox();
this.pictureBox1_2 = new System.Windows.Forms.PictureBox();
this.pictureBox1_3 = new System.Windows.Forms.PictureBox();
this.pictureBox1_4 = new System.Windows.Forms.PictureBox();
this.pictureBox1_5 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1_1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1_2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1_3)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1_4)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1_5)).BeginInit();
this.SuspendLayout();
//
// hand_1
//
this.Controls.Add(this.pictureBox1_5);
this.Controls.Add(this.pictureBox1_4);
this.Controls.Add(this.pictureBox1_3);
this.Controls.Add(this.pictureBox1_2);
this.Controls.Add(this.pictureBox1_1);
this.Location = startPosition;
this.Name = "hand_1";
this.Size = panelSize;
this.TabIndex = 0;
//
// pictureBox1_1
//
this.pictureBox1_1.Image = (Image)(new Bitmap(back, cardSize));
this.pictureBox1_1.Location = firstCardPosition;
this.pictureBox1_1.Name = "pictureBox1_1";
this.pictureBox1_1.Size = cardSize;
this.pictureBox1_1.TabIndex = 4;
this.pictureBox1_1.TabStop = false;
//
// pictureBox1_2
//
var positionCard_2 = firstCardPosition;
positionCard_2.Offset(offset);
this.pictureBox1_2.Image = (Image)(new Bitmap(back, cardSize));
this.pictureBox1_2.Location = positionCard_2;
this.pictureBox1_2.Name = "pictureBox1_2";
this.pictureBox1_2.Size = cardSize;
this.pictureBox1_2.TabIndex = 3;
this.pictureBox1_2.TabStop = false;
//
// pictureBox1_3
//
var positionCard_3 = positionCard_2;
positionCard_3.Offset(offset);
this.pictureBox1_3.Image = (Image)(new Bitmap(back, cardSize));
this.pictureBox1_3.Location = positionCard_3;
this.pictureBox1_3.Name = "pictureBox1_3";
this.pictureBox1_3.Size = cardSize;
this.pictureBox1_3.TabIndex = 2;
this.pictureBox1_3.TabStop = false;
//
// pictureBox1_4
//
var positionCard_4 = positionCard_3;
positionCard_4.Offset(offset);
this.pictureBox1_4.Image = (Image)(new Bitmap(back, cardSize));
this.pictureBox1_4.Location = positionCard_4;
this.pictureBox1_4.Name = "pictureBox1_4";
this.pictureBox1_4.Size = cardSize;
this.pictureBox1_4.TabIndex = 1;
this.pictureBox1_4.TabStop = false;
//
// pictureBox1_5
//
var positionCard_5 = positionCard_4;
positionCard_5.Offset(offset);
this.pictureBox1_5.Image = (Image)(new Bitmap(back, cardSize));
this.pictureBox1_5.Location = positionCard_5;
this.pictureBox1_5.Name = "pictureBox1_5";
this.pictureBox1_5.Size = cardSize;
this.pictureBox1_5.TabIndex = 0;
this.pictureBox1_5.TabStop = false;
((System.ComponentModel.ISupportInitialize)(this.pictureBox1_5)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1_4)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1_3)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1_2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1_1)).EndInit();
}
}
}
I also use a timer to display the cards so that they stagger.
Any help would be appreciated.
Thanks,
Found my problem,
I was not clearing the controls between hand changes, so I was trying to update a panel that was in the background. Controls.Clear() before moving to the next hand fixed that for me. Sorry for the long post for nothing. :(
Have you considered refreshing the entire form?
Form1.Refresh();
or only refresh the panel
PokerPanel.Refresh();

To clear(reset) dynamically generated picture box

` private void getbtn_Click(object sender, EventArgs e) // To generate Images
{
if (cmbDocType.SelectedIndex > 0)
{
if (con.State != ConnectionState.Open)
con.Open();
string directory = System.IO.Directory.GetDirectoryRoot(System.IO.Directory.GetCurrentDirectory().ToString());
string FileNamePath = directory + "MembersDocuments\\" + GlobalValues.Member_ID + "\\" + cmbDocType.Text;
string[] list = Directory.GetFiles(FileNamePath);
if (list.Length > 0)
{
label1.Text = "";
PictureBox[] picturebox = new PictureBox[list.Length];
int y = 0;
for (int index = 0; index < picturebox.Length; index++)
{
picturebox[index] = new PictureBox();
if (x % 3 == 0)
{
y = y + 150; // 3 images per rows, first image will be at (20,150)
x = 0;
}
picturebox[index].Location = new Point(x * 230 + 20, y);
picturebox[index].Size = new Size(200, 150);
x++;
picturebox[index].Size = new Size(200, 100);
picturebox[index].Image = Image.FromFile(list[index]);
picturebox[index].SizeMode = PictureBoxSizeMode.StretchImage;
picturebox[index].Click += new EventHandler(picturebox_Click);
cmbDocType_SelectedIndexChanged(picturebox[index], e);
this.Controls.Add(picturebox[index]);
}
}
else
{
label1.Text = "No Images to display";
label1.ForeColor = Color.Red;
}
con.Close();
}
else
{
MessageBox.Show("Please select the Document Type");
}
}
`
Can anyone tell me how to clear previous images(result of first call) in dynamically created pictureboxes on a new call. On making new call,previous images should not be seen..in c#
I have combobox named Type.
lets say if i have Aminals,Birds in my Combobox.
on first call pictures of animals will be displayed and on choosing combobox for second time i.e birds, Pictures of Both the types are getting displayed.
i Need to display Pictures of single type at a time. in c#
Thanks;
As suggested by TaW in the comments:
private List<PictureBox> PBs = new List<PictureBox>();
private void getbtn_Click(object sender, EventArgs e) // To generate Images
{
if (cmbDocType.SelectedIndex > 0)
{
foreach(PictureBox pb in PBs)
{
pb.Dispose();
}
PBs.Clear();
if (con.State != ConnectionState.Open)
con.Open();
string directory = System.IO.Directory.GetDirectoryRoot(System.IO.Directory.GetCurrentDirectory().ToString());
string FileNamePath = directory + "MembersDocuments\\" + GlobalValues.Member_ID + "\\" + cmbDocType.Text;
string[] list = Directory.GetFiles(FileNamePath);
if (list.Length > 0)
{
label1.Text = "";
PictureBox PB;
int y = 0;
for (int index = 0; index < list.Length; index++)
{
PB = new PictureBox();
if (x % 3 == 0)
{
y = y + 150; // 3 images per rows, first image will be at (20,150)
x = 0;
}
PB.Location = new Point(x * 230 + 20, y);
PB.Size = new Size(200, 150);
x++;
PB.Size = new Size(200, 100);
PB.Image = Image.FromFile(list[index]);
PB.SizeMode = PictureBoxSizeMode.StretchImage;
PB.Click += new EventHandler(picturebox_Click);
PBs.Add(PB);
this.Controls.Add(PB)
cmbDocType_SelectedIndexChanged(PB, e);
}
}
else
{
label1.Text = "No Images to display";
label1.ForeColor = Color.Red;
}
con.Close();
}
else
{
MessageBox.Show("Please select the Document Type");
}
}

Categories

Resources