Click to See Complete Forum and Search --> : Outofmemory exception
wiadus
Sep 29th, 2008, 05:00 AM
Hi, I am trying to use the code below to load an image file from db but Iam getting OUTOFMEMORY EXCEPTION could anyone help me out.
Private Sub SetPictures()
Dim cr As DataRow
For Each cr In Me.DsPictures.Pictures.Rows
Me.txtID.Text = cr.Item(0)
Dim arrPicture() As Byte = CType(cr.Item(1), Byte())
Dim ms As New MemoryStream(arrPicture)
With PictureBox1
.Image = New Bitmap(ms)
.SizeMode = PictureBoxSizeMode.StretchImage
End With
ms.Close()
Me.txtFileName.Text = cr.Item(2)
Next
End Sub
petevick
Sep 29th, 2008, 05:14 AM
how big is the bitmap?
Shaggy Hiker
Sep 29th, 2008, 08:39 AM
Why do you have that in a loop? How many iterations through the loop do you go before crashing? Portables generally have very limited memory, so you are probably just overrunning it.
wiadus
Sep 29th, 2008, 08:58 AM
how big is the bitmap?
Is just an ordinary picture taken by digital camera and on shaggy's ? I have the same error without a loop.
gep13
Sep 29th, 2008, 10:17 AM
Hello there,
Based on the other threads that I have seen from you, why are you storing the picture in the database? Why not simply store the location of the picture in the database, and actually store the picture on the file system.
I would have thought this would have made things a lot easier.
Just a thought!!
Gary
petevick
Sep 29th, 2008, 01:00 PM
Hi,
it may just be an 'ordinary picture' - but as Shaggy says - memory is limited.
What is the upper bound of the array?
If you save it as a jpg, can you load it into a picture box without an error?
As Gary says, you can just store the location on the database, and the picture on the storage card. I have used both methods in the past, and certainly storing on a card is easier - unless you are using replication or RDA.
Pete
wiadus
Sep 30th, 2008, 06:11 AM
Hi,
it may just be an 'ordinary picture' - but as Shaggy says - memory is limited.
What is the upper bound of the array?
If you save it as a jpg, can you load it into a picture box without an error?
As Gary says, you can just store the location on the database, and the picture on the storage card. I have used both methods in the past, and certainly storing on a card is easier - unless you are using replication or RDA.
Pete
Thanks All, My db is on the card and I am able to open the images form the device and save it to the db but to retrieve from the db is the problem now. I think I have to give. Thanks every one
petevick
Sep 30th, 2008, 06:49 AM
HI,
this is my working code...
Dim m_image As Byte() = CType(rs("EmpPhoto"), Byte())
Dim ms As New MemoryStream(m_image)
Dim bmap As New Bitmap(ms)
I then save it to an image
bmap.Save(strCurrentPhoto, System.Drawing.Imaging.ImageFormat.Png)
where strCurrentPhoto is the full name, e.g. \program files\my app\my data\photo.png
I then set the picture box to the file just saved
pbPhoto.Image = New Bitmap(strCurrentPhoto)
This works.
wiadus
Sep 30th, 2008, 06:12 PM
bmap.Save(strCurrentPhoto, System.Drawing.Imaging.ImageFormat.Png)
where strCurrentPhoto is the full name, e.g. \program files\my app\my data\photo.png
I then set the picture box to the file just saved
pbPhoto.Image = New Bitmap(strCurrentPhoto)
Thanks Pet but what do you mean by the full name of the file? Your example look like the directory of the db. Could you explain that bit a little further. Tis is how my table looke like (Table name Pictures and Columns PictureID, Picture, FileName)
gep13
Sep 30th, 2008, 06:16 PM
Hey,
Looking at your column names, am I right in assuming that FileName contains a path to where the photo is stored on the Mobile Device?
If that is the case, then all you need to do is the following:
pbPhoto.Image = New Bitmap(<your file name, extracted from the DB>)
You don't need any streams at all :)
Gary
gep13
Sep 30th, 2008, 06:31 PM
Or actually, looking again at your code...
Assuming that the FileName column in your DB is just that, a FileName, create a folder on your Mobile Device, and save the photo into that folder, using the FileName as it's name, and then load the photo up into the PictureBox from there.
Gary
petevick
Oct 1st, 2008, 12:50 AM
Hi,
that is what my code does - extracts the photo, saves it as a file, and loads the file.
I don't know how to explain it further really except maybe the line
bmap.Save(strCurrentPhoto, System.Drawing.Imaging.ImageFormat.Png)
could be shown as
bmap.Save("\Program files\my app\my data\myphoto.png", System.Drawing.Imaging.ImageFormat.Png)
I am simply extracting the bitmap from the database, saving it as a .png (or .bmp or .jpg) and then loading the saved file into a picture box
wiadus
Oct 2nd, 2008, 10:49 AM
[QUOTE=petevick]HI,
this is my working code...
Dim m_image As Byte() = CType(rs("EmpPhoto"), Byte())
Dim ms As New MemoryStream(m_image)
Dim bmap As New Bitmap(ms)
I then save it to an image
bmap.Save(strCurrentPhoto, System.Drawing.Imaging.ImageFormat.Png)
Thanks All, but Petevick if you look at your code above (rs) is a datatable or dataset so why do you go back to the db again if the images has been populated in the dataset
So how do you declear this (strCurrentPhoto) and the thing is somebody like petevivk can have about ten pictures in my db having a filename column as Pete and the picture columns.
gep13
Oct 2nd, 2008, 10:54 AM
Hey,
I think it would help if you declared what exactly it is that you are trying to achieve?
PeteVicks solution is based around extracting the picture data from the database, saving it to the file system and then loading it into the picturebox.
Is this what you are wanting to do?
This sounds rather convoluted to me. To me, it would make sense to store the photos on the file system of the Mobile Device, maybe a "Pictures" folder in the root of the application, or on the storage card. From there, you store the exact path to the picture in the database, and use that to populate the picturebox.
What exactly is it you are trying to achieve?
Gary
wiadus
Oct 2nd, 2008, 05:11 PM
Gary, what I am trying to achieve is, I store number of pictures from diffrent people some may have 2 or more pictures in the db. Saving the pictures to the db now is ok but to populate it and bind it to the picturebox is the problem now. Gary let say you Gary have 10 pictures in my db I can't save your pitures with diffrent names except Gary. So in the select statement, I use the filename column as a parameter so when you type Gary and on button click all the rows having a filename column with 'Gary' will be populated in a dataset and I have to bind the picture column to the picturebox so that on button click I can navigate the 10 pictures of Gary forward and backwards. This all what I want to achieve.
gep13
Oct 2nd, 2008, 09:41 PM
Hey,
Ok, so now we have a clearer idea of what it is you are trying to achieve, I have a couple of questions.
How are you populating the photos into the database? Are they synchronized with a database on a server somewhere, or are they added to the database on the mobile device?
Also, I can understand the need to associate the username with the picture, but it is still maybe a good idea to have a column that uniquely identifies each picture. For instance, what if you wanted to order the pictures in a particular way, or wanted to navigate to a particular picture, your current table would not allow this.
Based on what you have told us, in order to use the technique that pete has suggested, each time you write the photo to the file system, you could append the filename with an incrementing number, so for instance the files would be saved to the file system as Gary1, Gary2 Gary3 etc. That make sense.
Given that you are having an issue loading the pictures into the picturebox directly from the database, your own option seems to be to save them to the file system first. Depending on your answer to my first question, then i still think that saving the pictures to the file system by default makes sense. And just store the path to the picture in the database.
Thanks
Gary
petevick
Oct 3rd, 2008, 01:06 AM
Have you tried out the 'working code' I posted, and does it work for you as a first step?
wiadus
Oct 3rd, 2008, 10:21 AM
bmap.Save(strCurrentPhoto, System.Drawing.Imaging.ImageFormat.Png)
Hi Petevick I would like to try it but the above part of the code this (strCurrentPhoto) needs to be decleared and I would like to know how you declear it. Thanks.
gep13
Oct 3rd, 2008, 10:38 AM
Hey,
All that variable is, is a path to where the file should be saved.
Create a folder on your device, and then use that path, plus the filename for the picture. From your previous post, you said that the filename is always the same, so append it with a number so that it is unique.
All you are trying to do at the minute is prove the concept. You could even hard code the strCurrentPhoto variable as PeteVick described in post #12.
Gary
gep13
Oct 3rd, 2008, 10:39 AM
It is a string variable
Dim strCurrentPhoto As String
strCurrentPhoto = "<path to where you want the photo saved>"
Gary
wiadus
Oct 3rd, 2008, 02:51 PM
It is a string variable
Dim strCurrentPhoto As String
strCurrentPhoto = "<path to where you want the photo saved>"
Gary
I just tried Petevick code as below
Dim strCurrent As String = Nothing
Dim m_image As Byte() = CType(dtView("Picture"), Byte())
Dim ms As New MemoryStream(m_image)
Dim bmap As New Bitmap(ms)
bmap.Save(strCurrent, System.Drawing.Imaging.ImageFormat.Png)
Me.PictureBox1.Image = New Bitmap(strCurrent)
And this the error I got ( InvalidCastException was unhandle, conversion from string "Picture" to type Integer is not valid)
gep13
Oct 3rd, 2008, 02:54 PM
But you haven't specified the path to where you want to save the the picture!!!
You need to give it the equivalent of saving a file to C:\temp\pic1.jpg.
Gary
gep13
Oct 3rd, 2008, 02:55 PM
Also,
What line are you getting the error on?
Gary
wiadus
Oct 3rd, 2008, 04:05 PM
Also,
What line are you getting the error on?
Gary
I am not saving the picture. I have code which is able to save the images in the db alright. I want to populate them and using bindingsource I can bind them to the picturebox so that I can movenext, moveprevious, movefirst and movelast. Saving is not a proble now but populating and show them in the picturebox is the problem now. Thanks
gep13
Oct 3rd, 2008, 04:07 PM
Hey,
The problem that you are having is that you can't load the image directly from the DB into the picturebox, correct?
The workaround that is being suggested is that you extract the picture information from the DB, save it to the filesystem, and then load it into the picturebox. In order to use the code the Pete has provided, this has to be done.
Gary
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.