Tuesday, July 15, 2008

Load Image to PictureBox in Windows Mobile

Hi guys,
Another cool stuff, many guys, asking in MS forums, how load the image to picture box from the device directory. it's pretty easy, there have many ways.

1.loan image directly from the location
2. load the image from the memory stream.

So here code for you

pictureBox.Image = new Bitmap (“Full.Path.To.Bitmpap.”);

private void LoadImage(string filePath)
using (FileStream reader = new FileStream(filePath, FileMode.Open))
byte[] data = new byte[reader.Length];
reader.Read(data, 0, (int)reader.Length);
using (MemoryStream memory = new MemoryStream(data))
pictureBox1.Image = new Bitmap(memory);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

Thank you


Julio César said...

Hi, i have a question: in the first case, between the () you shoul type the relative path of the image?


LuckyFox said...

please specify about first example, the path should be relative, i am trying diferent versions of path and every time i get the error that path is incorrect during program run

Ryan Hardouin said...

hi there
This worked for me thanks.

MOHD ILYAS said...
This comment has been removed by the author.
Luis Cedeño said...

hi and thanks by the post! but I get an error in the line code:

pictureBox1.Image = new Bitmap(memory);

the error is "OutOfMemoryException"

How to solve it?

I am running on Compact Framework 3.5