|
-
Sep 29th, 2008, 05:00 AM
#1
Thread Starter
Hyperactive Member
Outofmemory exception
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.
Code:
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
-
Sep 29th, 2008, 05:14 AM
#2
Frenzied Member
Re: Outofmemory exception
-
Sep 29th, 2008, 08:39 AM
#3
Re: Outofmemory exception
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.
My usual boring signature: Nothing
 
-
Sep 29th, 2008, 08:58 AM
#4
Thread Starter
Hyperactive Member
Re: Outofmemory exception
 Originally Posted by petevick
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.
-
Sep 29th, 2008, 10:17 AM
#5
Re: Outofmemory exception
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
-
Sep 29th, 2008, 01:00 PM
#6
Frenzied Member
Re: Outofmemory exception
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
-
Sep 30th, 2008, 06:11 AM
#7
Thread Starter
Hyperactive Member
Re: Outofmemory exception
 Originally Posted by petevick
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
-
Sep 30th, 2008, 06:49 AM
#8
Frenzied Member
Re: Outofmemory exception
HI,
this is my working code...
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
Code:
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
Code:
pbPhoto.Image = New Bitmap(strCurrentPhoto)
This works.
-
Sep 30th, 2008, 06:12 PM
#9
Thread Starter
Hyperactive Member
Re: Outofmemory exception
Code:
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
Code:
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)
-
Sep 30th, 2008, 06:16 PM
#10
Re: Outofmemory exception
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:
Code:
pbPhoto.Image = New Bitmap(<your file name, extracted from the DB>)
You don't need any streams at all 
Gary
-
Sep 30th, 2008, 06:31 PM
#11
Re: Outofmemory exception
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
-
Oct 1st, 2008, 12:50 AM
#12
Frenzied Member
Re: Outofmemory exception
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
-
Oct 2nd, 2008, 10:49 AM
#13
Thread Starter
Hyperactive Member
Re: Outofmemory exception
[QUOTE=petevick]HI,
this is my working code...
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
Code:
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.
-
Oct 2nd, 2008, 10:54 AM
#14
Re: Outofmemory exception
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
-
Oct 2nd, 2008, 05:11 PM
#15
Thread Starter
Hyperactive Member
Re: Outofmemory exception
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.
-
Oct 2nd, 2008, 09:41 PM
#16
Re: Outofmemory exception
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
-
Oct 3rd, 2008, 01:06 AM
#17
Frenzied Member
Re: Outofmemory exception
Have you tried out the 'working code' I posted, and does it work for you as a first step?
-
Oct 3rd, 2008, 10:21 AM
#18
Thread Starter
Hyperactive Member
Re: Outofmemory exception
Code:
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.
-
Oct 3rd, 2008, 10:38 AM
#19
Re: Outofmemory exception
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
-
Oct 3rd, 2008, 10:39 AM
#20
Re: Outofmemory exception
It is a string variable
Dim strCurrentPhoto As String
strCurrentPhoto = "<path to where you want the photo saved>"
Gary
-
Oct 3rd, 2008, 02:51 PM
#21
Thread Starter
Hyperactive Member
Re: Outofmemory exception
 Originally Posted by gep13
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
Code:
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)
-
Oct 3rd, 2008, 02:54 PM
#22
Re: Outofmemory exception
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
-
Oct 3rd, 2008, 02:55 PM
#23
Re: Outofmemory exception
Also,
What line are you getting the error on?
Gary
-
Oct 3rd, 2008, 04:05 PM
#24
Thread Starter
Hyperactive Member
Re: Outofmemory exception
 Originally Posted by gep13
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
-
Oct 3rd, 2008, 04:07 PM
#25
Re: Outofmemory exception
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|