Solved

C# - Creating PNG PictureBoxes function problem

Posted on 2007-04-04
18
413 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need help with a query 6 55
asp.net mvc5 3 38
c# regex: extract & replace text between braces 6 30
Get String split 5 33
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 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

11 Experts available now in Live!

Get 1:1 Help Now