Skip to main content

Best way to display a JPG

 
Logged in as: Guest
Users viewing this topic: none
  Printable Version
All Forums >> [.NET Compact Framework Development] >> .NET Compact Framework - VB.NET >> Best way to display a JPG Page: [1]
Login
Message << Older Topic   Newer Topic >>
Best way to display a JPG - 4/22/2008 5:35:41 AM   
ymahmood


Posts: 218
Score: 0
Joined: 8/13/2002
From: Birmingham - UK
Status: offline
Hi,
I have images (jpgs) on the handheld, they are actually photo's. I am trying to show them in a picturebox. I have tried two different ways but both ways i am getting errors related to memory.

The first method i tried was to to simply load the jpg as a bitmap and place into picturebox control ie

pcbPhoto.Image = New Bitmap(pPhotoPath & lFieldVal)


Doing it this way started to give me outofmemoryexception errors after displaying different images a number of times, i think i have understood why after reading up in this issue.

So then I changed code to display the images using the OpennetCf way.

What i did was create a function which returned a bitmap object which i then passed to the image property of the Picturebox control. here is that function:


Function Bitmap_Return(ByVal lPath As String, ByVal lWidth As Integer, ByVal lHeight As Integer) As System.Drawing.Bitmap
Dim lFactory As ImagingFactory = New ImagingFactoryClass()
Dim lImg As IImage
Dim lImgB As IBitmapImage
Dim lErrPos As String = ""

 

Try

 

lFactory.CreateImageFromFile(lPath, lImg)
 

lFactory.CreateBitmapFromImage(lImg, lWidth, lHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb, InterpolationHint.InterpolationHintDefault, lImgB)
 

Bitmap_Return = ImageUtils.IBitmapImageToBitmap(lImgB)



System.Runtime.InteropServices.Marshal.ReleaseComObject(lFactory)

System.Runtime.InteropServices.Marshal.ReleaseComObject(lImg)


System.Runtime.InteropServices.Marshal.ReleaseComObject(lImgB)
 

GC.Collect()
 

Catch ex As Exception
MsgBox(
" Err: " & ex.Message, MsgBoxStyle.Critical, "Bitmap_Return")
 

End Try
End Function
 
 
I had to add the garbage collection because the file that i was opening was being locked by the OpennetCF, and GC released it.
 
Any how, i am now getting another error, again the error does not happen all the time: "Not enough storage is available to complete this operation". When I get the error I check the memory status through settings and it seems fine. I cannot work out why apart from possibly a bug on OpennetCF side of things. I have rasied it with them but mean time need need a third way of displaying a photo in a picture box control, Any ideas ??
 
Thanks in advance.
 
Post #: 1
RE: Best way to display a JPG - 4/22/2008 12:48:02 PM   
petevick


Posts: 3320
Score: 0
Joined: 4/26/2002
From: Bolton - UK
Status: offline
Hi,
when you did it the first way, did you dispose of the object, before loading the next picture - I guess that is what is consuming the memory.



_____________________________

Pete Vickers - Microsoft MVP - Device Application Development
www.gui-innovations.com

Please do not email me with questions. Posting your questions in the forums will get a quicker response - Thanks.

(in reply to ymahmood)
Post #: 2
RE: Best way to display a JPG - 4/24/2008 4:44:31 AM   
ymahmood


Posts: 218
Score: 0
Joined: 8/13/2002
From: Birmingham - UK
Status: offline
Pete,
You mentioned "dispose of the object" I assume you are talking about the picturebox.
The pcbPhoto is a picturebox that i am creating dynamically and adding it to a panel. Before i add any controls to that panel I do a Panel.controls.clear. From what i understand doing a clear should be handling the dispose of each object including the picturebox.
Am i right in assuming that or do i need to get rid the of the controls a different way?

Thanks

(in reply to petevick)
Post #: 3
RE: Best way to display a JPG - 4/24/2008 9:19:49 AM   
petevick


Posts: 3320
Score: 0
Joined: 4/26/2002
From: Bolton - UK
Status: offline
Hi,
try doing

