Mira estoy haciendo un ejemplo en c#.net en el que muestro imagenes en
un picturebox, pero no me los muestra sin embargo todo lo demás me
funciona simplemente no me muestra las imégenes
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO ;
namespace WindowsApplication6
{
/// <summary>
/// Descripción breve de Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ComboBox cmb;
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.PictureBox pictureBox1;
/// <summary>
/// Variable del diseñador requerida.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Necesario para admitir el Diseñador de Windows Forms
//
InitializeComponent();
string [] unidades = System.Environment.GetLogicalDrives ();
foreach (string u in unidades)
{
cmb.Items.Add (u.ToUpper ());
}
//
// TODO: agregar código de constructor después de llamar a
InitializeComponent
//
}
/// <summary>
/// Limpiar los recursos que se estén utilizando.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Código generado por el Diseñador de Windows Forms
/// <summary>
/// Método necesario para admitir el Diseñador. No se puede
modificar
/// el contenido del método con el editor de código.
/// </summary>
private void InitializeComponent()
{
this.cmb = new System.Windows.Forms.ComboBox();
this.listBox1 = new System.Windows.Forms.ListBox();
this.treeView1 = new System.Windows.Forms.TreeView();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// cmb
//
this.cmb.Location = new System.Drawing.Point(16, 16);
this.cmb.Name = "cmb";
this.cmb.Size = new System.Drawing.Size(88, 21);
this.cmb.TabIndex = 0;
this.cmb.Text = "comboBox1";
this.cmb.SelectedIndexChanged += new
System.EventHandler(this.cmb_SelectedIndexChanged);
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(16, 176);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(96, 69);
this.listBox1.TabIndex = 2;
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(16, 64);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(96, 88);
this.treeView1.TabIndex = 4;
this.treeView1.AfterSelect += new
System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(136, 56);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(144, 176);
this.pictureBox1.TabIndex = 5;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.treeView1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.cmb);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// Punto de entrada principal de la aplicación.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void cmb_SelectedIndexChanged(object sender, System.EventArgs
e)
{
try
{
System.IO.DirectoryInfo tDirInfo = new System.IO.DirectoryInfo
(cmb.Text );
DirectoryInfo[] Directorios = tDirInfo.GetDirectories ();
treeView1.Nodes.Clear ();
foreach (DirectoryInfo di in Directorios )
{
TreeNode nodo =new TreeNode (di.FullName,3,4);
treeView1.Nodes.Add (nodo);
}
}
catch
{
MessageBox.Show ("El dispositivo no está listo");
}
}
private void treeView1_AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
System.IO.DirectoryInfo tDirInfo = new System.IO.DirectoryInfo
(e.Node.FullPath);
DirectoryInfo[] Directorios = tDirInfo.GetDirectories ();
foreach (DirectoryInfo di in Directorios)
{
TreeNode nodo = new TreeNode (di.Name,3,4);
e.Node.Nodes.Add (nodo.Text);
}
FileInfo [] ficheros = tDirInfo.GetFiles ();
listBox1.Items.Clear ();
foreach ( System.IO.FileInfo fic in ficheros )
{
if (fic.Extension.ToUpper () == ".JPG" || fic.Extension.ToUpper ()
== ".BMP"
|| fic.Extension.ToUpper () == ".GIF" || fic.Extension.ToUpper ()
== ".TIF")
{
listBox1.Items.Add (fic.Name);
}
}
}
private void listBox1_SelectedValueChanged(object sender,
System.EventArgs e)
{
pictureBox1.Invalidate ();
}
private void pictureBox1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
if (treeView1.SelectedNode != null )
{
try
{
Bitmap b = new Bitmap (treeView1.SelectedNode.FullPath + @"\" +
listBox1.SelectedItem.ToString ());
Graphics g = e.Graphics ;
g.DrawImage (b,0,0, b.Width,b.Width );
}
catch
{
MessageBox.Show ("error al mostrar Gráfico");
}
}
}
}
}
Leer las respuestas