Solved

C# - Creating PNG PictureBoxes function problem

Posted on 2007-04-04
18
416 Views
Last Modified: 2008-02-01
Hi guys 'n gals,

I need some help with my C# function....

It is meant to:

connect to connect to MySQL (this works)
query a table (this works)
loop through the results and create picture boxes from *.png files located in a subfolder in the bin\ directory titled "node_graphics" (does not work)

No errors are thrown, it merely does not work. Could somebody take a look and tell me what I am doing wrong please?


Cheers!



CODE:



private void PopulateNodeCat()
        {
            MySqlConnection conn = new MySqlConnection(connString);
            DataSet ds = new DataSet();
            conn.Open();
            MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM tblNodeCat", conn);
            adapter.Fill(ds);
            conn.Close();

            int intX = panel2.Left + 2;
            int intY = panel2.Top + 2;

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                PictureBox pb;
                pb = new PictureBox();
                pb.Name = "NodeCatPic_" + dr["UID"].ToString();
                pb.Height = 100;
                pb.Width = 100;
                pb.SizeMode = PictureBoxSizeMode.StretchImage;
                pb.Location = new Point(intX, intY);
                pb.Image = Image.FromFile("node_graphics\\" + dr["Filename"]);

                this.panel2.Controls.Add(pb);

                if (intY == panel2.Top + 2)
                {
                    intY = intY + 105;
                }
                else
                {
                    intY = panel2.Top + 2;
                    intX = intX + 105;
                }
            }
        }
0
Comment
Question by:Cyber-Drugs
  • 9
  • 7
  • 2
18 Comments
 
LVL 5

Expert Comment

by:Yttribium
ID: 18853502
Can elaborate what you mean by "does not work"?  I tried your code and it shows the images just fine.  It appears to work pretty good.
What are you using?

I used VS 2005, .NET 2.0.  
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18853675
Sorry for not being specific enough, basically I meant the images do not display...

I am using VS 2005, .NET 2.0

Same as yourself, I am a bit lost on what to try now that you said it works for you...
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18853730
Do the images actually work in say, paint or I.E?
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18853752
They do, yes. I am basically porting over an application I wrote in HTML/JavaScript/AJAX into a C# application, and these images are a copy of the ones used in my existing system, which work perfectly.


Just to confirm, this is what the directory structure is at the moment:


bin\myApp.exe
bin\node_graphics\pic1.png
bin\node_graphics\pic2.png
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18853805
Did you try to use jpg or gif format?
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18853943
I just tried with JPG, and it still gave the same result. Just added a line to that code to add the filepaths to a multiline textbox, and got the following results:

node_graphics\workstation_network.png
node_graphics\PCI-card_network.png
node_graphics\laptop.png
node_graphics\office-building.png
node_graphics\houses.png
node_graphics\printer2.png
node_graphics\printer_network.png
node_graphics\workstation2.png
node_graphics\pda_write.png
node_graphics\modem_earth.png
node_graphics\cubes.png
node_graphics\server_network.png
node_graphics\cubes.png
node_graphics\test.jpg


Any other ideas?
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18853972
Did you override the on_paint function at any point?
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18854050
It is possible that you have a troube in other part of code. Try to create a sample from scratch with your written /here/ functrion.
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18854274
I've not overwritten the function as it's a fresh project, this is the only bit of code inside it at the moment.


I have althought just tried something, which displayed the problem (now I need to know how to fix it). If you change:

this.panel2.Controls.Add(pb);

for

this.Controls.Add(pb);

Then it all works, so any ideas on how to fix it so that the images are rendered inside panel2, AND display?

Cheers!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:Yttribium
ID: 18854325
What are the properties of the panel?
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18854340
           //
            // panel2
            //
            this.panel2.Location = new System.Drawing.Point(620, 27);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(160, 250);
            this.panel2.TabIndex = 2;


Pretty much standard from what I can see...
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18854349
Is this in the code as well?
this.Controls.Add(this.panel2);
I'm sure it would be, but your problem is extraordinary... so anything is possible.
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18854406
It is indeed, it's really baffling... I don't see why it would be hidden...

Can I add scrollbars to my panel to make sure that the images are not maybe displayed off the screen?
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18854418
Try do this code on a new form, with a single panel, see if it's even possible.  If this works, you can compare.  If it still doesn't work, then possibly you may not have the latest .NET 2.0 patches...
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18854582
I found your problem, I'm working on a solution quick.  Give me a few minutes please.
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18854595
It's your positioning of X,Y inside the panel, it's relative to the panel, not the screen or form.
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18856335
AHA! That would explain everything, cheers Yttribium!!

Problem solved!!!
0
 
LVL 5

Accepted Solution

by:
Yttribium earned 500 total points
ID: 18856350
No problem, I was in a rush when I wrote the answer, so in benefit to future readers that may look at this:

 The Location(x,y) coordinates of the pictureboxes he attached to the Panel, is relative to the panel.  So 0,0 would be top left, inside the panel, not of the main form.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now