Try
pcbphoto.image.dispose
catch
end try

pcbphoto.image = new bitmap....

Pete

_____________________________

Pete Vickers - Microsoft MVP - Device Application Development
www.gui-innovations.com

Please do not email me with questions. Posting your questions in the forums will get a quicker response - Thanks.

(in reply to ymahmood)
Post #: 4
RE: Best way to display a JPG - 4/24/2008 10:18:02 AM   
ymahmood


Posts: 218
Score: 0
Joined: 8/13/2002
From: Birmingham - UK
Status: offline
Pete,

Do i still need to do that even though i am creating the picturebox on the fly.
From what you are saying i should do the following

Panel1.controls.clear
dim lPCB as new PictureBox
lPCB.Image.Dispose
lPCB.image = new Bitmap (.....
Panel1.Controls.Add(lPCB)


Is that what you mean?

(in reply to petevick)
Post #: 5
RE: Best way to display a JPG - 4/24/2008 12:03:53 PM   
petevick


Posts: 3320
Score: 0
Joined: 4/26/2002
From: Bolton - UK
Status: offline
Do you create it on the fly every time you load a picture?

If so, you should dispose of the control when you have finished with it.

_____________________________

Pete Vickers - Microsoft MVP - Device Application Development
www.gui-innovations.com

Please do not email me with questions. Posting your questions in the forums will get a quicker response - Thanks.

(in reply to ymahmood)
Post #: 6
RE: Best way to display a JPG - 4/25/2008 10:26:13 AM   
ymahmood


Posts: 218
Score: 0
Joined: 8/13/2002
From: Birmingham - UK
Status: offline
Pete,
I think i have located what this message "Not enough Storage is available ..." means.

I think i am running out of Virtual Memory. Basically i am logging the Program memory and virtual memory using the following call
GlobalMemoryStatus and the virtual memory is dropping even though i am doing garbage collections etc etc.

I start to get above errors when virtual memory drops from 19202048 down to about 2621440 which i think means approx 2.5 mb.
 

Now that i have a better idea on whats going on, what are my options inorder to resolve this issue of running out of VM.

Thanks so far.

(in reply to petevick)
Post #: 7
RE: Best way to display a JPG - 4/25/2008 11:22:24 AM   
ymahmood


Posts: 218
Score: 0
Joined: 8/13/2002
From: Birmingham - UK
Status: offline
Has picturebox control got a memory leak.

Bypassing the picturebox control and not creating one, the trace showed that i had gained VM from 19136512  to 20054016.
But when i uncomment the picture box code i am losing Virtual memory.

It seems to me that every time I create a Picturebox on the fly it uses up VM and does not release it again.

(in reply to ymahmood)
Post #: 8
RE: Best way to display a JPG - 4/25/2008 11:29:53 AM   
petevick


Posts: 3320
Score: 0
Joined: 4/26/2002
From: Bolton - UK
Status: offline
Hi,
why are you creating it on the fly - why not just use one?

As I said before, if you are creating, and not disposing, you will eat up memory


_____________________________

Pete Vickers - Microsoft MVP - Device Application Development
www.gui-innovations.com

Please do not email me with questions. Posting your questions in the forums will get a quicker response - Thanks.

(in reply to ymahmood)
Post #: 9
RE: Best way to display a JPG - 4/29/2008 5:01:55 AM   
ymahmood


Posts: 218
Score: 0
Joined: 8/13/2002
From: Birmingham - UK
Status: offline
Pete,

Basically the controls are generated on the fly because whether they exist or not is based upon data.

I am disposing of the image and all other controls on the form by doing the following:

Dim lY As Integer

For lY = 0 To pnlForm.Controls.Count - 1

  Try

  If pnlForm.Controls(lY).GetType.Name = "PictureBox" Then

    Dim lbm As PictureBox = pnlForm.Controls(lY)
  lbm.Image.Dispose()
  lbm.Dispose()
  pnlForm.Controls(lY).Dispose()
  Else

  pnlForm.Controls(lY).Dispose()
  End If

  Catch ex As Exception
  End Try

Next

pnlForm.Controls.Clear()



Is that the best way?
Thanks pete!

< Message edited by ymahmood -- 4/29/2008 7:52:12 AM >

(in reply to petevick)
Post #: 10
RE: Best way to display a JPG - 4/30/2008 1:54:39 AM   
petevick


Posts: 3320
Score: 0
Joined: 4/26/2002
From: Bolton - UK
Status: offline
Hi,
looks ok to me :)

