Tythtyth 0 Posted March 27, 2011 Report Share Posted March 27, 2011 Eu tenho uma PictureBox e quero que ela ponha uma imagem dependendo do número variável. Se for 1 põe uma imagem, se for 2 põe outra. Como faço isso? Link to post Share on other sites
f-22 0 Posted March 27, 2011 Report Share Posted March 27, 2011 Switch (var) case 1: pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; pictureBox1.Image = Image.FromFile("c:\x.jpg"); break: case N... Link to post Share on other sites
Tythtyth 0 Posted March 27, 2011 Author Report Share Posted March 27, 2011 Vou ter de fazer isso 600 vezes? o: Não se pode fazer tipo por array? Link to post Share on other sites
RJ90 0 Posted March 27, 2011 Report Share Posted March 27, 2011 private void btnChange_Click(object sender, EventArgs e) { Image img1 = Image.FromFile("./Images/green.png"); Image img2 = Image.FromFile("./Images/red.png"); if (nudValue.Value == 0) { pbImage.Image = img1; } else pbImage.Image = img2; } Projecto: http://dl.dropbox.com/u/4494157/PictureBox.rar Cumprimentos, ◣ Samsung 700Z5A-S01PT ◥ ◣ Samsung Ominia W ◥ ᥡ What Else? ᥡ Link to post Share on other sites
Tythtyth 0 Posted March 27, 2011 Author Report Share Posted March 27, 2011 Mesmo assim s: Estou mesmo a ver que vou ter de fazer isso imensas vezes. Link to post Share on other sites
f-22 0 Posted March 27, 2011 Report Share Posted March 27, 2011 Tens de explicar melhor o que queres fazer. O que é para fazer em concreto? Disses-te que querias mudar uma img dependendo de uma var. Tens ai, e agora o que queres? Link to post Share on other sites
RJ90 0 Posted March 27, 2011 Report Share Posted March 27, 2011 Não necessariamente. Crias um método numa classe publica que recebe como parâmetros dois valores e retorna um booleano. Depois quando quiseres usar apenas tens de fazer isto: if(Classe.Método(valor1,valor2) == true) { pbImage.Image = img1; } else pbImage.Image = img2; ◣ Samsung 700Z5A-S01PT ◥ ◣ Samsung Ominia W ◥ ᥡ What Else? ᥡ Link to post Share on other sites
MSOlhao 0 Posted March 28, 2011 Report Share Posted March 28, 2011 Viva, É muito mais simples utilizares um Dictionary para fazeres isso. public partial class Form1 : Form { private Dictionary<int, string> images; public Form1() { InitializeComponent(); fillDictionary(); } private void fillDictionary() { images = new Dictionary<int, string>(); // Adiciona novos items images.Add(1, "image1.jpg"); images.Add(2, "image2.jpg"); images.Add(3, "image3.jpg"); } private void button1_Click(object sender, EventArgs e) { pictureBox1.ImageLocation = getImage(Convert.ToInt32(textBox1.Text)); } private string getImage(int valor) { if (images.ContainsKey(valor)) { return images[valor]; } return string.Empty; } } Link to post Share on other sites
Tythtyth 0 Posted March 28, 2011 Author Report Share Posted March 28, 2011 Mas tenho na mesma de adicionar no Dictionary as imagens, certo? Link to post Share on other sites
xtrm0 1 Posted March 28, 2011 Report Share Posted March 28, 2011 Podes dar nome às imagens tipo 1, 2 ,3 ,4 ,5 ,6, etc.. e fazer: Image img = Image.FromFile("./Images/" + var + ".png"); pbImage.Image = img; Não é obvio? <Signature goes here> Link to post Share on other sites
MSOlhao 0 Posted March 28, 2011 Report Share Posted March 28, 2011 Viva, Sim, com o Dictionary tens de adicionar as imagens, se não quiseres podes usar o exemplo do xtrm0. Link to post Share on other sites
Tythtyth 0 Posted March 28, 2011 Author Report Share Posted March 28, 2011 O problema é que eu não uso as imagens de uma pasta, está em Resources porque eu as importei. Link to post Share on other sites
MSOlhao 0 Posted March 28, 2011 Report Share Posted March 28, 2011 Então dá uma vista de olhos nisto, http://msdn.microsoft.com/en-us/library/aa287676(v=vs.71).aspx Link to post Share on other sites
Tythtyth 0 Posted March 30, 2011 Author Report Share Posted March 30, 2011 Não percebi nada nesse site s: Link to post Share on other sites
MSOlhao 0 Posted March 30, 2011 Report Share Posted March 30, 2011 Viva, Dizes que tens as imagens num Resource File (.resx), se deres nomes às imagens do tipo, image1; image2; image3; Podes fazer isto: using System.Resources; private Image getFromResources(String id) { if (!(id.Equals(String.Empty))) { String filename = "image"+ id; ResourceManager rm = new ResourceManager("DictionarySample.ImageResources", GetType().Assembly); Bitmap image = (Bitmap)rm.GetObject(filename); return image; } return null; } // Modo de utilização: private void button1_Click(object sender, EventArgs e) { pictureBox1.Image = getFromResources(textBox1.Text); } Nota: Onde eu tenho DictionarySample.ImageResources, tu colocas o <nome do teu Projecto>.<nome da tua Resource> Link to post Share on other sites
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now