Rabu, 04 Februari 2009

Loading image files vb 2005


First, let me say that i haven't written codes since the early days of VB.NET 2003. However, I'm back and I'm catching on. So I'm working on a project and I would like to load an image into a picture box.

This code works:

pictureBox.Image = Image.FromFile("C:\Empty Device.jpg")

Then i thought, when i take the program to another computer, the image 'Empty device' won't be in the C: of that computer. I remember a way to solve this problem in the past VB versions was to put the image file in the bin folder and write the code as above without the drive letter. That doesn't seem to work in VB2005. So is there a new solution to this?

Also if you can be kind enough to include a similar solution to opening other files (like word files, etc) without having to put drive letters since I would be taking the program to another computer.


Check whether the image exists using...

Code:
if new io.FileInfo("C:\Empty Device.jpg").exists then
pictureBox.Image = Image.FromFile("C:\Empty Device.jpg")
end if




Alternatively, distribute the image with your application and place it into the BIN directory. You can then refer to the image and the BIN directory location using...

Code:
pictureBox.Image = Application.StartupPath & "\Empty Device.jpg"




And lastly, you can add the image as a resource of your project, e.g. you effectively embed the image into the executable, and then retreive the image through the My namespace, e.g.

Code:
pictureBox.Image = My.Resources.Empty_Device.jpg




Note: all of the above is air-coded, for thorough examples search the forum or play around with intellisense for the appropriate methods.

improvise, overcome, go home
Microsoft MVP Visual Basic 2004, 2005, 2006, 2007 & 2008



source :http://www.vbcity.com

-------------------------------------------------------

Trik Gambar Bergerak

Trik Gambar-dimouse

Trik hapus pwd mysql

Trik insertin to db

Trik jadi root dilinux

Trik jam-distatus-bar

Trik Koneksi-ke database

Trik Koneksi-msql-php

Trik lihat-database-mysql

Trik membahas-fungsi-else

Trik member-area