Do you still get memory problems?

Pete

_____________________________

Pete Vickers - Microsoft MVP - Device Application Development
www.gui-innovations.com

Please do not email me with questions. Posting your questions in the forums will get a quicker response - Thanks.

(in reply to ymahmood)
Post #: 11
RE: Best way to display a JPG - 5/1/2008 4:23:30 AM   
ymahmood


Posts: 218
Score: 0
Joined: 8/13/2002
From: Birmingham - UK
Status: offline
Pete,

Yes, It's not recovering the Virtual memory.

What else can i try?

< Message edited by ymahmood -- 5/1/2008 8:20:26 AM >

(in reply to petevick)
Post #: 12
RE: Best way to display a JPG - 5/1/2008 9:12:59 AM   
petevick


Posts: 3320
Score: 0
Joined: 4/26/2002
From: Bolton - UK
Status: offline
Force garbage collection?

_____________________________

Pete Vickers - Microsoft MVP - Device Application Development
www.gui-innovations.com

Please do not email me with questions. Posting your questions in the forums will get a quicker response - Thanks.

(in reply to ymahmood)
Post #: 13
RE: Best way to display a JPG - 5/1/2008 11:19:47 AM   
ymahmood


Posts: 218
Score: 0
Joined: 8/13/2002
From: Birmingham - UK
Status: offline
Pete,
I am , after the controls.clear I am doing a gc.collect

I think i have come to the end of this... gonna have to do it another way.

thanks for the help.

(in reply to petevick)
Post #: 14
RE: Best way to display a JPG - 5/1/2008 1:52:37 PM   
petevick


Posts: 3320
Score: 0
Joined: 4/26/2002
From: Bolton - UK
Status: offline
HI,
after loading howmany pictures do you get the error?

Pete

_____________________________

Pete Vickers - Microsoft MVP - Device Application Development
www.gui-innovations.com

Please do not email me with questions. Posting your questions in the forums will get a quicker response - Thanks.

(in reply to ymahmood)
Post #: 15
RE: Best way to display a JPG - 5/2/2008 5:26:11 AM   
ymahmood


Posts: 218
Score: 0
Joined: 8/13/2002
From: Birmingham - UK
Status: offline
Pete,

Basically I am going in and out of a screen, that screen dynamically creates different controls on the fly, one of them is a picturebox which also loads an image using opennetCF method. The image is approx 17kb. I'll get the error after approx 40 mins of going in and out of that screen. The memory trace shows me that when i get the error is when I run out of Virtual memory.

Just another point which may or may not be relevant. I am not using the actual picturebox but my own class which is based on the picturebox. here is the class:


Public
Class PhotoControl
Inherits System.Windows.Forms.PictureBox
 
Dim lMandatory As Boolean

Dim lFileName As String

Dim lText As String




Public Property Mandatory() As Boolean

Get

Return lMandatory
End Get

Set(ByVal value As Boolean)
lMandatory = value
End Set

End Property
 

Public Property FileName() As String

Get

Return lFileName
End Get

Set(ByVal value As String)
lFileName = value
End Set

End Property
 

Public Property Text() As String

Get

Return lText
End Get

Set(ByVal value As String)
lText = value
End Set

End Property
 


End Class



This allows me to add some more properties to the control.

thanks!

(in reply to petevick)
Post #: 16
Page:   [1]
All Forums >> [.NET Compact Framework Development] >> .NET Compact Framework - VB.NET >> Best way to display a JPG Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts


Forum Software © ASPPlayground.NET Advanced Edition 2.5.5 Unicode

0.078